mirror of
https://github.com/rommapp/romm.git
synced 2026-06-28 06:46:00 +00:00
- 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.
127 lines
3.6 KiB
Python
127 lines
3.6 KiB
Python
from collections.abc import Sequence
|
|
|
|
from sqlalchemy import and_, delete, select, update
|
|
from sqlalchemy.orm import QueryableAttribute, Session, load_only
|
|
|
|
from decorators.database import begin_session
|
|
from models.firmware import Firmware
|
|
|
|
from .base_handler import DBBaseHandler
|
|
|
|
|
|
class DBFirmwareHandler(DBBaseHandler):
|
|
@begin_session
|
|
def add_firmware(
|
|
self,
|
|
firmware: Firmware,
|
|
session: Session = None, # type: ignore
|
|
) -> Firmware:
|
|
return session.merge(firmware)
|
|
|
|
@begin_session
|
|
def get_firmware(
|
|
self,
|
|
id: int,
|
|
*,
|
|
session: Session = None, # type: ignore
|
|
) -> Firmware | None:
|
|
return session.scalar(select(Firmware).filter_by(id=id).limit(1))
|
|
|
|
@begin_session
|
|
def list_firmware(
|
|
self,
|
|
*,
|
|
platform_id: int | None = None,
|
|
only_fields: Sequence[QueryableAttribute] | None = None,
|
|
hidden_platform_ids: Sequence[int] | None = None,
|
|
session: Session = None, # type: ignore
|
|
) -> Sequence[Firmware]:
|
|
query = select(Firmware).order_by(Firmware.file_name.asc())
|
|
|
|
if platform_id:
|
|
query = query.filter_by(platform_id=platform_id)
|
|
|
|
# Firmware inherits its platform's visibility: hide firmware whose
|
|
# platform an admin has hidden from the caller.
|
|
if hidden_platform_ids:
|
|
query = query.filter(Firmware.platform_id.not_in(hidden_platform_ids))
|
|
|
|
if only_fields:
|
|
query = query.options(load_only(*only_fields))
|
|
|
|
return session.scalars(query).all()
|
|
|
|
@begin_session
|
|
def get_firmware_by_filename(
|
|
self,
|
|
platform_id: int,
|
|
file_name: str,
|
|
session: Session = None, # type: ignore
|
|
):
|
|
return session.scalar(
|
|
select(Firmware)
|
|
.filter_by(platform_id=platform_id, file_name=file_name)
|
|
.limit(1)
|
|
)
|
|
|
|
@begin_session
|
|
def update_firmware(
|
|
self,
|
|
id: int,
|
|
data: dict,
|
|
session: Session = None, # type: ignore
|
|
) -> Firmware:
|
|
session.execute(
|
|
update(Firmware)
|
|
.where(Firmware.id == id)
|
|
.values(**data)
|
|
.execution_options(synchronize_session="evaluate")
|
|
)
|
|
return session.query(Firmware).filter_by(id=id).one()
|
|
|
|
@begin_session
|
|
def delete_firmware(
|
|
self,
|
|
id: int,
|
|
session: Session = None, # type: ignore
|
|
) -> None:
|
|
session.execute(
|
|
delete(Firmware)
|
|
.where(Firmware.id == id)
|
|
.execution_options(synchronize_session="evaluate")
|
|
)
|
|
|
|
@begin_session
|
|
def mark_missing_firmware(
|
|
self,
|
|
platform_id: int,
|
|
fs_firmwares_to_keep: list[str],
|
|
session: Session = None, # type: ignore
|
|
) -> Sequence[Firmware]:
|
|
missing_firmware = (
|
|
session.scalars(
|
|
select(Firmware)
|
|
.order_by(Firmware.file_name.asc())
|
|
.where(
|
|
and_(
|
|
Firmware.platform_id == platform_id,
|
|
Firmware.file_name.not_in(fs_firmwares_to_keep),
|
|
)
|
|
)
|
|
) # type: ignore[attr-defined]
|
|
.unique()
|
|
.all()
|
|
)
|
|
session.execute(
|
|
update(Firmware)
|
|
.where(
|
|
and_(
|
|
Firmware.platform_id == platform_id,
|
|
Firmware.file_name.not_in(fs_firmwares_to_keep),
|
|
)
|
|
)
|
|
.values(**{"missing_from_fs": True})
|
|
.execution_options(synchronize_session="evaluate")
|
|
)
|
|
return missing_firmware
|