Rbac
rbac
¶
Role-based access control: roles, permissions, and enforcement.
Permission format: action:scope where scope is a domain name or
domain.entity pair. The wildcard * matches everything.
Examples:
read:Orders— read any entity in the Orders domainwrite:Billing.Invoice— write the Invoice entity in Billingdelete:*— delete anything*:*— superuser (all actions, all scopes)
Built-in roles
- admin —
*:* - editor —
read:*,write:* - viewer —
read:*
Action
¶
Bases: str, Enum
Permission actions.
RoleDefinition
¶
Bases: BaseModel
A named role with a list of permission strings.
RBACConfig
¶
Bases: BaseModel
Declarative RBAC configuration (lives under rbac in auth.json).
RBACPolicy
¶
RBACPolicy(config: RBACConfig | None = None)
Resolves roles to permissions and checks access.
Merges built-in roles with any custom roles from config. Custom roles with the same name as a built-in role override the built-in.
Source code in libs/ninja-auth/src/ninja_auth/rbac.py
roles
¶
permissions_for_roles
¶
Return the union of permissions granted by roles.
Source code in libs/ninja-auth/src/ninja_auth/rbac.py
is_allowed
¶
Check whether permissions grant action on domain (optionally entity).
Source code in libs/ninja-auth/src/ninja_auth/rbac.py
check
¶
Like :meth:is_allowed but raises :class:PermissionError on denial.
Source code in libs/ninja-auth/src/ninja_auth/rbac.py
permission_matches
¶
Return True if grant satisfies required.
Wildcards
*in the action position matches any action.*in the scope position matches any scope.DomainNamein grant scope matchesDomainName.AnyEntity.
Source code in libs/ninja-auth/src/ninja_auth/rbac.py
require_domain_permission
¶
require_domain_permission(
action: str,
domain: str,
entity: str | None = None,
*,
policy: RBACPolicy | None = None,
) -> None
Raise :class:PermissionError if the current user lacks the permission.
Imports current_user_context lazily to avoid circular imports.