Files
duty-teller/duty_teller/db/__init__.py
Nikolay Tatarinov 4824450088
All checks were successful
CI / lint-and-test (push) Successful in 22s
feat: implement role-based access control for miniapp
- 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.
2026-02-20 23:58:54 +03:00

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)