50 lines
1.3 KiB
Python
50 lines
1.3 KiB
Python
"""
|
|
Access control middleware
|
|
"""
|
|
from pyrogram import Client
|
|
from pyrogram.handlers import MessageHandler, CallbackQueryHandler
|
|
from bot.modules.access_control.auth import is_authorized
|
|
import logging
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
async def access_middleware(client: Client, update, *args, **kwargs):
|
|
"""
|
|
Middleware for checking bot access
|
|
|
|
Args:
|
|
client: Pyrogram client
|
|
update: Telegram update
|
|
"""
|
|
user_id = None
|
|
|
|
if hasattr(update, 'from_user') and update.from_user:
|
|
user_id = update.from_user.id
|
|
elif hasattr(update, 'message') and update.message and update.message.from_user:
|
|
user_id = update.message.from_user.id
|
|
|
|
if not user_id:
|
|
return False
|
|
|
|
# Check authorization
|
|
if not await is_authorized(user_id):
|
|
logger.warning(f"Unauthorized user access attempt: {user_id}")
|
|
if hasattr(update, 'message') and update.message:
|
|
await update.message.reply("❌ You don't have access to this bot")
|
|
return False
|
|
|
|
return True
|
|
|
|
|
|
def setup_middleware(app: Client):
|
|
"""
|
|
Setup middleware for application
|
|
|
|
Args:
|
|
app: Pyrogram client
|
|
"""
|
|
# Middleware will be applied via decorators in handlers
|
|
logger.info("Access control middleware configured")
|
|
|