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.

Move the new auth lookups behind the model/service

Juan Denis4e45ecb5f522
diff --git a/backend/app/api/apps.py b/backend/app/api/apps.py
index a18d860d..be84c816 100644
--- a/backend/app/api/apps.py
+++ b/backend/app/api/apps.py
@@ -942,7 +942,7 @@ def create_app():
if app_type not in valid_types:
return jsonify({'error': f'Invalid app_type. Must be one of: {", ".join(valid_types)}'}), 400
- clash = app_name_clash_error(Application.query.filter_by(name=name).first(), name)
+ clash = Application.name_clash_error(name)
if clash:
raise ValidationError(clash)
@@ -1333,8 +1333,7 @@ def update_app(app_id):
if 'name' in data and data['name'] != app.name:
if not data['name'] or _service_slug(data['name']) != data['name']:
raise ValidationError('Service name must be lowercase letters, digits and hyphens')
- clash = app_name_clash_error(
- Application.query.filter_by(name=data['name']).first(), data['name'])
+ clash = Application.name_clash_error(data['name'])
if clash:
raise ValidationError(clash)
if 'deploy_settings' in data:
diff --git a/backend/app/api/servers.py b/backend/app/api/servers.py
index 67fddd7b..32713033 100644
--- a/backend/app/api/servers.py
+++ b/backend/app/api/servers.py
@@ -86,8 +86,8 @@ def _get_operable_server(server_id):
"""The server, if the current user may operate it; 404 otherwise, so a
non-member can't tell a foreign workspace's server from a missing one."""
from app.services.workspace_service import WorkspaceService
- server = Server.query.get(server_id)
- if not server or not WorkspaceService.can_access_server(get_current_user(), server):
+ server = WorkspaceService.get_accessible_server(get_current_user(), server_id)
+ if server is None:
raise not_found('server')
return server
diff --git a/backend/app/models/application.py b/backend/app/models/application.py
index bd2f0986..0e54904e 100644
--- a/backend/app/models/application.py
+++ b/backend/app/models/application.py
@@ -179,6 +179,13 @@ class Application(JsonColumnMixin, TimestampMixin, SoftDeleteMixin, db.Model):
"""
return {row.id for row in cls.query_deleted().with_entities(cls.id).all()}
+ @classmethod
+ def name_clash_error(cls, name, suffix=''):
+ """Message when *name* is taken — tombstones included, so a restore can
+ never collide — or ``None`` when it is free."""
+ from app.utils.slug import app_name_clash_error
+ return app_name_clash_error(cls.query.filter_by(name=name).first(), name, suffix)
+
@property
def live_domains(self):
"""The domains this app actually serves — tombstones excluded.
diff --git a/backend/app/services/workspace_service.py b/backend/app/services/workspace_service.py
index ed72fd50..4956c0c0 100644
--- a/backend/app/services/workspace_service.py
+++ b/backend/app/services/workspace_service.py
@@ -147,6 +147,15 @@ class WorkspaceService:
return True
return WorkspaceService.get_user_role(server.workspace_id, user.id) is not None
+ @staticmethod
+ def get_accessible_server(user, server_id):
+ """The server if *user* may operate it (see can_access_server), else None."""
+ from app.models.server import Server
+ server = Server.query.get(server_id)
+ if server is None or not WorkspaceService.can_access_server(user, server):
+ return None
+ return server
+
@staticmethod
def check_quota(workspace_id, current_count, quota_field):
"""Return an error string if a workspace quota would be exceeded,
diff --git a/backend/tests/api_controller_boundary_baseline.json b/backend/tests/api_controller_boundary_baseline.json
index 2f7af738..2767fd32 100644
--- a/backend/tests/api_controller_boundary_baseline.json
+++ b/backend/tests/api_controller_boundary_baseline.json
@@ -3,7 +3,7 @@
"policy": "Exact reviewed inventory of direct persistence, subprocess, and filesystem mutation calls in Flask API controllers. New calls are forbidden; remove entries as controllers delegate to services.",
"counts": {
"filesystem": 8,
- "persistence": 483
+ "persistence": 480
},
"owners": {
"api/admin.py": {
@@ -259,7 +259,6 @@
"api/apps.py::apply_image_update::persistence::Application.query_active().filter_by(id=app_id).first()::1",
"api/apps.py::apply_image_update::persistence::User.query.get(current_user_id)::1",
"api/apps.py::apply_image_update::persistence::db.session.commit()::1",
- "api/apps.py::create_app::persistence::User.query.get(current_user_id)::1",
"api/apps.py::create_app::persistence::db.session.add(app)::1",
"api/apps.py::create_app::persistence::db.session.commit()::1",
"api/apps.py::create_app_db_snapshot::persistence::db.session.add(snapshot)::1",
@@ -618,7 +617,6 @@
"api/servers.py::list_agent_versions::persistence::AgentVersion.query.order_by(AgentVersion.version.desc()).all()::1",
"api/servers.py::list_groups::persistence::ServerGroup.query.all()::1",
"api/servers.py::ping_server::persistence::Server.query.get(server_id)::1",
- "api/servers.py::regenerate_token::persistence::Server.query.get(server_id)::1",
"api/servers.py::regenerate_token::persistence::db.session.commit()::1",
"api/servers.py::register_agent::persistence::Server.query.filter(Server.registration_token_hash.isnot(None)).all()::1",
"api/servers.py::register_agent::persistence::db.session.commit()::1",
@@ -627,7 +625,6 @@
"api/servers.py::rotate_api_key::persistence::Server.query.get(server_id)::1",
"api/servers.py::rotate_api_key::persistence::db.session.commit()::1",
"api/servers.py::set_server_workspace::persistence::Server.query.filter_by(workspace_id=ws.id).count()::1",
- "api/servers.py::set_server_workspace::persistence::Server.query.get(server_id)::1",
"api/servers.py::set_server_workspace::persistence::Workspace.query.get(target)::1",
"api/servers.py::set_server_workspace::persistence::db.session.commit()::1",
"api/servers.py::start_server_onboarding::persistence::Server.query.get(server_id)::1",
diff --git a/docs/MIGRATION_INVENTORY.md b/docs/MIGRATION_INVENTORY.md
index 95cde31a..b2464064 100644
--- a/docs/MIGRATION_INVENTORY.md
+++ b/docs/MIGRATION_INVENTORY.md
@@ -18,7 +18,7 @@ debt. Regenerate with `python scripts/generate-migration-inventory.py`.
| API crashes swallowed without recording | app.error_reporting | 0 | 0 | INVARIANT at 0 |
| Hand-shaped {'error': ...} bodies in app/api | typed errors + the global handler | 850 | 850 | migrate when touched; new endpoints raise |
| Raw subprocess calls outside the runners | app/utils/system.py runners | 23 | 23 | migrate when touched |
-| Controller-boundary violations (routes doing service work) | service layer extraction | 491 | 491 | migrate when touched (first-wave ratchet) |
+| Controller-boundary violations (routes doing service work) | service layer extraction | 488 | 488 | migrate when touched (first-wave ratchet) |
| raw api.* calls in pages/ | E1: useServerQuery/useServerMutation | 396 | 398 | migrate when touched |
| per-page toast.error extractions in pages/ | E1: query-layer error presentation | 200 | 206 | migrate when touched |
| hand-rolled form-group blocks | F2: FormField/useForm | 320 | 321 | migrate when touched |