修复管理员分组代管写入的并发权限边界
This commit is contained in:
1 parent
641dda8b2c
commit
1a6c0ec383
10 files changed
+224
-50
No files matched your search
@@ -12,6 +12,13 @@ import (
|
||||
"git.pcux.cn/xuans/Smail/utils/context"
|
||||
)
|
||||
|
||||
func accountWriteGuard(ctx *context.Context, target models.User) accountgroup.WriteGuard {
|
||||
if ctx == nil {
|
||||
return nil
|
||||
}
|
||||
return accountgroup.Guard(ctx.UserID, ctx.EffectiveRole(), ctx.SessionVersion, ctx.SecurityVersion, target)
|
||||
}
|
||||
|
||||
func requireAccountScope(ctx *context.Context, w http.ResponseWriter, targetID int) bool {
|
||||
if ctx == nil || !accountgroup.CanAccess(db.Instance, ctx.UserID, ctx.EffectiveRole(), targetID) {
|
||||
writeSecurityError(w, response.NoAccessPrivileges, "无权查看或管理该账号")
|
||||
@@ -110,7 +117,7 @@ func SetAccountGroupMembership(ctx *context.Context, w http.ResponseWriter, req
|
||||
writeAccountGroupError(w, accountgroup.ErrConflict)
|
||||
return
|
||||
}
|
||||
if err := accountgroup.SetMembership(db.Instance, user, input.GroupIDs); err != nil {
|
||||
if err := accountgroup.SetMembership(db.Instance, user, input.GroupIDs, accountWriteGuard(ctx, user)); err != nil {
|
||||
writeAccountGroupError(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -172,7 +172,7 @@ func SetNotificationAuthorization(ctx *context.Context, w http.ResponseWriter, r
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
err := notification.SetAuthorization(db.Instance, *user, input.Version, *input.Enabled, input.MinuteLimit, input.DayLimit)
|
||||
err := notification.SetAuthorization(db.Instance, *user, input.Version, *input.Enabled, input.MinuteLimit, input.DayLimit, accountWriteGuard(ctx, *user))
|
||||
recordNotificationManagement(ctx, user.ID, "设置系统通知授权", err, map[string]any{"enabled": *input.Enabled, "minute_limit": input.MinuteLimit, "day_limit": input.DayLimit})
|
||||
if err != nil {
|
||||
writeNotificationError(w, err)
|
||||
@@ -191,7 +191,7 @@ func RotateNotificationCredential(ctx *context.Context, w http.ResponseWriter, r
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
code, err := notification.Rotate(db.Instance, *user, input.Version)
|
||||
code, err := notification.Rotate(db.Instance, *user, input.Version, accountWriteGuard(ctx, *user))
|
||||
recordNotificationManagement(ctx, user.ID, "重置系统通知凭据", err, nil)
|
||||
if err != nil {
|
||||
writeNotificationError(w, err)
|
||||
@@ -212,6 +212,10 @@ func recordNotificationManagement(ctx *context.Context, userID int, action strin
|
||||
}
|
||||
|
||||
func writeNotificationError(w http.ResponseWriter, err error) {
|
||||
if errors.Is(err, accountgroup.ErrDenied) || errors.Is(err, accountgroup.ErrConflict) {
|
||||
writeAccountGroupError(w, err)
|
||||
return
|
||||
}
|
||||
switch {
|
||||
case errors.Is(err, notification.ErrConflict):
|
||||
writeSecurityError(w, response.ParamsError, "授权状态已变化,请刷新后重试")
|
||||
|
||||
@@ -320,6 +320,7 @@ func EditUser(ctx *context.Context, w http.ResponseWriter, req *http.Request) {
|
||||
return
|
||||
}
|
||||
targetRole := user.EffectiveRole()
|
||||
writeGuard := accountWriteGuard(ctx, user)
|
||||
if !requireAccountScope(ctx, w, user.ID) {
|
||||
return
|
||||
}
|
||||
@@ -421,6 +422,10 @@ func EditUser(ctx *context.Context, w http.ResponseWriter, req *http.Request) {
|
||||
}
|
||||
}()
|
||||
updateColumns := []string{"name", "disabled", "role", "is_admin"}
|
||||
if err := accountgroup.CheckWrite(tx, writeGuard); err != nil {
|
||||
writeAccountGroupError(w, err)
|
||||
return
|
||||
}
|
||||
if reqData.Password != "" {
|
||||
updateColumns = append(updateColumns, "password", "must_change_password", "password_changed_at")
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@ import (
|
||||
"git.pcux.cn/xuans/Smail/dto/response"
|
||||
"git.pcux.cn/xuans/Smail/models"
|
||||
"git.pcux.cn/xuans/Smail/services/access"
|
||||
"git.pcux.cn/xuans/Smail/services/accountgroup"
|
||||
"git.pcux.cn/xuans/Smail/services/externalemail"
|
||||
"git.pcux.cn/xuans/Smail/services/verification"
|
||||
"git.pcux.cn/xuans/Smail/utils/context"
|
||||
@@ -64,6 +65,14 @@ func loadManagedSecurityUser(ctx *context.Context, w http.ResponseWriter, userID
|
||||
return &target, true
|
||||
}
|
||||
|
||||
func writeManagedScopeError(w http.ResponseWriter, err error) bool {
|
||||
if errors.Is(err, accountgroup.ErrDenied) || errors.Is(err, accountgroup.ErrConflict) {
|
||||
writeAccountGroupError(w, err)
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func managedUserIDFromQuery(req *http.Request) int {
|
||||
if req == nil || req.URL == nil {
|
||||
return 0
|
||||
@@ -112,8 +121,11 @@ func GenerateManagedRecoveryCodes(ctx *context.Context, w http.ResponseWriter, r
|
||||
writeSecurityError(w, response.ServerError, "安全凭据服务不可用")
|
||||
return
|
||||
}
|
||||
codes, err := service.GenerateRecoveryCodesContext(recoveryRequestContext(req), target.ID, input.Count)
|
||||
codes, err := service.GenerateRecoveryCodesContext(recoveryRequestContext(req), target.ID, input.Count, accountWriteGuard(ctx, *target))
|
||||
if err != nil {
|
||||
if writeManagedScopeError(w, err) {
|
||||
return
|
||||
}
|
||||
if writePasswordBusy(w, err) {
|
||||
return
|
||||
}
|
||||
@@ -145,7 +157,10 @@ func RevokeManagedRecoveryCodes(ctx *context.Context, w http.ResponseWriter, req
|
||||
writeSecurityError(w, response.ServerError, "安全凭据服务不可用")
|
||||
return
|
||||
}
|
||||
if err := service.RevokeRecoveryCodes(target.ID); err != nil {
|
||||
if err := service.RevokeRecoveryCodes(target.ID, accountWriteGuard(ctx, *target)); err != nil {
|
||||
if writeManagedScopeError(w, err) {
|
||||
return
|
||||
}
|
||||
log.WithContext(ctx).WithError(err).Error("revoke managed recovery codes failed")
|
||||
writeSecurityError(w, response.ServerError, "撤销安全码失败")
|
||||
return
|
||||
@@ -230,8 +245,11 @@ func ChangeManagedExternalEmail(ctx *context.Context, w http.ResponseWriter, req
|
||||
UserID: target.ID,
|
||||
Email: normalizedEmail,
|
||||
Type: emailType,
|
||||
})
|
||||
}, accountWriteGuard(ctx, *target))
|
||||
if err != nil {
|
||||
if writeManagedScopeError(w, err) {
|
||||
return
|
||||
}
|
||||
if errors.Is(err, externalemail.ErrBindingExists) {
|
||||
writeSecurityError(w, response.ParamsError, "该外部邮箱已经绑定或正在被其他账号使用")
|
||||
return
|
||||
@@ -295,7 +313,10 @@ func VerifyManagedExternalEmail(ctx *context.Context, w http.ResponseWriter, req
|
||||
writeSecurityError(w, response.ParamsError, externalEmailVerificationCheckMessage(err))
|
||||
return
|
||||
}
|
||||
if err := runtime.external.VerifyReplacement(target.ID, input.BindingID); err != nil {
|
||||
if err := runtime.external.VerifyReplacement(target.ID, input.BindingID, accountWriteGuard(ctx, *target)); err != nil {
|
||||
if writeManagedScopeError(w, err) {
|
||||
return
|
||||
}
|
||||
log.WithContext(ctx).WithError(err).Error("verify managed external email replacement failed")
|
||||
writeSecurityError(w, response.ParamsError, "外部邮箱替换失败")
|
||||
return
|
||||
|
||||
Reference in new issue
Block a user