mirror of
https://github.com/langgenius/dify.git
synced 2026-05-28 21:03:22 +08:00
Signed-off-by: dependabot[bot] <support@github.com> Signed-off-by: EvanYao826 <155432245+EvanYao826@users.noreply.github.com> Co-authored-by: yyh <92089059+lyzno1@users.noreply.github.com> Co-authored-by: 盐粒 Yanli <yanli@dify.ai> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: Tianle <40735546+Tianlel@users.noreply.github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Yunlu Wen <yunlu.wen@dify.ai> Co-authored-by: zyssyz123 <916125788@qq.com> Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com> Co-authored-by: chariri <w@chariri.moe> Co-authored-by: Asuka Minato <i@asukaminato.eu.org> Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> Co-authored-by: Nian <11332799+Lillian68@users.noreply.github.com> Co-authored-by: 非法操作 <hjlarry@163.com> Co-authored-by: Carmen Fernández Ruiz <279459669+zeus1959@users.noreply.github.com> Co-authored-by: wangxiaolei <fatelei@gmail.com> Co-authored-by: QuantumGhost <obelisk.reg+git@gmail.com> Co-authored-by: L1nSn0w <l1nsn0w@qq.com> Co-authored-by: Evan <2869018789@qq.com> Co-authored-by: Escape0707 <tothesong@gmail.com> Co-authored-by: Jingyi <jingyi.qi@dify.ai> Co-authored-by: Amr Sherif <140330826+amr-sheriff@users.noreply.github.com> Co-authored-by: ZHOU ZHICHEN <118870511+zhuiguangzhe2003@users.noreply.github.com> Co-authored-by: unknown <EI05187@apwx.com> Co-authored-by: JzoNg <jzongcode@gmail.com> Co-authored-by: Xiyuan Chen <52963600+GareArc@users.noreply.github.com>
68 lines
2.4 KiB
Python
68 lines
2.4 KiB
Python
from __future__ import annotations
|
|
|
|
from werkzeug.exceptions import Forbidden, InternalServerError, NotFound, Unauthorized
|
|
|
|
from controllers.openapi.auth.data import AuthData
|
|
from core.app.entities.app_invoke_entities import InvokeFrom
|
|
from extensions.ext_database import db
|
|
from models.account import TenantStatus
|
|
from services.account_service import AccountService, TenantService
|
|
from services.app_service import AppService
|
|
from services.end_user_service import EndUserService
|
|
from services.enterprise.enterprise_service import EnterpriseService, WebAppAccessMode
|
|
|
|
|
|
def load_app(data: AuthData) -> None:
|
|
app_id = data.path_params["app_id"]
|
|
app = AppService.get_app_by_id(db.session, app_id)
|
|
if not app or app.status != "normal":
|
|
raise NotFound("app not found")
|
|
if not app.enable_api:
|
|
raise Forbidden("service_api_disabled")
|
|
data.app = app
|
|
|
|
|
|
def load_tenant(data: AuthData) -> None:
|
|
if data.app is None:
|
|
raise InternalServerError("pipeline_invariant_violated: app not loaded before load_tenant")
|
|
tenant = TenantService.get_tenant_by_id(db.session, str(data.app.tenant_id))
|
|
if tenant is None or tenant.status == TenantStatus.ARCHIVE:
|
|
raise Forbidden("workspace unavailable")
|
|
data.tenant = tenant
|
|
|
|
|
|
def load_account(data: AuthData) -> None:
|
|
account = AccountService.get_account_by_id(db.session, str(data.account_id))
|
|
if account is None:
|
|
raise Unauthorized("account not found")
|
|
if data.tenant:
|
|
account.current_tenant = data.tenant
|
|
data.caller = account
|
|
data.caller_kind = "account"
|
|
|
|
|
|
def resolve_external_user(data: AuthData) -> None:
|
|
if data.tenant is None or data.app is None or data.external_identity is None:
|
|
raise Unauthorized("missing context for external user resolution")
|
|
end_user = EndUserService.get_or_create_end_user_by_type(
|
|
InvokeFrom.OPENAPI,
|
|
tenant_id=str(data.tenant.id),
|
|
app_id=str(data.app.id),
|
|
user_id=data.external_identity.email,
|
|
)
|
|
data.caller = end_user
|
|
data.caller_kind = "end_user"
|
|
|
|
|
|
def load_app_access_mode(data: AuthData) -> None:
|
|
if data.app is None:
|
|
return
|
|
try:
|
|
settings = EnterpriseService.WebAppAuth.get_app_access_mode_by_id(app_id=str(data.app.id))
|
|
if settings is None:
|
|
data.app_access_mode = None
|
|
return
|
|
data.app_access_mode = WebAppAccessMode(settings.access_mode)
|
|
except ValueError:
|
|
data.app_access_mode = None
|