Smail/server/utils/context/context.go
smail dd04bdb49e
Some checks are pending
Security Checks / security (push) Waiting to run
完成安全整改、配置重构与邮件认证优化
2026-08-28 14:33:13 +08:00

64 lines
1.3 KiB
Go

package context
import (
"context"
"git.pcux.cn/xuans/Smail/services/access"
)
const (
LogID = "LogID"
)
type Context struct {
context.Context `json:"-"`
UserID int
UserAccount string
UserName string
Role access.Role
Values map[string]any
Lang string
IsAdmin bool
MustChangePassword int
RecentAuth bool
RecoveryCodeConfigured bool
TargetRecoveryConfigured bool
PluginInstanceAuthorized bool
AuditScopeAuthorized bool
DeploymentChannel bool
}
// HasRole 判断当前请求是否属于指定角色。
func (c *Context) HasRole(role access.Role) bool {
return c != nil && c.Role == role
}
// EffectiveRole 兼容旧调用方,确保旧会话仍能得到明确角色。
func (c *Context) EffectiveRole() access.Role {
if c == nil {
return ""
}
if c.Role.Valid() {
return c.Role
}
if c.IsAdmin {
return access.RoleSuperAdmin
}
return access.RoleUser
}
func (c *Context) SetValue(key string, value any) {
if c.Values == nil {
c.Values = map[string]any{}
}
c.Values[key] = value
}
func (c *Context) GetValue(key string) any {
if c.Values == nil {
return nil
}
return c.Values[key]
}