diff --git a/backend/route_registry.py b/backend/route_registry.py index a2f51e2..c3ed164 100644 --- a/backend/route_registry.py +++ b/backend/route_registry.py @@ -24,6 +24,7 @@ class RouteSpec: ROUTES: tuple[RouteSpec, ...] = ( RouteSpec("GET", "ui/bootstrap", "backend.routes.ui_routes.get_ui_bootstrap", "read"), + RouteSpec("GET", "ui/actions", "backend.routes.ui_routes.get_ui_actions", "read"), RouteSpec("GET", "dashboard/summary", "backend.routes.dashboard_routes.get_dashboard_summary", "read"), RouteSpec("GET", "profiles", "backend.routes.profile_routes.list_profiles", "read"), RouteSpec("POST", "profiles", "backend.routes.profile_routes.create_profile", "write"), diff --git a/backend/routes/ui_routes.py b/backend/routes/ui_routes.py index 75dab70..1cd41b0 100644 --- a/backend/routes/ui_routes.py +++ b/backend/routes/ui_routes.py @@ -16,3 +16,13 @@ def get_ui_bootstrap(ctx: RequestContext, service: UiService | None = None) -> d return ui_service.get_bootstrap(ctx) return handle_request(ctx, action) + + +def get_ui_actions(ctx: RequestContext, service: UiService | None = None) -> dict: + ui_service = service or UiService() + + def action() -> list[dict]: + require_authenticated(ctx) + return ui_service.get_actions(ctx) + + return handle_request(ctx, action) diff --git a/backend/services/ui_service.py b/backend/services/ui_service.py index 4e4ab70..d69371b 100644 --- a/backend/services/ui_service.py +++ b/backend/services/ui_service.py @@ -4,6 +4,7 @@ from __future__ import annotations from core.config_service import ConfigService from core.identity import RequestContext +from core.interactions import build_default_actions, filter_actions_for_permissions API_BASE = "/api/v1/plugins/extensions/astrbot_plugin_imagestudio" @@ -30,3 +31,7 @@ class UiService: "feature_flags": config.feature_flags(), "api_base": API_BASE, } + + def get_actions(self, ctx: RequestContext) -> list[dict]: + actions = build_default_actions(api_base=API_BASE) + return [action.to_dict() for action in filter_actions_for_permissions(actions, ctx.permissions)] diff --git a/core/interactions.py b/core/interactions.py index 7d0f7dd..b287b2a 100644 --- a/core/interactions.py +++ b/core/interactions.py @@ -40,9 +40,9 @@ class InteractionAction: def build_default_actions(*, api_base: str = "") -> list[InteractionAction]: return [ InteractionAction("open_workbench", "Workbench", "open_workbench", {"url": f"{api_base}/web"}), - InteractionAction("quick_profile", "Quick profile", "quick_profile", requires_permission="write"), + InteractionAction("quick_profile", "Quick profile", "quick_profile", requires_permission="can_write"), InteractionAction("open_logs", "Logs", "open_logs", {"view": "logs"}), - InteractionAction("open_settings", "Settings", "open_settings", {"view": "settings"}, "config"), + InteractionAction("open_settings", "Settings", "open_settings", {"view": "settings"}, "can_manage_config"), ] diff --git a/tests/test_core_modules.py b/tests/test_core_modules.py index cac9cd1..4d69e22 100644 --- a/tests/test_core_modules.py +++ b/tests/test_core_modules.py @@ -140,7 +140,7 @@ class InteractionTests(unittest.TestCase): self.assertEqual(action.action_type, "open_logs") actions = build_default_actions(api_base="/api/plugin") - filtered = filter_actions_for_permissions(actions, {"write"}) + filtered = filter_actions_for_permissions(actions, {"can_write"}) self.assertIn("quick_profile", [item.action_id for item in filtered]) self.assertNotIn("open_settings", [item.action_id for item in filtered]) diff --git a/tests/test_route_registry.py b/tests/test_route_registry.py index 4633b41..2183356 100644 --- a/tests/test_route_registry.py +++ b/tests/test_route_registry.py @@ -13,6 +13,7 @@ class RouteRegistryTests(unittest.TestCase): keys = {(route["method"], route["endpoint"]) for route in routes} self.assertIn(("GET", "ui/bootstrap"), keys) + self.assertIn(("GET", "ui/actions"), keys) self.assertIn(("POST", "nodes/{node_id}/generate"), keys) self.assertIn(("GET", "security/audit-logs"), keys) self.assertIn(("POST", "api-discovery/probe-provider"), keys) diff --git a/tests/test_s2_routes.py b/tests/test_s2_routes.py index 126a2ca..8ab8f91 100644 --- a/tests/test_s2_routes.py +++ b/tests/test_s2_routes.py @@ -51,7 +51,7 @@ from backend.routes.settings_routes import ( validate_settings, ) from backend.routes.storage_routes import get_storage_status -from backend.routes.ui_routes import get_ui_bootstrap +from backend.routes.ui_routes import get_ui_actions, get_ui_bootstrap from backend.routes.web_routes import get_web_manifest from backend.routes.workflow_routes import ( archive_workflow, @@ -180,6 +180,23 @@ storage: "/api/v1/plugins/extensions/astrbot_plugin_imagestudio", ) + def test_ui_actions_are_filtered_by_permissions(self): + response = get_ui_actions(self.ctx, UiService(self.config_service)) + + self.assertTrue(response["ok"]) + action_ids = [item["action_id"] for item in response["data"]] + self.assertIn("open_workbench", action_ids) + self.assertIn("quick_profile", action_ids) + self.assertIn("open_settings", action_ids) + + read_only = RequestContext.from_astrbot(username="viewer", user_id="viewer_001") + read_only_response = get_ui_actions(read_only, UiService(self.config_service)) + self.assertTrue(read_only_response["ok"]) + read_only_action_ids = [item["action_id"] for item in read_only_response["data"]] + self.assertIn("open_logs", read_only_action_ids) + self.assertNotIn("quick_profile", read_only_action_ids) + self.assertNotIn("open_settings", read_only_action_ids) + def test_ui_bootstrap_requires_authentication(self): anonymous = RequestContext()