检查点:保存安全方式二选一授权调整以补充回归覆盖
This commit is contained in:
1 parent
13c1cdcba3
commit
96656d9b21
12 files changed
+40
-20
No files matched your search
@@ -22,7 +22,9 @@ func authorizeCapabilityWithNewRole(ctx *context.Context, capability access.Capa
|
||||
OwnResource: ownResource,
|
||||
RecentAuth: ctx.RecentAuth,
|
||||
RecoveryCodeConfigured: ctx.RecoveryCodeConfigured,
|
||||
ExternalEmailConfigured: ctx.ExternalEmailConfigured,
|
||||
TargetRecoveryConfigured: ctx.TargetRecoveryConfigured,
|
||||
TargetExternalEmailConfigured: ctx.TargetExternalEmailConfigured,
|
||||
NewRole: newRole,
|
||||
PluginInstanceAuthorized: ctx.PluginInstanceAuthorized,
|
||||
AuditScopeAuthorized: ctx.AuditScopeAuthorized,
|
||||
|
||||
@@ -372,7 +372,7 @@ func configSectionFromPath(path string) (config.Section, bool) {
|
||||
}
|
||||
|
||||
func configActor(ctx *appcontext.Context, req *http.Request) config.AuditActor {
|
||||
actor := config.AuditActor{UserID: strconv.Itoa(ctx.UserID), Role: string(ctx.EffectiveRole()), RecentAuth: ctx.RecentAuth, RecoveryCodeConfigured: ctx.RecoveryCodeConfigured}
|
||||
actor := config.AuditActor{UserID: strconv.Itoa(ctx.UserID), Role: string(ctx.EffectiveRole()), RecentAuth: ctx.RecentAuth, RecoveryCodeConfigured: ctx.RecoveryCodeConfigured, ExternalEmailConfigured: ctx.ExternalEmailConfigured}
|
||||
actor.Name = ctx.UserAccount
|
||||
if sessionManager := session.Instance; sessionManager != nil {
|
||||
if cookie, err := req.Cookie(sessionManager.Cookie.Name); err == nil {
|
||||
|
||||
@@ -164,13 +164,24 @@ func InvalidateSecurityRuntimeUser(userID int, account string) {
|
||||
}
|
||||
|
||||
func LoadSecurityContext(ctx *context.Context) {
|
||||
if ctx == nil || ctx.UserID <= 0 {
|
||||
if ctx == nil {
|
||||
return
|
||||
}
|
||||
// 每次请求重新读取,停用邮箱或用完安全码后不能沿用旧的授权状态。
|
||||
ctx.RecoveryCodeConfigured = false
|
||||
ctx.ExternalEmailConfigured = false
|
||||
if ctx.UserID <= 0 {
|
||||
return
|
||||
}
|
||||
if ctx.EffectiveRole() == access.RoleSuperAdmin {
|
||||
ctx.RecoveryCodeConfigured = false
|
||||
return
|
||||
}
|
||||
if db.Instance != nil {
|
||||
configured, err := db.Instance.Where("user_id = ? AND status = ?", ctx.UserID, models.ExternalEmailStatusVerified).Exist(&models.ExternalEmail{})
|
||||
if err == nil {
|
||||
ctx.ExternalEmailConfigured = configured
|
||||
}
|
||||
}
|
||||
service := currentSecurityRuntime().credentials
|
||||
if service == nil && db.Instance != nil {
|
||||
service, _ = credentials.NewService(db.Instance)
|
||||
|
||||
@@ -52,7 +52,7 @@ func loadNotificationUser(ctx *context.Context, w http.ResponseWriter, userID in
|
||||
if access.IsDenied(err, access.CodeRecentAuthRequired) {
|
||||
writeSecurityErrorWithCode(w, response.NoAccessPrivileges, "请先确认当前管理员密码", string(access.CodeRecentAuthRequired))
|
||||
} else if access.IsDenied(err, access.CodeRecoveryCodeRequired) {
|
||||
writeSecurityError(w, response.NoAccessPrivileges, "请先为当前管理员配置安全码")
|
||||
writeSecurityError(w, response.NoAccessPrivileges, "请先为当前管理员配置有效安全码或验证外部邮箱")
|
||||
} else {
|
||||
writeSecurityError(w, response.NoAccessPrivileges, "无权修改该账号的系统通知授权")
|
||||
}
|
||||
|
||||
+10
-10
@@ -421,7 +421,7 @@ func EditUser(ctx *context.Context, w http.ResponseWriter, req *http.Request) {
|
||||
}
|
||||
if reqData.Password != "" {
|
||||
if err := authorizeCapability(ctx, access.CapabilityPasswordReset, targetRole, false); err != nil {
|
||||
writeUserAuthorizationError(w, err, "密码重置需要为当前管理员配置有效安全码")
|
||||
writeUserAuthorizationError(w, err, "请先为当前管理员配置有效安全码或验证外部邮箱")
|
||||
return
|
||||
}
|
||||
}
|
||||
@@ -436,17 +436,17 @@ func EditUser(ctx *context.Context, w http.ResponseWriter, req *http.Request) {
|
||||
return
|
||||
}
|
||||
ctx.TargetRecoveryConfigured = false
|
||||
if credentialService := credentialRuntime(); credentialService != nil {
|
||||
configured, checkErr := credentialService.HasValidRecoveryCode(user.ID)
|
||||
if checkErr != nil {
|
||||
log.WithContext(ctx).WithError(checkErr).Error("load target recovery code status failed")
|
||||
response.NewErrorResponse(response.ServerError, "读取目标用户恢复码状态失败", "").FPrint(w)
|
||||
return
|
||||
}
|
||||
ctx.TargetRecoveryConfigured = configured
|
||||
ctx.TargetExternalEmailConfigured = false
|
||||
recoveryStatus, emailStatus, checkErr := loadUserSecurityStatus([]models.User{user})
|
||||
if checkErr != nil {
|
||||
log.WithContext(ctx).WithError(checkErr).Error("load target recovery options failed")
|
||||
response.NewErrorResponse(response.ServerError, "读取目标用户安全方式失败", "").FPrint(w)
|
||||
return
|
||||
}
|
||||
ctx.TargetRecoveryConfigured = recoveryStatus[user.ID]
|
||||
ctx.TargetExternalEmailConfigured = emailStatus[user.ID]
|
||||
if err := authorizeCapabilityWithNewRole(ctx, access.CapabilityRoleManage, targetRole, newRole, false); err != nil {
|
||||
writeUserAuthorizationError(w, err, "更改权限级别前,请先为该用户配置有效安全码")
|
||||
writeUserAuthorizationError(w, err, "更改权限级别前,请先为该用户配置有效安全码或验证外部邮箱")
|
||||
return
|
||||
}
|
||||
user.ApplyRole(newRole)
|
||||
|
||||
@@ -57,7 +57,7 @@ func loadManagedSecurityUser(ctx *context.Context, w http.ResponseWriter, userID
|
||||
return nil, false
|
||||
}
|
||||
if access.IsDenied(err, access.CodeRecoveryCodeRequired) {
|
||||
writeSecurityErrorWithCode(w, response.NoAccessPrivileges, "请先为当前管理员配置有效安全码", string(access.CodeRecoveryCodeRequired))
|
||||
writeSecurityErrorWithCode(w, response.NoAccessPrivileges, "请先为当前管理员配置有效安全码或验证外部邮箱", string(access.CodeRecoveryCodeRequired))
|
||||
return nil, false
|
||||
}
|
||||
writeSecurityError(w, response.NoAccessPrivileges, "无权管理该用户的安全信息")
|
||||
|
||||
Reference in new issue
Block a user