From 0e43ac6b8d24fca1d12600e91479d6c3e10ebf40 Mon Sep 17 00:00:00 2001 From: "benjamin.baker" Date: Mon, 7 Sep 2026 12:39:24 -0400 Subject: [PATCH] Add prebuilt user scopes and guard invalid Reporter grants A newly created user held no roles and no permissions, so the sidebar filtered every nav item away and the account appeared broken. Adds three prebuilt scopes (Reporter, Operator, Technician) derived from the live permission catalog by verb rather than hand-listed, so a new plugin permission lands in the right scope automatically instead of silently going ungranted. users.view is granted to all three since a user who cannot see the directory cannot collaborate; users.create/edit/delete stay admin-only. Role.seedscopes() is additive and idempotent, seeded via flask seed permissions, and preserves custom grants on rerun. Reporter guard: the auth model is additive across admin, direct grants and roles, so Reporter cannot be expressed as merely fewer grants - a direct endpoint-action grant would silently re-arm a read-only account. Rather than change the global permission predicate and alter every route's semantics, _check_scope_grants() rejects the invalid combination at write time on both create and update. The update path validates the effective post-update state, so demoting a Technician to Reporter is refused while a stale endpoint grant remains rather than quietly leaving a read-only user with write authority. Validation runs before the User object is constructed. Assigning user.roles first put the relationship into the session autoflush path while the User was still unattached; nothing broke because the refusal returns before db.session.add(), but it left a trap where any query added below that point would autoflush a half-built user and surface later as an orphan row. Verified with -W error::sqlalchemy.exc.SAWarning: 25 passed. --- frontend/src/views/settings/UsersList.vue | 220 +++++++++++++++- migrations/versions/7d43_userpermissions.py | 59 +++++ shopdb/cli/__init__.py | 14 +- shopdb/core/api/users.py | 158 +++++++++++- shopdb/core/models/__init__.py | 9 +- shopdb/core/models/user.py | 259 ++++++++++++++++++- tests/test_core/test_apitokens.py | 56 ++++ tests/test_core/test_user_scopes.py | 267 ++++++++++++++++++++ tests/test_core/test_users_api.py | 82 ++++++ 9 files changed, 1107 insertions(+), 17 deletions(-) create mode 100644 migrations/versions/7d43_userpermissions.py create mode 100644 tests/test_core/test_user_scopes.py diff --git a/frontend/src/views/settings/UsersList.vue b/frontend/src/views/settings/UsersList.vue index 242506da..0f81f6d9 100644 --- a/frontend/src/views/settings/UsersList.vue +++ b/frontend/src/views/settings/UsersList.vue @@ -154,15 +154,36 @@ >
- + + +

{{ scopeDescription }}

+
+
+ +

{{ endpointActionsHint }}

-
@@ -306,7 +327,7 @@