Implement phone number normalization and access control for Telegram users

- Added functionality to normalize phone numbers for comparison, ensuring only digits are stored and checked.
- Updated configuration to include optional phone number allowlists for users and admins in the environment settings.
- Enhanced authentication logic to allow access based on normalized phone numbers, in addition to usernames.
- Introduced new helper functions for parsing and validating phone numbers, improving code organization and maintainability.
- Added unit tests to validate phone normalization and access control based on phone numbers.
This commit is contained in:
2026-02-18 16:11:44 +03:00
parent d0d22c150a
commit 59ba2a9ca4
10 changed files with 344 additions and 48 deletions

View File

@@ -7,6 +7,11 @@ from sqlalchemy.orm import Session
from duty_teller.db.models import User, Duty, GroupDutyPin
def get_user_by_telegram_id(session: Session, telegram_user_id: int) -> User | None:
"""Find user by telegram_user_id. Returns None if not found (no creation)."""
return session.query(User).filter(User.telegram_user_id == telegram_user_id).first()
def get_or_create_user(
session: Session,
telegram_user_id: int,
@@ -15,7 +20,7 @@ def get_or_create_user(
first_name: str | None = None,
last_name: str | None = None,
) -> User:
user = session.query(User).filter(User.telegram_user_id == telegram_user_id).first()
user = get_user_by_telegram_id(session, telegram_user_id)
if user:
user.full_name = full_name
user.username = username