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(rbac): grant member policies when a dataset is opened to the work… (#39735)

Benjamin6a37e1538a7b
diff --git a/api/controllers/console/workspace/rbac.py b/api/controllers/console/workspace/rbac.py
index c60f335762a..9aaf2e59e3d 100644
--- a/api/controllers/console/workspace/rbac.py
+++ b/api/controllers/console/workspace/rbac.py
@@ -727,14 +727,17 @@ class RBACDatasetWhitelistApi(Resource):
def put(self, dataset_id):
tenant_id, account_id = _current_ids()
request = _payload(_ResourceAccessScopeRequest)
- return _dump(
- svc.RBACService.DatasetAccess.replace_whitelist(
- tenant_id,
- account_id,
- str(dataset_id),
- svc.ReplaceMemberBindings(scope=request.scope.value),
- )
+ result = svc.RBACService.DatasetAccess.replace_whitelist(
+ tenant_id,
+ account_id,
+ str(dataset_id),
+ svc.ReplaceMemberBindings(scope=request.scope.value),
)
+ # Widening the scope only records it: the members still need the default access policy
+ # before they can reach the dataset, same as the app whitelist route above.
+ if dify_config.RBAC_ENABLED and request.scope is RBACResourceWhitelistScope.ALL:
+ initialize_created_app_rbac_access_task.delay(tenant_id, account_id, dataset_id=str(dataset_id))
+ return _dump(result)
@console_ns.route("/workspaces/current/rbac/datasets/<uuid:dataset_id>/user-access-policies")
diff --git a/api/tests/unit_tests/controllers/console/workspace/test_rbac.py b/api/tests/unit_tests/controllers/console/workspace/test_rbac.py
index 33952546b31..5d6599e23a1 100644
--- a/api/tests/unit_tests/controllers/console/workspace/test_rbac.py
+++ b/api/tests/unit_tests/controllers/console/workspace/test_rbac.py
@@ -329,6 +329,46 @@ class TestResourceAccessScopeBindings:
mock_sync_task.delay.assert_called_once_with("tenant-1", "acct-actor", "app-1")
+ def test_dataset_whitelist_all_schedules_member_policy_sync(self, app):
+ # Widening a dataset to the whole workspace only records the scope; without granting the
+ # default policy to the current members nobody actually gains access.
+ with (
+ app.test_request_context(
+ "/workspaces/current/rbac/datasets/dataset-1/whitelist",
+ method="PUT",
+ json={"scope": "all"},
+ ),
+ patch("controllers.console.workspace.rbac._current_ids", return_value=("tenant-1", "acct-actor")),
+ patch("controllers.console.workspace.rbac.dify_config.RBAC_ENABLED", True),
+ patch(
+ "controllers.console.workspace.rbac.svc.RBACService.DatasetAccess.replace_whitelist",
+ return_value=rbac_mod.svc.ResourceWhitelist(),
+ ),
+ patch("controllers.console.workspace.rbac.initialize_created_app_rbac_access_task") as mock_sync_task,
+ ):
+ inspect.unwrap(rbac_mod.RBACDatasetWhitelistApi.put)(rbac_mod.RBACDatasetWhitelistApi(), "dataset-1")
+
+ mock_sync_task.delay.assert_called_once_with("tenant-1", "acct-actor", dataset_id="dataset-1")
+
+ def test_dataset_whitelist_specific_does_not_schedule_member_policy_sync(self, app):
+ with (
+ app.test_request_context(
+ "/workspaces/current/rbac/datasets/dataset-1/whitelist",
+ method="PUT",
+ json={"scope": "specific"},
+ ),
+ patch("controllers.console.workspace.rbac._current_ids", return_value=("tenant-1", "acct-actor")),
+ patch("controllers.console.workspace.rbac.dify_config.RBAC_ENABLED", True),
+ patch(
+ "controllers.console.workspace.rbac.svc.RBACService.DatasetAccess.replace_whitelist",
+ return_value=rbac_mod.svc.ResourceWhitelist(),
+ ),
+ patch("controllers.console.workspace.rbac.initialize_created_app_rbac_access_task") as mock_sync_task,
+ ):
+ inspect.unwrap(rbac_mod.RBACDatasetWhitelistApi.put)(rbac_mod.RBACDatasetWhitelistApi(), "dataset-1")
+
+ mock_sync_task.delay.assert_not_called()
+
def test_app_user_access_policy_assignment_forwards_ids(self, app):
with (
app.test_request_context(