Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

5.0 KB
Raw Blame
"""Application service for resolving web-app access."""

from collections.abc import Mapping, Sequence
from typing import Protocol

from enums import WebAppAccessMode

_PERMISSION_CHECK_MODES = frozenset({WebAppAccessMode.PRIVATE, WebAppAccessMode.PRIVATE_ALL})


class WebAppAccessQuery(Protocol):
    def find_app_id_by_code(self, app_code: str) -> str | None: ...

    def is_app_available(self, app_id: str) -> bool: ...


class WebAppAccessPolicyGateway(Protocol):
    def get_access_mode(self, app_id: str) -> WebAppAccessMode: ...

    def is_user_allowed(self, *, user_id: str, app_id: str) -> bool: ...


class WebAppAccessModesQuery(Protocol):
    def __call__(self, *, app_ids: Sequence[str]) -> Mapping[str, WebAppAccessMode]: ...


class WebAppUserPermissionsQuery(Protocol):
    def __call__(self, *, user_id: str, app_ids: Sequence[str]) -> Mapping[str, bool]: ...


class WebAppAccessReferenceRequiredError(ValueError):
    """Raised when neither an app ID nor an app code was provided."""


class WebAppAccessAppNotFoundError(LookupError):
    """Raised when an app code does not resolve to an app."""


class WebAppAccessUnavailableError(RuntimeError):
    """Raised when an access dependency cannot answer the query."""


class WebAppAccessQueryService:
    def __init__(
        self,
        *,
        access: WebAppAccessQuery,
        policy: WebAppAccessPolicyGateway,
        webapp_auth_enabled: bool,
        get_access_modes: WebAppAccessModesQuery,
        get_user_permissions: WebAppUserPermissionsQuery,
    ) -> None:
        self._access: WebAppAccessQuery = access
        self._policy: WebAppAccessPolicyGateway = policy
        self._webapp_auth_enabled: bool = webapp_auth_enabled
        self._get_access_modes: WebAppAccessModesQuery = get_access_modes
        self._get_user_permissions: WebAppUserPermissionsQuery = get_user_permissions

    def get_app_id_by_code(self, app_code: str) -> str:
        app_id = self._access.find_app_id_by_code(app_code)
        if app_id is None:
            raise WebAppAccessAppNotFoundError(f"App with code {app_code} not found")
        return app_id

    def resolve_app_id(self, *, app_id: str | None = None, app_code: str | None = None) -> str:
        """Resolve a published public App identity without tokens or user writes."""
        if app_code:
            app_id = self._access.find_app_id_by_code(app_code)
            if app_id is None:
                raise WebAppAccessAppNotFoundError
        elif app_id and not self._access.is_app_available(app_id):
            raise WebAppAccessAppNotFoundError
        if not app_id:
            raise WebAppAccessReferenceRequiredError("appId or appCode must be provided")
        return app_id

    def get_access_mode(self, *, app_id: str | None, app_code: str | None) -> WebAppAccessMode:
        # Preserve the auth-disabled, reference-free bootstrap shortcut. Supplied
        # references must still resolve to a live App regardless of auth settings.
        if not self._webapp_auth_enabled and not app_id and not app_code:
            return WebAppAccessMode.PUBLIC
        app_id = self.resolve_app_id(app_id=app_id, app_code=app_code)
        return self.get_configured_access_mode(app_id=app_id)

    def get_configured_access_mode(self, *, app_id: str) -> WebAppAccessMode:
        """Read policy for an App already authorized by a management context.

        Management must be able to configure unpublished or disabled sites.
        Public callers must use get_access_mode to retain publication admission.
        """
        if not self._webapp_auth_enabled:
            return WebAppAccessMode.PUBLIC
        return self._policy.get_access_mode(app_id)

    def find_app_id_by_code(self, app_code: str) -> str | None:
        return self._access.find_app_id_by_code(app_code)

    def requires_permission_check(self, app_id: str) -> bool:
        return self.is_permission_check_required(self._policy.get_access_mode(app_id))

    def requires_authentication(self, app_id: str) -> bool:
        return self._webapp_auth_enabled and self._policy.get_access_mode(app_id) != WebAppAccessMode.PUBLIC

    @staticmethod
    def is_permission_check_required(access_mode: str) -> bool:
        return access_mode in _PERMISSION_CHECK_MODES

    def is_user_allowed(self, *, user_id: str, app_id: str) -> bool:
        if not self._webapp_auth_enabled:
            return True

        return self._policy.is_user_allowed(user_id=user_id, app_id=app_id)

    def batch_get_access_modes(self, *, app_ids: Sequence[str]) -> Mapping[str, WebAppAccessMode]:
        """Return successfully parsed modes; missing IDs do not grant access."""
        if not self._webapp_auth_enabled or not app_ids:
            return dict.fromkeys(app_ids, WebAppAccessMode.PUBLIC)

        return self._get_access_modes(app_ids=app_ids)

    def batch_get_user_permissions(self, *, user_id: str, app_ids: Sequence[str]) -> Mapping[str, bool]:
        if not self._webapp_auth_enabled or not app_ids:
            return dict.fromkeys(app_ids, True)

        return self._get_user_permissions(user_id=user_id, app_ids=app_ids)