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 Optional
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 AccessDecision, AccessResource, AccessSubject, RbacPolicy
def check_access(policy: RbacPolicy, subject: AccessSubject, action: str, resource: AccessResource) -> AccessDecision:
"""May this subject do this action to this resource? An unrestricted grant
allows it; an :own grant only when the subject owns the resource. A
resource with no owner is nobody's own."""
by_name = check_policy(policy)
if not isinstance(subject.id, str) or subject.id == "":
raise ValueError("subject.id must be a non-empty string")
grants = grants_of(by_name, expand_roles(by_name, subject.roles))
rtype, act, _ = parse_permission("%s:%s" % (resource.resource_type, action), False)
own_grant: Optional[str] = None
for grant in grants:
r, a, own_only = parse_grant(grant)
if (r == "*" or r == rtype) and (a == "*" or a == act):
if not own_only:
return AccessDecision(allowed=True, reason="granted", grant=grant)
if own_grant is None:
own_grant = grant
if own_grant is not None:
if resource.owner_id is not None and resource.owner_id == subject.id:
return AccessDecision(allowed=True, reason="granted_own", grant=own_grant)
return AccessDecision(allowed=False, reason="not_owner", grant=None)
return AccessDecision(allowed=False, reason="denied", grant=None)