NewAccessController 创建访问控制器
(config *AccessControlConfig, auditLog AuditLog)
| 267 | |
| 268 | // NewAccessController 创建访问控制器 |
| 269 | func NewAccessController(config *AccessControlConfig, auditLog AuditLog) *AccessController { |
| 270 | if config == nil { |
| 271 | config = &AccessControlConfig{ |
| 272 | SessionTimeout: time.Hour * 24, |
| 273 | MaxSessionsPerUser: 5, |
| 274 | EnableSessionCache: true, |
| 275 | EnablePermissionCache: true, |
| 276 | PermissionCacheTimeout: time.Minute * 15, |
| 277 | EnableAudit: true, |
| 278 | AuditLevel: AuditLevelBasic, |
| 279 | LoginFailureLockout: true, |
| 280 | MaxFailedAttempts: 5, |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | ac := &AccessController{ |
| 285 | roles: make(map[string]*Role), |
| 286 | permissions: make(map[string]*Permission), |
| 287 | policies: make(map[string]*AccessPolicy), |
| 288 | users: make(map[string]*User), |
| 289 | sessions: make(map[string]*Session), |
| 290 | userRoles: make(map[string][]string), |
| 291 | rolePermissions: make(map[string][]string), |
| 292 | userPermissions: make(map[string]map[string]bool), |
| 293 | config: config, |
| 294 | auditLog: auditLog, |
| 295 | } |
| 296 | |
| 297 | // 初始化缓存 |
| 298 | if config.EnablePermissionCache { |
| 299 | ac.cache = &AccessCache{ |
| 300 | entries: make(map[string]*CacheEntry), |
| 301 | ttl: config.PermissionCacheTimeout, |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | // 启动清理协程 |
| 306 | go ac.startCleanupWorker() |
| 307 | |
| 308 | return ac |
| 309 | } |
| 310 | |
| 311 | // CreateUser 创建用户 |
| 312 | func (ac *AccessController) CreateUser(user *User) error { |