from typing import Sequence from .auth_rbac_resolve_roles import check_policy, expand_roles, grants_of, parse_grant, parse_permission from .auth_rbac_types import RbacPolicy def has_permission(policy: RbacPolicy, roles: Sequence[str], permission: str) -> bool: """Whether these roles grant a permission. resource:action asks about every resource of the type, so only an unrestricted grant answers yes; resource:action:own asks about the user's own, which an :own grant also covers.""" by_name = check_policy(policy) grants = grants_of(by_name, expand_roles(by_name, roles)) resource, action, own = parse_permission(permission, True) for grant in grants: r, a, own_only = parse_grant(grant) if (r == "*" or r == resource) and (a == "*" or a == action) and (not own_only or own): return True return False