acl.py 869 B

123456789101112131415161718192021222324252627
  1. from aiogram import types
  2. from aiogram.dispatcher.handler import CancelHandler
  3. from aiogram.dispatcher.middlewares import BaseMiddleware
  4. from app import config
  5. from app.models.user import User
  6. class ACLMiddleware(BaseMiddleware):
  7. async def setup_chat(self, data: dict, user: types.User, message: types.Message):
  8. user_id = user.id
  9. user = await User.get(user_id)
  10. if user is None:
  11. user = await User.create(user_id=user_id)
  12. if user.is_blocked:
  13. await message.answer(
  14. f"Вы заблокированы. По всем вопросам пишите {config.ADMIN_USERNAME}"
  15. )
  16. raise CancelHandler()
  17. data["user"] = user
  18. async def on_pre_process_message(self, message: types.Message, data: dict):
  19. await self.setup_chat(data, message.from_user, message)