chore: 纳入本地 Dify 1.19.0 源码及品牌与免登改动
演示目录只保留可重建的源码。运行密钥、数据卷、依赖和带口令的本地配置不进入仓库。 Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
60158935f4
commit
6519682885
15831 files changed
+3063428
No files matched your search
@@ -0,0 +1,40 @@
|
||||
from flask import request
|
||||
|
||||
from core.rbac import RBACPermission, RBACResourceScope
|
||||
|
||||
from .checks import RBAC_CHECKS_ATTR, RBACCheck, enforce_rbac_checks
|
||||
from .locators import (
|
||||
AgentBehindApp,
|
||||
AgentId,
|
||||
DatasetByDocument,
|
||||
DatasetByPipeline,
|
||||
DatasetId,
|
||||
PlainApp,
|
||||
ResourceIdentity,
|
||||
ResourceLocator,
|
||||
Workspace,
|
||||
)
|
||||
|
||||
__all__ = [
|
||||
"RBAC_CHECKS_ATTR",
|
||||
"AgentBehindApp",
|
||||
"AgentId",
|
||||
"DatasetByDocument",
|
||||
"DatasetByPipeline",
|
||||
"DatasetId",
|
||||
"PlainApp",
|
||||
"RBACCheck",
|
||||
"RBACPermission",
|
||||
"RBACResourceScope",
|
||||
"ResourceIdentity",
|
||||
"ResourceLocator",
|
||||
"Workspace",
|
||||
"enforce_rbac_checks",
|
||||
"rbac_language",
|
||||
]
|
||||
|
||||
|
||||
def rbac_language() -> str | None:
|
||||
"""Read and normalize the optional RBAC label language from the query string."""
|
||||
value = (request.args.get("language") or "").strip().lower()
|
||||
return value if value in {"en", "ja", "zh"} else None
|
||||
@@ -0,0 +1,69 @@
|
||||
from collections.abc import Mapping, Sequence
|
||||
from dataclasses import dataclass
|
||||
|
||||
from flask import request
|
||||
from werkzeug.exceptions import Forbidden, NotFound
|
||||
|
||||
from configs import dify_config
|
||||
from core.rbac import RBACPermission, RBACResourceScope
|
||||
from services.enterprise.rbac_service import RBACService
|
||||
|
||||
from .locators import ResourceLocator, Workspace
|
||||
|
||||
__all__ = ["RBAC_CHECKS_ATTR", "InvalidRBACCheckError", "RBACCheck", "enforce_rbac_checks"]
|
||||
|
||||
RBAC_CHECKS_ATTR = "rbac_checks"
|
||||
|
||||
|
||||
class InvalidRBACCheckError(RuntimeError):
|
||||
"""A route declares a permission scene with an incompatible resource locator."""
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class RBACCheck:
|
||||
scene: RBACPermission
|
||||
locator: ResourceLocator
|
||||
|
||||
def __post_init__(self) -> None:
|
||||
if isinstance(self.locator, Workspace) or self.scene.scope is RBACResourceScope.WORKSPACE:
|
||||
return
|
||||
if self.locator.scope is not self.scene.scope:
|
||||
raise InvalidRBACCheckError(
|
||||
f"{self.scene} is a {self.scene.scope} scene but {self.locator!r} locates {self.locator.scope}"
|
||||
)
|
||||
|
||||
|
||||
def enforce_rbac_checks(
|
||||
*,
|
||||
tenant_id: str,
|
||||
account_id: str,
|
||||
checks: Sequence[RBACCheck],
|
||||
path_args: Mapping[str, object] | None = None,
|
||||
) -> None:
|
||||
if not dify_config.RBAC_ENABLED:
|
||||
return
|
||||
try:
|
||||
view_args = request.view_args or {}
|
||||
except RuntimeError:
|
||||
view_args = {}
|
||||
merged: dict[str, object] = {**view_args, **(path_args or {})}
|
||||
applicable = [
|
||||
(check, identity) for check in checks if (identity := check.locator.locate(tenant_id, merged)) is not None
|
||||
]
|
||||
if not applicable:
|
||||
raise NotFound()
|
||||
for check, identity in applicable:
|
||||
is_workspace = identity.scope is RBACResourceScope.WORKSPACE
|
||||
owner = None if is_workspace else check.locator.owner_id(tenant_id, identity)
|
||||
if owner is not None and owner == account_id:
|
||||
return
|
||||
allowed = RBACService.CheckAccess.check(
|
||||
tenant_id,
|
||||
account_id,
|
||||
scene=check.scene,
|
||||
resource_type=None if is_workspace else identity.scope,
|
||||
resource_id=None if is_workspace else identity.id,
|
||||
)
|
||||
if allowed:
|
||||
return
|
||||
raise Forbidden()
|
||||
@@ -0,0 +1,154 @@
|
||||
from collections.abc import Mapping
|
||||
from dataclasses import dataclass
|
||||
from typing import Protocol, override, runtime_checkable
|
||||
|
||||
from flask import g
|
||||
from werkzeug.exceptions import NotFound
|
||||
|
||||
from controllers.common.errors import InvalidArgumentError
|
||||
from core.rbac import RBACResourceScope
|
||||
from extensions.ext_application_services import application_services
|
||||
from services.entities.app_entities import AppAgentBinding
|
||||
|
||||
__all__ = [
|
||||
"AgentBehindApp",
|
||||
"AgentId",
|
||||
"DatasetByDocument",
|
||||
"DatasetByPipeline",
|
||||
"DatasetId",
|
||||
"PlainApp",
|
||||
"ResourceIdentity",
|
||||
"ResourceLocator",
|
||||
"Workspace",
|
||||
"agent_binding",
|
||||
]
|
||||
|
||||
_AGENT_BINDING_CACHE_KEY = "_rbac_agent_bindings"
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class ResourceIdentity:
|
||||
scope: RBACResourceScope
|
||||
id: str
|
||||
|
||||
|
||||
@runtime_checkable
|
||||
class ResourceLocator(Protocol):
|
||||
scope: RBACResourceScope
|
||||
|
||||
def locate(self, tenant_id: str, path_args: Mapping[str, object]) -> ResourceIdentity | None: ...
|
||||
|
||||
def owner_id(self, tenant_id: str, identity: ResourceIdentity) -> str | None: ...
|
||||
|
||||
|
||||
def _required(path_args: Mapping[str, object], param: str) -> str:
|
||||
value = path_args.get(param)
|
||||
if not value:
|
||||
raise InvalidArgumentError(description=f"Missing {param} in request path")
|
||||
return str(value)
|
||||
|
||||
|
||||
def agent_binding(tenant_id: str, app_id: str) -> AppAgentBinding | None:
|
||||
cache: dict[tuple[str, str], AppAgentBinding | None] = g.setdefault(_AGENT_BINDING_CACHE_KEY, {})
|
||||
key = (tenant_id, app_id)
|
||||
if key not in cache:
|
||||
cache[key] = application_services().rbac.queries.get_app_agent_binding(tenant_id, app_id)
|
||||
return cache[key]
|
||||
|
||||
|
||||
class Workspace:
|
||||
scope = RBACResourceScope.WORKSPACE
|
||||
|
||||
def locate(self, tenant_id: str, path_args: Mapping[str, object]) -> ResourceIdentity | None:
|
||||
return ResourceIdentity(self.scope, "")
|
||||
|
||||
def owner_id(self, tenant_id: str, identity: ResourceIdentity) -> str | None:
|
||||
return None
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return "Workspace()"
|
||||
|
||||
|
||||
class _ParamLocator:
|
||||
scope: RBACResourceScope
|
||||
default_param: str
|
||||
|
||||
def __init__(self, param: str | None = None):
|
||||
self.param = param or self.default_param
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return f"{type(self).__name__}({self.param!r})"
|
||||
|
||||
|
||||
class AgentId(_ParamLocator):
|
||||
scope = RBACResourceScope.AGENT
|
||||
default_param = "agent_id"
|
||||
|
||||
def locate(self, tenant_id: str, path_args: Mapping[str, object]) -> ResourceIdentity | None:
|
||||
return ResourceIdentity(self.scope, _required(path_args, self.param))
|
||||
|
||||
def owner_id(self, tenant_id: str, identity: ResourceIdentity) -> str | None:
|
||||
return application_services().rbac.queries.get_agent_creator(tenant_id, identity.id)
|
||||
|
||||
|
||||
class PlainApp(_ParamLocator):
|
||||
scope = RBACResourceScope.APP
|
||||
default_param = "app_id"
|
||||
|
||||
def locate(self, tenant_id: str, path_args: Mapping[str, object]) -> ResourceIdentity | None:
|
||||
app_id = _required(path_args, self.param)
|
||||
if agent_binding(tenant_id, app_id) is not None:
|
||||
return None
|
||||
return ResourceIdentity(self.scope, app_id)
|
||||
|
||||
def owner_id(self, tenant_id: str, identity: ResourceIdentity) -> str | None:
|
||||
return application_services().rbac.queries.get_app_maintainer(tenant_id, identity.id)
|
||||
|
||||
|
||||
class AgentBehindApp(_ParamLocator):
|
||||
scope = RBACResourceScope.AGENT
|
||||
default_param = "app_id"
|
||||
|
||||
def locate(self, tenant_id: str, path_args: Mapping[str, object]) -> ResourceIdentity | None:
|
||||
binding = agent_binding(tenant_id, _required(path_args, self.param))
|
||||
if binding is None or binding.workflow_only:
|
||||
return None
|
||||
return ResourceIdentity(self.scope, binding.id)
|
||||
|
||||
def owner_id(self, tenant_id: str, identity: ResourceIdentity) -> str | None:
|
||||
return None
|
||||
|
||||
|
||||
class DatasetId(_ParamLocator):
|
||||
scope = RBACResourceScope.DATASET
|
||||
default_param = "dataset_id"
|
||||
|
||||
def locate(self, tenant_id: str, path_args: Mapping[str, object]) -> ResourceIdentity | None:
|
||||
return ResourceIdentity(self.scope, _required(path_args, self.param))
|
||||
|
||||
def owner_id(self, tenant_id: str, identity: ResourceIdentity) -> str | None:
|
||||
return application_services().rbac.queries.get_dataset_maintainer(tenant_id, identity.id)
|
||||
|
||||
|
||||
class DatasetByDocument(DatasetId):
|
||||
default_param = "document_id"
|
||||
|
||||
@override
|
||||
def locate(self, tenant_id: str, path_args: Mapping[str, object]) -> ResourceIdentity | None:
|
||||
document_id = _required(path_args, self.param)
|
||||
dataset_id = application_services().rbac.queries.get_dataset_id_by_document(tenant_id, document_id)
|
||||
if dataset_id is None:
|
||||
raise NotFound("Document not found")
|
||||
return ResourceIdentity(self.scope, dataset_id)
|
||||
|
||||
|
||||
class DatasetByPipeline(DatasetId):
|
||||
default_param = "pipeline_id"
|
||||
|
||||
@override
|
||||
def locate(self, tenant_id: str, path_args: Mapping[str, object]) -> ResourceIdentity | None:
|
||||
pipeline_id = _required(path_args, self.param)
|
||||
dataset_id = application_services().rbac.queries.get_dataset_id_by_pipeline(tenant_id, pipeline_id)
|
||||
if dataset_id is None:
|
||||
raise NotFound("Dataset not found for pipeline")
|
||||
return ResourceIdentity(self.scope, dataset_id)
|
||||
Reference in new issue
Block a user