fix(auth): rename seeded permission groups to Viewer and Editor

Every install showed "Viewer (legacy)" and "Editor (legacy)" as the built-in
groups, which reads as deprecated. 0092 already shipped in 5.3.0, so a new
revision renames the untouched system rows and drops the "pre-upgrade"
framing from their descriptions. It skips a group whose target name an admin
already took, and keeps a description the admin edited.

Fixes #4678

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Xdb9FnfhEqP6ectkSZ2T3F
This commit is contained in:
Sam Dornan
2026-09-25 00:36:34 +00:00
committed by Claude
parent a63d67f539
commit 86fa809612
8 changed files with 151 additions and 15 deletions

View File

@@ -10,7 +10,7 @@ Two accounts, matching the two sides of every permission assertion:
* ``e2e_admin`` — role admin, so `useCan` short-circuits to true and every
gated affordance must be present.
* ``e2e_viewer`` — the seeded "Viewer (legacy)" group, i.e. library read plus
* ``e2e_viewer`` — the seeded "Viewer" group, i.e. library read plus
own collections/assets. Every ROM write affordance must be absent.
Never point this at a real library: it writes users with a known password.
@@ -32,7 +32,7 @@ from handler.auth import auth_handler # noqa: E402
from handler.database import db_permission_handler, db_user_handler # noqa: E402
from models.user import Role, User # noqa: E402
VIEWER_GROUP_NAME = "Viewer (legacy)"
VIEWER_GROUP_NAME = "Viewer"
E2E_ADMIN_USERNAME = "e2e_admin"
E2E_VIEWER_USERNAME = "e2e_viewer"

View File

@@ -0,0 +1,83 @@
"""Rename the seeded permission groups to Viewer and Editor
0092 seeded them as "Viewer (legacy)" and "Editor (legacy)", which reads as
deprecated even though they are the built-in roles. Only the untouched system
rows are renamed: a group an admin already renamed keeps its name, a clash
with an existing "Viewer"/"Editor" skips the rename, and a description the
admin edited is kept.
Revision ID: 0135_rename_system_groups
Revises: 0134_gallery_sort_indexes
Create Date: 2026-09-25 00:00:00.000000
"""
import sqlalchemy as sa
from alembic import op # type: ignore[attr-defined]
# revision identifiers, used by Alembic.
revision = "0135_rename_system_groups"
down_revision = "0134_gallery_sort_indexes"
branch_labels = None
depends_on = None
# (old name, new name, old description, new description)
RENAMES = (
(
"Viewer (legacy)",
"Viewer",
"Read the library; manage only your own collections, "
"assets and devices. Reproduces the pre-upgrade default user.",
"Read the library; manage only your own collections, assets and devices.",
),
(
"Editor (legacy)",
"Editor",
"Viewer access plus library-wide create/edit/delete of "
"roms, platforms and firmware. Reproduces the pre-upgrade "
"editor role.",
"Viewer access plus library-wide create/edit/delete of "
"roms, platforms and firmware.",
),
)
groups_t = sa.table(
"permission_groups",
sa.column("id", sa.Integer),
sa.column("name", sa.String),
sa.column("description", sa.String),
sa.column("is_system", sa.Boolean),
)
def _rename(from_name: str, to_name: str, from_desc: str, to_desc: str) -> None:
conn = op.get_bind()
group_id = conn.execute(
sa.select(groups_t.c.id).where(
groups_t.c.name == from_name, groups_t.c.is_system.is_(True)
)
).scalar()
if group_id is None:
return
# `name` is unique, so an admin-created group already holding it wins.
if conn.execute(sa.select(groups_t.c.id).where(groups_t.c.name == to_name)).first():
return
conn.execute(
groups_t.update().where(groups_t.c.id == group_id).values(name=to_name)
)
conn.execute(
groups_t.update()
.where(groups_t.c.id == group_id, groups_t.c.description == from_desc)
.values(description=to_desc)
)
def upgrade() -> None:
for old_name, new_name, old_desc, new_desc in RENAMES:
_rename(old_name, new_name, old_desc, new_desc)
def downgrade() -> None:
for old_name, new_name, old_desc, new_desc in RENAMES:
_rename(new_name, old_name, new_desc, old_desc)

View File

@@ -74,7 +74,7 @@ _GRANT_SCOPES: dict[tuple[PermEntity, PermAction], frozenset[Scope]] = {
}
# --- Legacy group matrices ----------------------------------------------------
# "Viewer (legacy)" == today's default user (WRITE_SCOPES): read the library;
# "Viewer" == today's default user (WRITE_SCOPES): read the library;
# create/modify/delete only OWN collections/assets/devices.
LEGACY_VIEWER_GRANTS: tuple[Grant, ...] = (
(PermEntity.ROMS, PermAction.READ, False),
@@ -94,7 +94,7 @@ LEGACY_VIEWER_GRANTS: tuple[Grant, ...] = (
(PermEntity.DEVICES, PermAction.DELETE, True),
)
# "Editor (legacy)" == EDIT_SCOPES: viewer + library-wide write AND delete of
# "Editor" == EDIT_SCOPES: viewer + library-wide write AND delete of
# roms/platforms/firmware. Delete is True here because today's delete endpoints
# gate on *_WRITE -- editors can already delete, so preserving that is required
# to avoid silently revoking access on upgrade.

View File

@@ -65,7 +65,7 @@ class PermissionGroup(BaseModel):
A group carries a read/write/delete matrix over entity types (its
``grants``). Exactly one group is the server-wide default (``is_default``)
applied to new users. ``is_system`` marks the auto-created legacy groups so
applied to new users. ``is_system`` marks the seeded Viewer and Editor groups so
the admin UI can warn before editing/deleting them.
"""

View File

@@ -482,7 +482,7 @@ def admin_user():
@pytest.fixture
def editor_user():
# role collapses to `user`; editor-level access now comes from the group.
group = db_permission_handler.get_group_by_name("Editor (legacy)")
group = db_permission_handler.get_group_by_name("Editor")
user = User(
username="test_editor",
hashed_password=_password_hash("test_editor_password"),
@@ -494,7 +494,7 @@ def editor_user():
@pytest.fixture
def viewer_user():
group = db_permission_handler.get_group_by_name("Viewer (legacy)")
group = db_permission_handler.get_group_by_name("Viewer")
user = User(
username="test_viewer",
hashed_password=_password_hash("test_viewer_password"),

View File

@@ -24,7 +24,7 @@ from models.user import Role, User
def test_viewer_matrix_projects_to_write_scopes():
projected = set(grants_to_scopes(LEGACY_VIEWER_GRANTS))
assert projected == set(WRITE_SCOPES)
# A group-less user falls back to the default (Viewer legacy) group.
# A group-less user falls back to the default Viewer group.
assert projected == set(User(role=Role.USER).oauth_scopes)
@@ -33,8 +33,8 @@ def test_editor_matrix_projects_to_edit_scopes():
projected = set(grants_to_scopes(LEGACY_EDITOR_GRANTS))
assert projected == set(EDIT_SCOPES)
# Editor-level access now comes from the Editor (legacy) group.
editor_group = db_permission_handler.get_group_by_name("Editor (legacy)")
# Editor-level access now comes from the Editor group.
editor_group = db_permission_handler.get_group_by_name("Editor")
assert editor_group is not None
assert projected == set(
User(role=Role.USER, permission_group_id=editor_group.id).oauth_scopes
@@ -75,14 +75,14 @@ def test_migration_seeded_legacy_groups():
groups = {
g.name: g
for g in s.query(PermissionGroup)
.filter(PermissionGroup.name.in_(["Viewer (legacy)", "Editor (legacy)"]))
.filter(PermissionGroup.name.in_(["Viewer", "Editor"]))
.all()
}
assert set(groups) == {"Viewer (legacy)", "Editor (legacy)"}
assert set(groups) == {"Viewer", "Editor"}
viewer = groups["Viewer (legacy)"]
editor = groups["Editor (legacy)"]
viewer = groups["Viewer"]
editor = groups["Editor"]
# Viewer is the server-wide default; both are system groups.
assert viewer.is_default is True

View File

@@ -215,4 +215,4 @@ def test_default_group_is_viewer_legacy():
group = db_permission_handler.get_default_group()
assert group is not None
assert group.name == "Viewer (legacy)"
assert group.name == "Viewer"

View File

@@ -598,3 +598,56 @@ def test_the_state_disc_file_migration_resumes_an_interrupted_run(drop_column: b
assert "disc_file_id" in columns
assert index
assert "fk_states_disc_file_id" in foreign_keys
def _system_groups(connection: sa.Connection) -> dict[str, str]:
rows = connection.execute(
sa.text("SELECT name, description FROM permission_groups WHERE is_system")
)
return {name: description for name, description in rows}
def _replay_group_rename(connection: sa.Connection, direction: str) -> None:
migration = _load_migration("0135_rename_system_groups.py")
with Operations.context(MigrationContext.configure(connection)):
getattr(migration, direction)()
def test_the_group_rename_round_trips_the_seeded_groups():
with sync_engine.connect() as connection, connection.begin() as transaction:
renamed = _system_groups(connection)
_replay_group_rename(connection, "downgrade")
legacy = _system_groups(connection)
_replay_group_rename(connection, "upgrade")
replayed = _system_groups(connection)
transaction.rollback()
assert set(renamed) == {"Viewer", "Editor"}
assert set(legacy) == {"Viewer (legacy)", "Editor (legacy)"}
assert "pre-upgrade" in legacy["Viewer (legacy)"]
assert replayed == renamed
def test_the_group_rename_leaves_admin_changes_alone():
"""A taken name skips that group, and an edited description survives."""
with sync_engine.connect() as connection, connection.begin() as transaction:
_replay_group_rename(connection, "downgrade")
connection.execute(
sa.text(
"INSERT INTO permission_groups "
"(name, description, is_default, is_system, created_at, updated_at) "
"VALUES ('Viewer', '', false, false, "
"CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)"
)
)
connection.execute(
sa.text(
"UPDATE permission_groups SET description = 'Custom' "
"WHERE name = 'Editor (legacy)'"
)
)
_replay_group_rename(connection, "upgrade")
groups = _system_groups(connection)
transaction.rollback()
assert groups == {"Viewer (legacy)": groups["Viewer (legacy)"], "Editor": "Custom"}