Functional Weave
Code in Python

auth.rbac@1.0.0

impl/python/has_permission.py

866 bytes · the Python implementation · view raw

Imports name this capability’s declared dependencies, which fune builds next to it in your project; each one links to its page.

from typing import Sequence

from .auth_rbac_resolve_roles import check_policy, expand_roles, grants_of, parse_grant, parse_permission  ← resolveRoles, another function of this group · built into the same file, even by a slim install
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