Files
dify/api/machinery/scope.py
Yunlu WenandDevin f183e0364b fix(api): let resource-scoped PATs discover their workspaces (#46610)
Co-authored-by: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
2026-11-18 07:11:03 +00:00

27 lines
898 B
Python

"""Identifier scopes that admission grants pass into application services.
An ``IdScope`` is either ``UNRESTRICTED`` (the grant adds no restriction to that
domain) or a ``frozenset`` of permitted IDs, where an empty set denies every
object. The marker is a distinct value so "no restriction" can never be confused
with an absent or forgotten value.
"""
from enum import Enum
from typing import Final, Literal
class Unrestricted(Enum):
"""Single-member marker type for a domain a grant leaves unrestricted."""
UNRESTRICTED = "unrestricted"
UNRESTRICTED: Final = Unrestricted.UNRESTRICTED
type IdScope = frozenset[str] | Literal[Unrestricted.UNRESTRICTED]
def scope_admits(scope: IdScope, object_id: str) -> bool:
"""Return whether ``scope`` permits ``object_id`` (narrowing the grant only, never widening permissions)."""
return scope is UNRESTRICTED or object_id in scope