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.

fix: fix user create app not display in app list (#44929)

wangxiaoleie8b0bb9b7265
diff --git a/api/repositories/app/console_repository.py b/api/repositories/app/console_repository.py
index 08787fde139..04477de9f1d 100644
--- a/api/repositories/app/console_repository.py
+++ b/api/repositories/app/console_repository.py
@@ -15,7 +15,7 @@ from typing import Any, cast, override
import sqlalchemy as sa
from pydantic import TypeAdapter
-from sqlalchemy import ColumnElement, delete, select
+from sqlalchemy import delete, select
from sqlalchemy.exc import IntegrityError
from sqlalchemy.orm import Session, sessionmaker
@@ -403,10 +403,9 @@ class ConsoleAppRepository(ConsoleApps, DiscoveryApps, RelatedApps):
if params.is_created_by_me:
filters.append(App.created_by == user_id)
elif params.accessible_app_ids is not None:
- accessible_filter: ColumnElement[bool] = App.id.in_(params.accessible_app_ids)
- if params.include_own_apps:
- accessible_filter = sa.or_(App.maintainer == user_id, accessible_filter)
- filters.append(accessible_filter)
+ filters.append(sa.or_(App.maintainer == user_id, App.id.in_(params.accessible_app_ids)))
+ if params.resource_app_ids is not None:
+ filters.append(App.id.in_(params.resource_app_ids))
if params.creator_ids:
filters.append(App.created_by.in_(params.creator_ids))
if params.name:
diff --git a/api/services/app/access.py b/api/services/app/access.py
index 4b3e8fe03f2..5f97660d15c 100644
--- a/api/services/app/access.py
+++ b/api/services/app/access.py
@@ -12,10 +12,6 @@ from services.rbac.contracts import MyPermissionsResponse, ResourceWhitelistReso
# the console and OpenAPI app-list endpoints.
APP_LIST_PERMISSION_KEYS: frozenset[str] = frozenset({"app.preview", "app.acl.preview", "app.full_access"})
-# Workspace permission key that lets a caller see apps they maintain when the
-# app whitelist is unrestricted.
-_MANAGE_OWN_APPS_PERMISSION_KEY = "app.create_and_management"
-
def has_app_list_permission(permission_keys: Sequence[str]) -> bool:
"""Return True if any of ``permission_keys`` grants app list/preview access."""
@@ -28,36 +24,33 @@ class AppAccessFilter:
``accessible_app_ids`` of ``None`` means the caller can see every app in the
workspace (unrestricted). Otherwise it is the exact set of app ids the
- caller may preview; combined with ``can_manage_own_apps`` it can also cover
- apps the caller maintains.
+ caller may preview in addition to apps the caller maintains.
"""
accessible_app_ids: set[str] | None
- can_manage_own_apps: bool
@classmethod
def unrestricted(cls) -> AppAccessFilter:
"""Filter that imposes no restriction (RBAC disabled / not applicable)."""
- return cls(accessible_app_ids=None, can_manage_own_apps=False)
+ return cls(accessible_app_ids=None)
def is_app_accessible(self, app_id: str, maintainer: str | None, account_id: str) -> bool:
"""Whether a single app is visible to the caller under this filter.
Mirrors the service-layer query gate: an app is visible when the filter
is unrestricted, the app id is whitelisted, or the caller maintains it
- and holds ``app.create_and_management``.
+ regardless of workspace permissions.
"""
if self.accessible_app_ids is None:
return True
if app_id in self.accessible_app_ids:
return True
- return self.can_manage_own_apps and maintainer is not None and maintainer == account_id
+ return maintainer is not None and maintainer == account_id
def apply_to_params(self, params: AppListBaseParams) -> None:
if self.accessible_app_ids is None:
return
params.accessible_app_ids = sorted(self.accessible_app_ids)
- params.include_own_apps = self.can_manage_own_apps
def resolve_app_access_filter(
@@ -85,7 +78,6 @@ def app_access_filter_from_permissions(
permissions: MyPermissionsResponse, whitelist_scope: ResourceWhitelistResources
) -> AppAccessFilter:
"""Compute visibility from permission and whitelist snapshots without I/O."""
- can_manage_own_apps = _MANAGE_OWN_APPS_PERMISSION_KEY in permissions.workspace.permission_keys
has_default_preview = has_app_list_permission(permissions.app.default_permission_keys) or has_app_list_permission(
permissions.workspace.permission_keys
)
@@ -99,14 +91,8 @@ def app_access_filter_from_permissions(
if has_app_list_permission(override.permission_keys)
}
- accessible_app_ids: set[str] | None
- if whitelist_scope.unrestricted:
- accessible_app_ids = permission_app_ids
- else:
- # A restricted app whitelist is the highest-priority visibility gate:
- # default preview, per-app permission overrides, and own-app management
- # must not expose apps outside this set.
- accessible_app_ids = set(whitelist_scope.resource_ids)
- can_manage_own_apps = False
-
- return AppAccessFilter(accessible_app_ids=accessible_app_ids, can_manage_own_apps=can_manage_own_apps)
+ # Maintainers always retain visibility of their own apps. The whitelist
+ # takes precedence over permission grants only for other apps.
+ accessible_app_ids = permission_app_ids if whitelist_scope.unrestricted else set(whitelist_scope.resource_ids)
+
+ return AppAccessFilter(accessible_app_ids=accessible_app_ids)
diff --git a/api/services/app/query_service.py b/api/services/app/query_service.py
index 0f577d77d56..c134edb7d17 100644
--- a/api/services/app/query_service.py
+++ b/api/services/app/query_service.py
@@ -66,9 +66,7 @@ class AppDiscoveryService:
def list_apps(self, context: RequestContext, query: AppDiscoveryQuery) -> PaginatedResult[AppDiscoveryEntry]:
access = (
- AppAccessFilter(set(context.resource_app_ids), can_manage_own_apps=False)
- if context.resource_app_ids is not None
- else self._access.visibility(context)
+ AppAccessFilter.unrestricted() if context.resource_app_ids is not None else self._access.visibility(context)
)
try:
app_id = str(UUID(query.name)) if query.name else None
@@ -79,6 +77,7 @@ class AppDiscoveryService:
app = self._apps.find_visible_app(app_id, context.active_workspace_id)
if (
app is None
+ or (context.resource_app_ids is not None and app.id not in context.resource_app_ids)
or app.mode not in SUPPORTED_APP_TYPES
or not access.is_app_accessible(app.id, app.maintainer, context.account_id)
):
@@ -96,6 +95,8 @@ class AppDiscoveryService:
}
)
access.apply_to_params(params)
+ if context.resource_app_ids is not None:
+ params.resource_app_ids = sorted(context.resource_app_ids)
result = self._apps.query_apps(context.account_id, context.active_workspace_id, params)
return self._page(
[app for app in result.items if app.mode in SUPPORTED_APP_TYPES] if result else [],
diff --git a/api/services/app_service.py b/api/services/app_service.py
index 64af129f2c2..212c921dd3a 100644
--- a/api/services/app_service.py
+++ b/api/services/app_service.py
@@ -9,7 +9,7 @@ from typing import Any, NotRequired, TypedDict, cast
import sqlalchemy as sa
from pydantic import JsonValue
-from sqlalchemy import ColumnElement, delete, select
+from sqlalchemy import delete, select
from sqlalchemy.exc import IntegrityError
from sqlalchemy.orm import Session
@@ -255,10 +255,9 @@ class AppService:
if params.is_created_by_me:
filters.append(App.created_by == user_id)
elif params.accessible_app_ids is not None:
- accessible_filter: ColumnElement[bool] = App.id.in_(params.accessible_app_ids)
- if params.include_own_apps:
- accessible_filter = sa.or_(App.maintainer == user_id, accessible_filter)
- filters.append(accessible_filter)
+ filters.append(sa.or_(App.maintainer == user_id, App.id.in_(params.accessible_app_ids)))
+ if params.resource_app_ids is not None:
+ filters.append(App.id.in_(params.resource_app_ids))
if params.creator_ids:
filters.append(App.created_by.in_(params.creator_ids))
if params.name:
diff --git a/api/services/entities/app_entities.py b/api/services/entities/app_entities.py
index 345f60505ef..7a4c207cd3e 100644
--- a/api/services/entities/app_entities.py
+++ b/api/services/entities/app_entities.py
@@ -62,7 +62,8 @@ class AppListBaseParams(BaseModel):
creator_ids: list[str] | None = None
is_created_by_me: bool | None = None
accessible_app_ids: list[str] | None = None
- include_own_apps: bool = False
+ # Credential resource bindings are a strict scope; None means no binding restriction.
+ resource_app_ids: list[str] | None = None
class AppListParams(AppListBaseParams):
diff --git a/api/tests/unit_tests/controllers/console/app/test_workflow.py b/api/tests/unit_tests/controllers/console/app/test_workflow.py
index 33db0588532..746e9519696 100644
--- a/api/tests/unit_tests/controllers/console/app/test_workflow.py
+++ b/api/tests/unit_tests/controllers/console/app/test_workflow.py
@@ -1153,7 +1153,7 @@ class TestWorkflowCases:
def resolve_access(tenant_id: str, account_id: str) -> AppAccessFilter:
actors.append((tenant_id, account_id))
- return AppAccessFilter(accessible_app_ids={app_id_1}, can_manage_own_apps=False)
+ return AppAccessFilter(accessible_app_ids={app_id_1})
monkeypatch.setattr(workflow_module, "WorkflowService", WorkflowQueries)
monkeypatch.setattr(workflow_module, "resolve_app_access_filter", resolve_access)
diff --git a/api/tests/unit_tests/repositories/app/test_console_repository.py b/api/tests/unit_tests/repositories/app/test_console_repository.py
index fe3e5ecc867..df4c619f0d1 100644
--- a/api/tests/unit_tests/repositories/app/test_console_repository.py
+++ b/api/tests/unit_tests/repositories/app/test_console_repository.py
@@ -343,3 +343,20 @@ def test_trace_settings_preserve_stored_json_validation(
) -> None:
app = persist_app(sqlite_session, tracing='{"enabled": "false", "obsolete_field": true}')
assert repository.get_trace(CONTEXT, app.id) == AppTraceSettings()
+
+
[email protected]("empty_whitelist", [True, False])
+def test_app_list_includes_maintained_apps_outside_whitelist(
+ repository: ConsoleAppRepository, sqlite_session: Session, empty_whitelist: bool
+) -> None:
+ own = persist_app(sqlite_session, maintainer=ACTOR, created_by="other")
+ allowed = persist_app(sqlite_session, maintainer="other")
+ persist_app(sqlite_session, maintainer="other", created_by=ACTOR)
+ persist_app(sqlite_session, maintainer=ACTOR, tenant_id=str(uuid4()))
+ params = AppListParams(accessible_app_ids=[] if empty_whitelist else [allowed.id])
+
+ page = repository.list_apps(CONTEXT, params)
+
+ expected = {own.id} if empty_whitelist else {own.id, allowed.id}
+ assert {app.id for app in page.data} == expected
+ assert page.total == len(expected)
diff --git a/api/tests/unit_tests/services/app/test_access.py b/api/tests/unit_tests/services/app/test_access.py
index 3cc9ef9078b..94120aaab91 100644
--- a/api/tests/unit_tests/services/app/test_access.py
+++ b/api/tests/unit_tests/services/app/test_access.py
@@ -60,47 +60,34 @@ class TestAppAccessFilterIsAppAccessible:
assert flt.is_app_accessible("app-1", maintainer="someone", account_id="acc-1")
def test_whitelisted_app_is_visible(self) -> None:
- flt = AppAccessFilter(accessible_app_ids={"app-1"}, can_manage_own_apps=False)
+ flt = AppAccessFilter(accessible_app_ids={"app-1"})
assert flt.is_app_accessible("app-1", maintainer=None, account_id="acc-1")
assert not flt.is_app_accessible("app-2", maintainer=None, account_id="acc-1")
- def test_own_app_visible_only_with_manage_permission(self) -> None:
- own = AppAccessFilter(accessible_app_ids=set(), can_manage_own_apps=True)
+ def test_own_app_visible_without_manage_permission(self) -> None:
+ own = AppAccessFilter(accessible_app_ids=set())
assert own.is_app_accessible("app-1", maintainer="acc-1", account_id="acc-1")
assert not own.is_app_accessible("app-1", maintainer="acc-2", account_id="acc-1")
- no_manage = AppAccessFilter(accessible_app_ids=set(), can_manage_own_apps=False)
- assert not no_manage.is_app_accessible("app-1", maintainer="acc-1", account_id="acc-1")
-
class TestAppAccessFilterApplyToParams:
def test_unrestricted_leaves_params_untouched(self) -> None:
params = AppListParams()
AppAccessFilter.unrestricted().apply_to_params(params)
assert params.accessible_app_ids is None
- assert params.include_own_apps is False
assert params.is_created_by_me is None
- def test_whitelisted_ids_are_sorted_with_own_apps_flag(self) -> None:
+ def test_whitelisted_ids_are_sorted(self) -> None:
params = AppListParams()
- AppAccessFilter(accessible_app_ids={"b", "a"}, can_manage_own_apps=True).apply_to_params(params)
+ AppAccessFilter(accessible_app_ids={"b", "a"}).apply_to_params(params)
assert params.accessible_app_ids == ["a", "b"]
- assert params.include_own_apps is True
- def test_empty_set_with_manage_falls_back_to_maintained_apps(self) -> None:
- # Own-app fallback must use maintainer (include_own_apps), consistent
+ def test_empty_set_keeps_maintainer_filter_separate_from_creator(self) -> None:
+ # Own-app fallback must use maintainer, consistent
# with is_app_accessible — not created_by (is_created_by_me).
params = AppListParams()
- AppAccessFilter(accessible_app_ids=set(), can_manage_own_apps=True).apply_to_params(params)
- assert params.accessible_app_ids == []
- assert params.include_own_apps is True
- assert params.is_created_by_me is None
-
- def test_empty_set_without_manage_sees_nothing(self) -> None:
- params = AppListParams()
- AppAccessFilter(accessible_app_ids=set(), can_manage_own_apps=False).apply_to_params(params)
+ AppAccessFilter(accessible_app_ids=set()).apply_to_params(params)
assert params.accessible_app_ids == []
- assert params.include_own_apps is False
assert params.is_created_by_me is None
@@ -117,7 +104,6 @@ class TestResolveAppAccessFilter:
flt = app_access_filter_from_permissions(permissions, ResourceWhitelistResources(unrestricted=True))
assert flt.accessible_app_ids is None
- assert flt.can_manage_own_apps is False
def test_restricted_whitelist_overrides_default_preview(self) -> None:
permissions = _permissions(
@@ -129,7 +115,6 @@ class TestResolveAppAccessFilter:
)
assert flt.accessible_app_ids == {"app-9"}
- assert flt.can_manage_own_apps is False
def test_override_apps_collected_without_default_preview(self) -> None:
permissions = _permissions(
@@ -173,5 +158,20 @@ class TestResolveAppAccessFilter:
)
assert flt.accessible_app_ids == set()
- assert flt.can_manage_own_apps is False
assert captured == {"tenant_id": "tenant-1", "account_id": "acc-1"}
+
+
[email protected]("workspace_keys", [[], ["app.create_and_management"]])
[email protected]("whitelist_ids", [[], ["allowed"]])
+def test_restricted_whitelist_retains_maintainer_visibility(
+ workspace_keys: list[str], whitelist_ids: list[str]
+) -> None:
+ access = app_access_filter_from_permissions(
+ _permissions(workspace_keys=workspace_keys, app_default_keys=["app.preview"]),
+ ResourceWhitelistResources(unrestricted=False, resource_ids=whitelist_ids),
+ )
+ assert access.is_app_accessible("own", "account", "account")
+ assert not access.is_app_accessible("other", "someone", "account")
+ assert not access.is_app_accessible("unassigned", None, "account")
+ if whitelist_ids:
+ assert access.is_app_accessible("allowed", "someone", "account")
diff --git a/api/tests/unit_tests/services/app/test_console_service.py b/api/tests/unit_tests/services/app/test_console_service.py
index 3f05836f1c1..2b89ba73883 100644
--- a/api/tests/unit_tests/services/app/test_console_service.py
+++ b/api/tests/unit_tests/services/app/test_console_service.py
@@ -137,7 +137,7 @@ class Apps:
@dataclass
class Permissions:
def filter_list(self, params: AppListParams) -> AppListParams:
- return params.model_copy(update={"accessible_app_ids": ["app"], "include_own_apps": True})
+ return params.model_copy(update={"accessible_app_ids": ["app"]})
def keys_for(self, app_ids: list[str]) -> dict[str, list[str]]:
return {app_id: ["app.preview"] for app_id in app_ids}
@@ -332,7 +332,6 @@ def test_list_applies_visibility_and_enriches_materialized_records(ports: Ports)
assert context == CONTEXT
assert isinstance(filtered, AppListParams)
assert filtered.accessible_app_ids == ["app"]
- assert filtered.include_own_apps is True
assert params.accessible_app_ids is None
assert result.page == 2
assert result.data[0].permission_keys == ["app.preview"]
diff --git a/api/tests/unit_tests/services/app/test_discovery_gateway.py b/api/tests/unit_tests/services/app/test_discovery_gateway.py
index 44cf837f3d5..71d65ed33f0 100644
--- a/api/tests/unit_tests/services/app/test_discovery_gateway.py
+++ b/api/tests/unit_tests/services/app/test_discovery_gateway.py
@@ -31,7 +31,7 @@ def test_visibility_needs_no_database_session(
monkeypatch.setattr(Session, "__init__", reject_session)
result = EnterpriseAppDiscoveryAccess().visibility(RequestContext("req", None, "account-1", "tenant-1"))
if rbac_enabled:
- assert result == AppAccessFilter({"app-1"}, False)
+ assert result == AppAccessFilter({"app-1"})
assert send.call_args_list == [
call(
"GET",
diff --git a/api/tests/unit_tests/services/app/test_query_service.py b/api/tests/unit_tests/services/app/test_query_service.py
index 4ea5675f358..3c8a8fc4267 100644
--- a/api/tests/unit_tests/services/app/test_query_service.py
+++ b/api/tests/unit_tests/services/app/test_query_service.py
@@ -84,12 +84,13 @@ def test_list_and_uuid_search_apply_same_visibility(
tenant = Tenant(name="Workspace")
tenant.id = tenant_id
sqlite_session.add(tenant)
- app_id = persist_app(sqlite_session, tenant_id=tenant_id, maintainer=context.account_id)
+ app_id = persist_app(
+ sqlite_session, tenant_id=tenant_id, maintainer="other" if visibility == "denied" else context.account_id
+ )
persist_app(sqlite_session, tenant_id=tenant_id, enable_api=False)
persist_app(sqlite_session, tenant_id=str(uuid4()))
access.access_filter = AppAccessFilter(
None if visibility == "unrestricted" else {app_id} if visibility == "whitelisted" else set(),
- visibility == "own",
)
query = AppDiscoveryQuery(3 if uuid_search else 1, 20, None, app_id.upper() if uuid_search else None)
result = service.list_apps(context, query)
@@ -135,7 +136,7 @@ def test_resource_bindings_replace_account_visibility(
tenant_id = str(uuid4())
app_id = persist_app(sqlite_session, tenant_id=tenant_id, maintainer="token-id")
persist_app(sqlite_session, tenant_id=tenant_id, maintainer="token-id")
- access.access_filter = AppAccessFilter(set(), can_manage_own_apps=True)
+ access.access_filter = AppAccessFilter(set())
context = RequestContext(
"req", None, "token-id", tenant_id, resource_app_ids=frozenset({app_id}) if bound else frozenset()
)