Files
quickbot/middleware/telegram/auth.py
Alexander Kalinovsky 6dbe0536ca init
2025-01-04 12:00:12 +01:00

40 lines
1.4 KiB
Python

from aiogram import BaseMiddleware
from aiogram.types import TelegramObject, Message, CallbackQuery
from aiogram.utils.i18n import gettext as _
from babel.support import LazyProxy
from typing import Any, Awaitable, Callable, Dict
from ...model.user import UserBase
class AuthMiddleware(BaseMiddleware):
def __init__[UserType: UserBase](self,
user_class: type[UserType],
not_authenticated_msg: LazyProxy | str = "not authenticated",
not_active_msg: LazyProxy | str = "not active"):
self.user_class = user_class
self.not_authenticated_msg = not_authenticated_msg
self.not_active_msg = not_active_msg
async def __call__(self,
handler: Callable[[TelegramObject, Dict[str, Any]], Awaitable[Any]],
event: TelegramObject,
data: Dict[str, Any]) -> Any:
user = await self.user_class.get(id = event.from_user.id, session = data["db_session"])
if user and user.is_active:
data["user"] = user
return await handler(event, data)
if type(event) in [Message, CallbackQuery]:
if user and not user.is_active:
return await event.answer(self.not_active_msg)
return await event.answer(self.not_authenticated_msg)