Skip to content

ServerKit is a lightweight, modern server control panel for managing web applications, databases, and services on your VPS or dedicated server. Built with Python/Flask backend and React frontend.

Compute visible workspace ids in one place

Juan Deniscc8c74e1ef9e
diff --git a/backend/app/api/_access.py b/backend/app/api/_access.py
index b700bc17..e0e54209 100644
--- a/backend/app/api/_access.py
+++ b/backend/app/api/_access.py
@@ -1,6 +1,9 @@
"""Per-app access for routes: the one way a view loads "the app this caller
may touch". Raises the typed 404/403 the global error handler renders."""
+from flask import request
+
from app.services.resource_grant_service import ResourceGrantService
+from app.services.workspace_service import WorkspaceService
from app.middleware.rbac import get_current_user
@@ -11,3 +14,11 @@ def require_app(app_id, *, write=False, conceal=False):
"""
return ResourceGrantService.get_app_for(
get_current_user(), app_id, write=write, conceal=conceal)
+
+
+def visible_workspace_ids(user):
+ """Workspace ids the caller may see, honouring the active workspace
+ context (``X-Workspace-Id`` header or ``workspace_id`` query param)."""
+ requested = request.headers.get('X-Workspace-Id') or request.args.get('workspace_id')
+ return WorkspaceService.visible_workspace_ids(
+ user, WorkspaceService.resolve_workspace_id(user, requested))
diff --git a/backend/app/api/environments.py b/backend/app/api/environments.py
index e7d2ccaf..61d25c66 100644
--- a/backend/app/api/environments.py
+++ b/backend/app/api/environments.py
@@ -10,28 +10,18 @@ from app.services.project_service import ProjectService
from app.services.workspace_service import WorkspaceService
from app.middleware.rbac import get_current_user
from app.exceptions import permission_denied
+from app.api._access import visible_workspace_ids
environments_bp = Blueprint('environments', __name__)
-def _accessible_workspace_ids(user):
- ws_id = WorkspaceService.resolve_workspace_id(
- user, request.headers.get('X-Workspace-Id') or request.args.get('workspace_id'))
- if ws_id is not None:
- return [ws_id]
- if user.is_admin:
- from app.models.workspace import Workspace
- return [w.id for w in Workspace.query.all()]
- return [w.id for w in WorkspaceService.list_workspaces(user_id=user.id)]
-
-
def _project_for_write(user, project_id):
"""Return (project, error_response). error_response is a (json, status) tuple
when the project can't be written by this user, else None."""
project = ProjectService.get_project(project_id)
if not project:
return None, (jsonify({'error': 'Project not found'}), 404)
- if not user.is_admin and project.workspace_id not in set(_accessible_workspace_ids(user)):
+ if not user.is_admin and project.workspace_id not in set(visible_workspace_ids(user)):
raise permission_denied()
if not WorkspaceService.can_write_in_workspace(user, project.workspace_id):
return None, (jsonify({'error': 'You have read-only access to this workspace'}), 403)
diff --git a/backend/app/api/projects.py b/backend/app/api/projects.py
index d0d08646..e04ff03e 100644
--- a/backend/app/api/projects.py
+++ b/backend/app/api/projects.py
@@ -14,6 +14,7 @@ from app.services.project_service import ProjectService
from app.services.workspace_service import WorkspaceService
from app.middleware.rbac import get_current_user
from app.exceptions import permission_denied
+from app.api._access import visible_workspace_ids
projects_bp = Blueprint('projects', __name__)
@@ -28,24 +29,10 @@ def _active_workspace_id(user):
return ws_id
-def _accessible_workspace_ids(user):
- """Workspace ids the caller may see projects for. With an explicit, valid
- workspace context, just that one; otherwise all the user's workspaces (admins
- see all)."""
- ws_id = WorkspaceService.resolve_workspace_id(
- user, request.headers.get('X-Workspace-Id') or request.args.get('workspace_id'))
- if ws_id is not None:
- return [ws_id]
- if user.is_admin:
- from app.models.workspace import Workspace
- return [w.id for w in Workspace.query.all()]
- return [w.id for w in WorkspaceService.list_workspaces(user_id=user.id)]
-
-
def _can_view_project(user, project):
if user.is_admin:
return True
- return project.workspace_id in set(_accessible_workspace_ids(user))
+ return project.workspace_id in set(visible_workspace_ids(user))
@projects_bp.route('', methods=['GET'])
@@ -54,7 +41,7 @@ def _can_view_project(user, project):
def list_projects():
"""List projects in the accessible workspace(s), each with resource counts."""
user = get_current_user()
- ws_ids = _accessible_workspace_ids(user)
+ ws_ids = visible_workspace_ids(user)
projects = []
for ws_id in ws_ids:
projects.extend(ProjectService.list_projects(ws_id))
diff --git a/backend/app/services/search_service.py b/backend/app/services/search_service.py
index 62adecb6..c81edeac 100644
--- a/backend/app/services/search_service.py
+++ b/backend/app/services/search_service.py
@@ -207,7 +207,7 @@ class SearchService:
logger.exception('search: server fan-out failed')
# --- project / environment (access derives from Workspace membership) ---
- accessible_workspace_ids = SearchService._accessible_workspace_ids(user, ws_id)
+ accessible_workspace_ids = WorkspaceService.visible_workspace_ids(user, ws_id)
try:
if (not includes('project') or required_capabilities
or environment_id is not None):
@@ -520,14 +520,3 @@ class SearchService:
def _server_capabilities(server):
raw = server.cached_capabilities if isinstance(server.cached_capabilities, dict) else {}
return {str(name) for name, enabled in raw.items() if enabled is True}
-
- @staticmethod
- def _accessible_workspace_ids(user, workspace_id):
- if workspace_id is not None:
- return [workspace_id]
- from app.services.workspace_service import WorkspaceService
- if getattr(user, 'is_admin', False):
- from app.models.workspace import Workspace
- return [workspace.id for workspace in Workspace.query.all()]
- return [workspace.id for workspace in
- WorkspaceService.list_workspaces(user_id=user.id)]
diff --git a/backend/app/services/workspace_service.py b/backend/app/services/workspace_service.py
index 4956c0c0..40b7505b 100644
--- a/backend/app/services/workspace_service.py
+++ b/backend/app/services/workspace_service.py
@@ -40,6 +40,18 @@ class WorkspaceService:
db.session.commit()
return ws
+ @staticmethod
+ def visible_workspace_ids(user, workspace_id=None):
+ """Workspace ids whose resources ``user`` may see: just ``workspace_id``
+ when a context is active, otherwise every workspace the user belongs
+ to (all of them for an admin)."""
+ if workspace_id is not None:
+ return [workspace_id]
+ if user.is_admin:
+ from app.models.workspace import Workspace
+ return [w.id for w in Workspace.query.all()]
+ return [w.id for w in WorkspaceService.list_workspaces(user_id=user.id)]
+
@staticmethod
def resolve_workspace_id(user, requested):
"""Resolve an optionally-requested workspace context to a usable workspace