Compare commits
2 Commits
a4ccf12b7a
...
8c89d89382
Author | SHA1 | Date | |
---|---|---|---|
8c89d89382 | |||
5200e00c9b |
69
.idea/workspace.xml
generated
69
.idea/workspace.xml
generated
@ -4,13 +4,14 @@
|
|||||||
<option name="autoReloadType" value="ALL" />
|
<option name="autoReloadType" value="ALL" />
|
||||||
</component>
|
</component>
|
||||||
<component name="ChangeListManager">
|
<component name="ChangeListManager">
|
||||||
<list default="true" id="3fefb2c4-b6f9-466b-a523-53352e8d6f95" name="更改" comment=":sparkles: Reset password APIs">
|
<list default="true" id="3fefb2c4-b6f9-466b-a523-53352e8d6f95" name="更改" comment=":recycle: Optimized the initial permission system">
|
||||||
|
<change afterPath="$PROJECT_DIR$/pkg/internal/models/audit.go" afterDir="false" />
|
||||||
|
<change afterPath="$PROJECT_DIR$/pkg/internal/server/admin/permissions_api.go" afterDir="false" />
|
||||||
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
|
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
|
||||||
<change beforePath="$PROJECT_DIR$/pkg/internal/server/api/accounts_api.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/server/api/accounts_api.go" afterDir="false" />
|
<change beforePath="$PROJECT_DIR$/pkg/internal/database/migrator.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/database/migrator.go" afterDir="false" />
|
||||||
<change beforePath="$PROJECT_DIR$/pkg/internal/server/api/index.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/server/api/index.go" afterDir="false" />
|
<change beforePath="$PROJECT_DIR$/pkg/internal/server/admin/index.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/server/admin/index.go" afterDir="false" />
|
||||||
<change beforePath="$PROJECT_DIR$/pkg/internal/services/accounts.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/services/accounts.go" afterDir="false" />
|
<change beforePath="$PROJECT_DIR$/pkg/internal/services/events.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/services/events.go" afterDir="false" />
|
||||||
<change beforePath="$PROJECT_DIR$/web/src/views/flow/confirm.vue" beforeDir="false" afterPath="$PROJECT_DIR$/web/src/views/flow/confirm.vue" afterDir="false" />
|
<change beforePath="$PROJECT_DIR$/pkg/main.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/main.go" afterDir="false" />
|
||||||
<change beforePath="$PROJECT_DIR$/web/src/views/flow/password-reset.vue" beforeDir="false" afterPath="$PROJECT_DIR$/web/src/views/flow/password-reset.vue" afterDir="false" />
|
|
||||||
</list>
|
</list>
|
||||||
<option name="SHOW_DIALOG" value="false" />
|
<option name="SHOW_DIALOG" value="false" />
|
||||||
<option name="HIGHLIGHT_CONFLICTS" value="true" />
|
<option name="HIGHLIGHT_CONFLICTS" value="true" />
|
||||||
@ -45,34 +46,34 @@
|
|||||||
<option name="hideEmptyMiddlePackages" value="true" />
|
<option name="hideEmptyMiddlePackages" value="true" />
|
||||||
<option name="showLibraryContents" value="true" />
|
<option name="showLibraryContents" value="true" />
|
||||||
</component>
|
</component>
|
||||||
<component name="PropertiesComponent"><![CDATA[{
|
<component name="PropertiesComponent">{
|
||||||
"keyToString": {
|
"keyToString": {
|
||||||
"DefaultGoTemplateProperty": "Go File",
|
"DefaultGoTemplateProperty": "Go File",
|
||||||
"Go Build.Backend.executor": "Run",
|
"Go Build.Backend.executor": "Run",
|
||||||
"Go 构建.Backend.executor": "Run",
|
"Go 构建.Backend.executor": "Run",
|
||||||
"RunOnceActivity.ShowReadmeOnStart": "true",
|
"RunOnceActivity.ShowReadmeOnStart": "true",
|
||||||
"RunOnceActivity.go.formatter.settings.were.checked": "true",
|
"RunOnceActivity.go.formatter.settings.were.checked": "true",
|
||||||
"RunOnceActivity.go.migrated.go.modules.settings": "true",
|
"RunOnceActivity.go.migrated.go.modules.settings": "true",
|
||||||
"RunOnceActivity.go.modules.automatic.dependencies.download": "true",
|
"RunOnceActivity.go.modules.automatic.dependencies.download": "true",
|
||||||
"RunOnceActivity.go.modules.go.list.on.any.changes.was.set": "true",
|
"RunOnceActivity.go.modules.go.list.on.any.changes.was.set": "true",
|
||||||
"git-widget-placeholder": "master",
|
"git-widget-placeholder": "master",
|
||||||
"go.import.settings.migrated": "true",
|
"go.import.settings.migrated": "true",
|
||||||
"go.sdk.automatically.set": "true",
|
"go.sdk.automatically.set": "true",
|
||||||
"last_opened_file_path": "/Users/littlesheep/Documents/Projects/Hydrogen/Passport/web/src/views",
|
"last_opened_file_path": "/Users/littlesheep/Documents/Projects/Hydrogen/Passport/web/src/views",
|
||||||
"node.js.detected.package.eslint": "true",
|
"node.js.detected.package.eslint": "true",
|
||||||
"node.js.selected.package.eslint": "(autodetect)",
|
"node.js.selected.package.eslint": "(autodetect)",
|
||||||
"nodejs_package_manager_path": "npm",
|
"nodejs_package_manager_path": "npm",
|
||||||
"run.code.analysis.last.selected.profile": "pProject Default",
|
"run.code.analysis.last.selected.profile": "pProject Default",
|
||||||
"settings.editor.selected.configurable": "preferences.pluginManager",
|
"settings.editor.selected.configurable": "preferences.pluginManager",
|
||||||
"ts.external.directory.path": "/Users/littlesheep/Documents/Projects/Hydrogen/Passport/web/node_modules/typescript/lib",
|
"ts.external.directory.path": "/Users/littlesheep/Documents/Projects/Hydrogen/Passport/web/node_modules/typescript/lib",
|
||||||
"vue.rearranger.settings.migration": "true"
|
"vue.rearranger.settings.migration": "true"
|
||||||
},
|
},
|
||||||
"keyToStringList": {
|
"keyToStringList": {
|
||||||
"DatabaseDriversLRU": [
|
"DatabaseDriversLRU": [
|
||||||
"postgresql"
|
"postgresql"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
}]]></component>
|
}</component>
|
||||||
<component name="RecentsManager">
|
<component name="RecentsManager">
|
||||||
<key name="CopyFile.RECENT_KEYS">
|
<key name="CopyFile.RECENT_KEYS">
|
||||||
<recent name="$PROJECT_DIR$/web/src/views" />
|
<recent name="$PROJECT_DIR$/web/src/views" />
|
||||||
@ -156,8 +157,6 @@
|
|||||||
</option>
|
</option>
|
||||||
</component>
|
</component>
|
||||||
<component name="VcsManagerConfiguration">
|
<component name="VcsManagerConfiguration">
|
||||||
<MESSAGE value=":bug: Authenticate wrong payload hotfix" />
|
|
||||||
<MESSAGE value=":sparkles: Can pick up mfa request" />
|
|
||||||
<MESSAGE value=":sparkles: Status system" />
|
<MESSAGE value=":sparkles: Status system" />
|
||||||
<MESSAGE value=":bug: Fix status expired in cache" />
|
<MESSAGE value=":bug: Fix status expired in cache" />
|
||||||
<MESSAGE value=":bug: Fix online condition" />
|
<MESSAGE value=":bug: Fix online condition" />
|
||||||
@ -181,7 +180,9 @@
|
|||||||
<MESSAGE value=":bug: Fix API mapping issue" />
|
<MESSAGE value=":bug: Fix API mapping issue" />
|
||||||
<MESSAGE value=":recycle: Improve notify API" />
|
<MESSAGE value=":recycle: Improve notify API" />
|
||||||
<MESSAGE value=":sparkles: Reset password APIs" />
|
<MESSAGE value=":sparkles: Reset password APIs" />
|
||||||
<option name="LAST_COMMIT_MESSAGE" value=":sparkles: Reset password APIs" />
|
<MESSAGE value=":sparkles: Password reset & user lookup API" />
|
||||||
|
<MESSAGE value=":recycle: Optimized the initial permission system" />
|
||||||
|
<option name="LAST_COMMIT_MESSAGE" value=":recycle: Optimized the initial permission system" />
|
||||||
</component>
|
</component>
|
||||||
<component name="VgoProject">
|
<component name="VgoProject">
|
||||||
<settings-migrated>true</settings-migrated>
|
<settings-migrated>true</settings-migrated>
|
||||||
|
@ -21,6 +21,7 @@ var AutoMaintainRange = []any{
|
|||||||
&models.ActionEvent{},
|
&models.ActionEvent{},
|
||||||
&models.Notification{},
|
&models.Notification{},
|
||||||
&models.NotificationSubscriber{},
|
&models.NotificationSubscriber{},
|
||||||
|
&models.AuditRecord{},
|
||||||
}
|
}
|
||||||
|
|
||||||
func RunMigration(source *gorm.DB) error {
|
func RunMigration(source *gorm.DB) error {
|
||||||
|
13
pkg/internal/models/audit.go
Normal file
13
pkg/internal/models/audit.go
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
package models
|
||||||
|
|
||||||
|
import "gorm.io/datatypes"
|
||||||
|
|
||||||
|
type AuditRecord struct {
|
||||||
|
BaseModel
|
||||||
|
|
||||||
|
Action string `json:"action"`
|
||||||
|
Metadata datatypes.JSONMap `json:"metadata"`
|
||||||
|
UserAgent string `json:"user_agent"`
|
||||||
|
IpAddress string `json:"ip_address"`
|
||||||
|
AccountID uint `json:"account_id"`
|
||||||
|
}
|
@ -11,5 +11,7 @@ func MapAdminAPIs(app *fiber.App) {
|
|||||||
admin.Delete("/badges/:badgeId", revokeBadge)
|
admin.Delete("/badges/:badgeId", revokeBadge)
|
||||||
|
|
||||||
admin.Post("/notify/all", notifyAllUser)
|
admin.Post("/notify/all", notifyAllUser)
|
||||||
|
|
||||||
|
admin.Put("/users/:user/permissions", editUserPermission)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
46
pkg/internal/server/admin/permissions_api.go
Normal file
46
pkg/internal/server/admin/permissions_api.go
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
package admin
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"git.solsynth.dev/hydrogen/passport/pkg/internal/database"
|
||||||
|
"git.solsynth.dev/hydrogen/passport/pkg/internal/models"
|
||||||
|
"git.solsynth.dev/hydrogen/passport/pkg/internal/server/exts"
|
||||||
|
"git.solsynth.dev/hydrogen/passport/pkg/internal/services"
|
||||||
|
"github.com/gofiber/fiber/v2"
|
||||||
|
)
|
||||||
|
|
||||||
|
func editUserPermission(c *fiber.Ctx) error {
|
||||||
|
userId, _ := c.ParamsInt("user")
|
||||||
|
|
||||||
|
if err := exts.EnsureGrantedPerm(c, "AdminUserPermission", true); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
operator := c.Locals("user").(models.Account)
|
||||||
|
|
||||||
|
var data struct {
|
||||||
|
PermNodes map[string]any `json:"perm_nodes" validate:"required"`
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := exts.BindAndValidate(c, &data); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
var user models.Account
|
||||||
|
if err := database.C.Where("id = ?", userId).First(&user).Error; err != nil {
|
||||||
|
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("account was not found: %v", err))
|
||||||
|
}
|
||||||
|
|
||||||
|
prev := user.PermNodes
|
||||||
|
user.PermNodes = data.PermNodes
|
||||||
|
|
||||||
|
if err := database.C.Save(&user).Error; err != nil {
|
||||||
|
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
||||||
|
} else {
|
||||||
|
services.AddAuditRecord(operator, "user.permissions.edit", c.IP(), c.Get(fiber.HeaderUserAgent), map[string]any{
|
||||||
|
"previous_permissions": prev,
|
||||||
|
"new_permissions": data.PermNodes,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
return c.SendStatus(fiber.StatusOK)
|
||||||
|
}
|
@ -118,8 +118,8 @@ func ConfirmAccount(code string) error {
|
|||||||
for k, v := range viper.GetStringMap("permissions.verified") {
|
for k, v := range viper.GetStringMap("permissions.verified") {
|
||||||
if val, ok := user.PermNodes[k]; !ok {
|
if val, ok := user.PermNodes[k]; !ok {
|
||||||
user.PermNodes[k] = v
|
user.PermNodes[k] = v
|
||||||
} else if !ComparePermNode(val, v) {
|
} else {
|
||||||
user.PermNodes[k] = v
|
user.PermNodes[k] = val
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3,18 +3,46 @@ package services
|
|||||||
import (
|
import (
|
||||||
"git.solsynth.dev/hydrogen/passport/pkg/internal/database"
|
"git.solsynth.dev/hydrogen/passport/pkg/internal/database"
|
||||||
"git.solsynth.dev/hydrogen/passport/pkg/internal/models"
|
"git.solsynth.dev/hydrogen/passport/pkg/internal/models"
|
||||||
|
"github.com/rs/zerolog/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
func AddEvent(user models.Account, event, target, ip, ua string) models.ActionEvent {
|
var writeEventQueue []models.ActionEvent
|
||||||
evt := models.ActionEvent{
|
var writeAuditQueue []models.AuditRecord
|
||||||
|
|
||||||
|
// AddEvent to keep operation logs by user themselves clear to query
|
||||||
|
func AddEvent(user models.Account, event, target, ip, ua string) {
|
||||||
|
writeEventQueue = append(writeEventQueue, models.ActionEvent{
|
||||||
Type: event,
|
Type: event,
|
||||||
Target: target,
|
Target: target,
|
||||||
IpAddress: ip,
|
IpAddress: ip,
|
||||||
UserAgent: ua,
|
UserAgent: ua,
|
||||||
AccountID: user.ID,
|
AccountID: user.ID,
|
||||||
}
|
})
|
||||||
|
}
|
||||||
database.C.Save(&evt)
|
|
||||||
|
// AddAuditRecord to keep logs to make administrators' operations clear to query
|
||||||
return evt
|
func AddAuditRecord(operator models.Account, act, ip, ua string, metadata map[string]any) {
|
||||||
|
writeAuditQueue = append(writeAuditQueue, models.AuditRecord{
|
||||||
|
Action: act,
|
||||||
|
Metadata: metadata,
|
||||||
|
IpAddress: ip,
|
||||||
|
UserAgent: ua,
|
||||||
|
AccountID: operator.ID,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// SaveEventChanges runs every 60 seconds to save events / audits changes into database
|
||||||
|
func SaveEventChanges() {
|
||||||
|
if len(writeEventQueue) > 0 {
|
||||||
|
count := len(writeEventQueue)
|
||||||
|
database.C.CreateInBatches(writeEventQueue, min(count, 1000))
|
||||||
|
log.Info().Int("count", count).Msg("Saved action events changes into database...")
|
||||||
|
clear(writeEventQueue)
|
||||||
|
}
|
||||||
|
if len(writeAuditQueue) > 0 {
|
||||||
|
count := len(writeAuditQueue)
|
||||||
|
database.C.CreateInBatches(writeAuditQueue, min(count, 1000))
|
||||||
|
log.Info().Int("count", count).Msg("Saved audit records changes into database...")
|
||||||
|
clear(writeAuditQueue)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -70,6 +70,7 @@ func main() {
|
|||||||
quartz.AddFunc("@every 60s", services.RecycleAuthContext)
|
quartz.AddFunc("@every 60s", services.RecycleAuthContext)
|
||||||
quartz.AddFunc("@every 60m", services.RecycleUnConfirmAccount)
|
quartz.AddFunc("@every 60m", services.RecycleUnConfirmAccount)
|
||||||
quartz.AddFunc("@every 5m", services.KexCleanup)
|
quartz.AddFunc("@every 5m", services.KexCleanup)
|
||||||
|
quartz.AddFunc("@every 60s", services.SaveEventChanges)
|
||||||
quartz.Start()
|
quartz.Start()
|
||||||
|
|
||||||
// Messages
|
// Messages
|
||||||
|
@ -44,7 +44,10 @@ dsn = "host=localhost user=postgres password=password dbname=hy_passport port=54
|
|||||||
prefix = "passport_"
|
prefix = "passport_"
|
||||||
|
|
||||||
[permissions.default]
|
[permissions.default]
|
||||||
CreatePaperclipAttachments = 1048576
|
CreatePost = true
|
||||||
|
CreateAttachments = 1048576
|
||||||
|
|
||||||
[permissions.verified]
|
[permissions.verified]
|
||||||
CreatePaperclipAttachments = 26214400
|
CreateRealm = true
|
||||||
|
CreateArticle = true
|
||||||
|
CreateAttachments = 26214400
|
||||||
|
Loading…
x
Reference in New Issue
Block a user