Files
dify/api/controllers/openapi/auth/conditions.py
+18 107bba0116 feat: dev snippet fronted (#36784)
Signed-off-by: dependabot[bot] <[email protected]>
Signed-off-by: EvanYao826 <[email protected]>
Co-authored-by: yyh <[email protected]>
Co-authored-by: 盐粒 Yanli <[email protected]>
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
Co-authored-by: Tianle <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Yunlu Wen <[email protected]>
Co-authored-by: zyssyz123 <[email protected]>
Co-authored-by: Claude Opus 4.7 (1M context) <[email protected]>
Co-authored-by: chariri <[email protected]>
Co-authored-by: Asuka Minato <[email protected]>
Co-authored-by: Copilot Autofix powered by AI <[email protected]>
Co-authored-by: Nian <[email protected]>
Co-authored-by: 非法操作 <[email protected]>
Co-authored-by: Carmen Fernández Ruiz <[email protected]>
Co-authored-by: wangxiaolei <[email protected]>
Co-authored-by: QuantumGhost <[email protected]>
Co-authored-by: L1nSn0w <[email protected]>
Co-authored-by: Evan <[email protected]>
Co-authored-by: Escape0707 <[email protected]>
Co-authored-by: Jingyi <[email protected]>
Co-authored-by: Amr Sherif <[email protected]>
Co-authored-by: ZHOU ZHICHEN <[email protected]>
Co-authored-by: unknown <[email protected]>
Co-authored-by: JzoNg <[email protected]>
Co-authored-by: Xiyuan Chen <[email protected]>
Co-authored-by: -LAN- <[email protected]>
2026-05-28 17:34:14 +08:00

54 lines
1.9 KiB
Python

from __future__ import annotations
from collections.abc import Callable
from controllers.openapi.auth.data import AuthData, Edition, RequestContext, current_edition
from libs.oauth_bearer import TokenType
from services.enterprise.enterprise_service import WebAppAccessMode
from services.feature_service import FeatureService
CondFn = Callable[[RequestContext, AuthData | None], bool]
class Cond:
def __init__(self, fn: CondFn) -> None:
self._fn = fn
def __call__(self, ctx: RequestContext, data: AuthData | None = None) -> bool:
return self._fn(ctx, data)
def __and__(self, other: Cond) -> Cond:
return Cond(lambda ctx, data: self(ctx, data) and other(ctx, data))
def __or__(self, other: Cond) -> Cond:
return Cond(lambda ctx, data: self(ctx, data) or other(ctx, data))
def __invert__(self) -> Cond:
return Cond(lambda ctx, data: not self(ctx, data))
def request_cond(fn: Callable[[RequestContext], bool]) -> Cond:
return Cond(lambda ctx, _: fn(ctx))
def data_cond(fn: Callable[[AuthData], bool]) -> Cond:
return Cond(lambda _, data: data is not None and fn(data))
def config_cond(fn: Callable[[], bool]) -> Cond:
return Cond(lambda _, __: fn())
TOKEN_IS_OAUTH_ACCOUNT = request_cond(lambda ctx: ctx.token_type == TokenType.OAUTH_ACCOUNT)
TOKEN_IS_OAUTH_EXTERNAL_SSO = request_cond(lambda ctx: ctx.token_type == TokenType.OAUTH_EXTERNAL_SSO)
PATH_HAS_APP_ID = request_cond(lambda ctx: "app_id" in ctx.path_params)
EDITION_CE = config_cond(lambda: current_edition() == Edition.CE)
EDITION_EE = config_cond(lambda: current_edition() == Edition.EE)
EDITION_SAAS = config_cond(lambda: current_edition() == Edition.SAAS)
WEBAPP_AUTH_ENABLED = config_cond(lambda: FeatureService.get_system_features().webapp_auth.enabled)
LOADED_APP_IS_PRIVATE = data_cond(lambda data: data.app_access_mode == WebAppAccessMode.PRIVATE)