Smail/backend/controllers/authorization.go
smail dbcfc5c461
Some checks are pending
Security Checks / security (push) Waiting to run
完善授权分页与恢复安全校验
2026-09-06 07:22:00 +08:00

32 lines
1.3 KiB
Go

package controllers
import (
"git.pcux.cn/xuans/Smail/services/access"
"git.pcux.cn/xuans/Smail/utils/context"
)
// authorizeCapability 统一把 HTTP 请求上下文映射到能力授权器。
func authorizeCapability(ctx *context.Context, capability access.Capability, targetRole access.Role, ownResource bool) error {
return authorizeCapabilityWithNewRole(ctx, capability, targetRole, "", ownResource)
}
// authorizeCapabilityWithNewRole 在角色变更时同时把目标新角色传给统一授权器。
func authorizeCapabilityWithNewRole(ctx *context.Context, capability access.Capability, targetRole, newRole access.Role, ownResource bool) error {
if ctx == nil {
return &access.Denial{Code: access.CodeAccessDenied}
}
return access.Authorize(access.Request{
ActorRole: ctx.EffectiveRole(),
Capability: capability,
TargetRole: targetRole,
OwnResource: ownResource,
RecentAuth: ctx.RecentAuth,
RecoveryCodeConfigured: ctx.RecoveryCodeConfigured,
TargetRecoveryConfigured: ctx.TargetRecoveryConfigured,
NewRole: newRole,
PluginInstanceAuthorized: ctx.PluginInstanceAuthorized,
AuditScopeAuthorized: ctx.AuditScopeAuthorized,
DeploymentChannel: ctx.DeploymentChannel,
})
}