package rbac import ( "errors" "online-back/internal/identity/rbac/perm" ) // Scope decides which records a role's permissions apply to. It is the only // data-visibility knob: "what can I do" is the permission list, "on whose // records" is the scope. type Scope string const ( ScopeOwn Scope = "own" // only records the caller owns (clients) ScopeBranch Scope = "branch" // records of the caller's branch_user branches ScopeAll Scope = "all" // every branch ) // Name is the Turkmen display label for the admin UI. func (s Scope) Name() string { switch s { case ScopeOwn: return "Diňe öz maglumatlary" case ScopeBranch: return "Öz şahamçalary" case ScopeAll: return "Ähli şahamçalar" default: return string(s) } } func (s Scope) Valid() bool { return s == ScopeOwn || s == ScopeBranch || s == ScopeAll } func (s Scope) rank() int { switch s { case ScopeBranch: return 1 case ScopeAll: return 2 default: return 0 } } // Principal is the caller as rbac sees it, loaded once per request (cached // across requests, see Service.Load). Treat it as read-only. type Principal struct { UserID int64 RoleID int64 RoleCode string Scope Scope IsSuper bool Perms map[perm.Code]struct{} BranchIDs []int64 } func (p *Principal) Can(code perm.Code) bool { if p == nil { return false } if p.IsSuper { return true } _, ok := p.Perms[code] return ok } // PermCodes lists the caller's effective permissions in catalog order. func (p *Principal) PermCodes() []perm.Code { out := []perm.Code{} for _, code := range perm.Codes() { if p.Can(code) { out = append(out, code) } } return out } // RoleInfo is what the grant/edit rules need to know about a role. type RoleInfo struct { ID int64 Code string Scope Scope IsSuper bool IsSystem bool Perms []perm.Code } var ( ErrSuperOnly = errors.New("only a superadmin can grant or change a superadmin role") ErrScopeTooWide = errors.New("role scope is wider than the caller's own scope") ErrPermissionHeld = errors.New("role holds a permission the caller does not have") ErrSystemRole = errors.New("system role scope cannot be changed") ErrUnknownPerm = errors.New("unknown permission code") ErrInvalidScope = errors.New("invalid scope") ) // CanGrant reports whether the caller may give target to a user - or, the // same question, manage a user who already holds target. A caller can only // hand out what they themselves hold: no wider scope, no permission they // lack, and superadmin only from a superadmin. Own-scope roles (clients) // skip the permission check: they only ever reach the holder's own records, // so a cards-only operator can still register a cards+loans client. func (p *Principal) CanGrant(target RoleInfo) error { if p == nil { return ErrSuperOnly } if p.IsSuper { return nil } if target.IsSuper { return ErrSuperOnly } if target.Scope.rank() > p.Scope.rank() { return ErrScopeTooWide } if target.Scope == ScopeOwn { return nil } for _, code := range target.Perms { if !p.Can(code) { return ErrPermissionHeld } } return nil } // CanSaveRole checks a role create (existing == nil) or update. The caller // must be able to grant both the role as it is now and as it will be, so // they can neither widen a role past their own rights nor strip one that is // already out of their reach. func (p *Principal) CanSaveRole(existing *RoleInfo, next RoleInfo) error { if !next.Scope.Valid() { return ErrInvalidScope } for _, code := range next.Perms { if !perm.Valid(code) { return ErrUnknownPerm } } if existing != nil { if existing.IsSystem && next.Scope != existing.Scope { return ErrSystemRole } if err := p.CanGrant(*existing); err != nil { return err } next.IsSuper = existing.IsSuper } return p.CanGrant(next) }