All checks were successful
CI / lint-and-test (push) Successful in 22s
- Introduced a new roles table in the database to manage user roles ('user' and 'admin') for access control.
- Updated the user model to include a foreign key reference to the roles table, allowing for role assignment.
- Enhanced command handlers to support the `/set_role` command for admins to assign roles to users.
- Refactored access control logic to utilize role checks instead of username/phone allowlists, improving security and maintainability.
- Updated documentation to reflect changes in access control mechanisms and role management.
- Added unit tests to ensure correct functionality of role assignment and access checks.
62 lines
1.3 KiB
Python
62 lines
1.3 KiB
Python
"""Database layer: SQLAlchemy models, Pydantic schemas, repository, init."""
|
|
|
|
from duty_teller.db.models import Base, User, Duty, Role
|
|
from duty_teller.db.schemas import (
|
|
UserCreate,
|
|
UserInDb,
|
|
DutyCreate,
|
|
DutyInDb,
|
|
DutyWithUser,
|
|
)
|
|
from duty_teller.db.session import (
|
|
get_engine,
|
|
get_session_factory,
|
|
get_session,
|
|
session_scope,
|
|
)
|
|
from duty_teller.db.repository import (
|
|
delete_duties_in_range,
|
|
get_or_create_user,
|
|
get_or_create_user_by_full_name,
|
|
get_duties,
|
|
insert_duty,
|
|
set_user_phone,
|
|
update_user_display_name,
|
|
)
|
|
|
|
__all__ = [
|
|
"Base",
|
|
"User",
|
|
"Duty",
|
|
"Role",
|
|
"UserCreate",
|
|
"UserInDb",
|
|
"DutyCreate",
|
|
"DutyInDb",
|
|
"DutyWithUser",
|
|
"get_engine",
|
|
"get_session_factory",
|
|
"get_session",
|
|
"session_scope",
|
|
"delete_duties_in_range",
|
|
"get_or_create_user",
|
|
"get_or_create_user_by_full_name",
|
|
"get_duties",
|
|
"insert_duty",
|
|
"set_user_phone",
|
|
"update_user_display_name",
|
|
"init_db",
|
|
]
|
|
|
|
|
|
def init_db(database_url: str) -> None:
|
|
"""Create all tables from SQLAlchemy metadata.
|
|
|
|
Prefer Alembic migrations for schema changes in production.
|
|
|
|
Args:
|
|
database_url: SQLAlchemy database URL.
|
|
"""
|
|
engine = get_engine(database_url)
|
|
Base.metadata.create_all(bind=engine)
|