Files
romm/backend/handler/database/platforms_handler.py
zurdi 649ddce2aa feat: Implement fine-grained permission checks and admin CRUD for permission groups
- Added new permission handling in `backend/handler/auth/dependencies.py` to support fine-grained, DB-backed permission checks.
- Enhanced user role update logic in `backend/endpoints/user.py` to prevent demotion of the last admin.
- Introduced `hidden_platform_ids` and `hidden_rom_ids` parameters in various database handlers to manage visibility based on admin settings.
- Created new endpoints for managing permission groups, user memberships, and hidden entities in `backend/tests/endpoints/test_permissions_admin.py`.
- Added tests for permissions visibility and CRUD operations in `backend/tests/endpoints/test_permissions_visibility.py` and `backend/tests/endpoints/test_permissions_me.py`.
- Updated archive handling in `backend/utils/archives.py` to improve error logging and timeout management during extraction.
2026-06-24 08:45:45 +00:00

162 lines
4.9 KiB
Python

import functools
from collections.abc import Sequence
from datetime import datetime
from sqlalchemy import delete, or_, select, update
from sqlalchemy.orm import Query, QueryableAttribute, Session, load_only, selectinload
from decorators.database import begin_session
from models.platform import Platform
from models.rom import Rom
from .base_handler import DBBaseHandler
def with_firmware(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
kwargs["query"] = select(Platform).options(
selectinload(Platform.firmware),
)
return func(*args, **kwargs)
return wrapper
class DBPlatformsHandler(DBBaseHandler):
@begin_session
@with_firmware
def add_platform(
self,
platform: Platform,
query: Query = None, # type: ignore
session: Session = None, # type: ignore
) -> Platform:
platform = session.merge(platform)
session.flush()
return session.scalar(query.filter_by(id=platform.id).limit(1))
@begin_session
def update_platform(
self,
id: int,
data: dict,
session: Session = None, # type: ignore
) -> Platform:
session.execute(
update(Platform)
.where(Platform.id == id)
.values(**data)
.execution_options(synchronize_session="evaluate")
)
return session.query(Platform).filter_by(id=id).one()
@begin_session
@with_firmware
def get_platform(
self,
id: int,
query: Query = None, # type: ignore
session: Session = None, # type: ignore
) -> Platform | None:
return session.scalar(query.filter_by(id=id).limit(1))
@begin_session
@with_firmware
def get_platforms(
self,
updated_after: datetime | None = None,
only_fields: Sequence[QueryableAttribute] | None = None,
hidden_platform_ids: Sequence[int] | None = None,
query: Query = None, # type: ignore
session: Session = None, # type: ignore
) -> Sequence[Platform]:
if updated_after:
query = query.filter(Platform.updated_at > updated_after)
# Admin-driven visibility (opt-out): exclude platforms hidden from the
# caller. Empty/None (e.g. admins) skips filtering.
if hidden_platform_ids:
query = query.filter(Platform.id.not_in(hidden_platform_ids))
if only_fields:
query = query.options(load_only(*only_fields))
return session.scalars(query.order_by(Platform.name.asc())).unique().all()
@begin_session
@with_firmware
def get_platform_by_fs_slug(
self,
fs_slug: str,
query: Query = None, # type: ignore
session: Session = None, # type: ignore
) -> Platform | None:
return session.scalar(query.filter_by(fs_slug=fs_slug).limit(1))
@begin_session
@with_firmware
def get_platform_by_slug(
self,
slug: str,
query: Query = None, # type: ignore
session: Session = None, # type: ignore
) -> Platform | None:
return session.scalar(query.filter_by(slug=slug).limit(1))
@begin_session
def delete_platform(
self,
id: int,
session: Session = None, # type: ignore
) -> None:
# Remove all roms from that platforms first
session.execute(
delete(Rom)
.where(Rom.platform_id == id)
.execution_options(synchronize_session="evaluate")
)
session.execute(
delete(Platform)
.where(Platform.id == id)
.execution_options(synchronize_session="evaluate")
)
@begin_session
def mark_missing_platforms(
self,
fs_platforms_to_keep: list[str],
query: Query = None, # type: ignore
session: Session = None, # type: ignore
) -> Sequence[Platform]:
missing_platforms = (
session.scalars(
select(Platform)
.order_by(Platform.name.asc())
.where(
or_(
Platform.fs_slug.not_in(fs_platforms_to_keep),
Platform.slug.is_(None),
)
)
) # type: ignore[attr-defined]
.unique()
.all()
)
session.execute(
update(Platform)
.where(or_(Platform.fs_slug.not_in(fs_platforms_to_keep), Platform.slug.is_(None))) # type: ignore[attr-defined]
.values(**{"missing_from_fs": True})
.execution_options(synchronize_session="fetch")
)
session.execute(
update(Rom)
.where(Rom.platform_id.in_([p.id for p in missing_platforms])) # type: ignore[attr-defined]
.values(**{"missing_from_fs": True})
.execution_options(synchronize_session="fetch")
)
return missing_platforms