Files
tg_loader/bot/modules/access_control/permissions.py
2025-12-04 00:12:56 +03:00

56 lines
1.7 KiB
Python

"""
Access permissions system
"""
from enum import Enum
from typing import Callable, Awaitable
from pyrogram import Client
from pyrogram.types import Message, CallbackQuery
from bot.modules.access_control.auth import is_authorized, is_admin, is_owner
import logging
logger = logging.getLogger(__name__)
class Permission(Enum):
"""Access permission types"""
USER = "user" # Regular user
ADMIN = "admin" # Administrator
OWNER = "owner" # Owner
def require_permission(permission: Permission):
"""
Decorator for checking access permissions
Args:
permission: Required access level
"""
def decorator(func: Callable):
async def wrapper(client: Client, message: Message, *args, **kwargs):
user_id = message.from_user.id if message.from_user else None
if not user_id:
await message.reply("❌ Failed to identify user")
return
# Check authorization
if not await is_authorized(user_id):
await message.reply("❌ You don't have access to this bot")
return
# Check permissions
if permission == Permission.OWNER:
if not await is_owner(user_id):
await message.reply("❌ This command is only available to owner")
return
elif permission == Permission.ADMIN:
if not await is_admin(user_id):
await message.reply("❌ This command is only available to administrators")
return
return await func(client, message, *args, **kwargs)
return wrapper
return decorator