51 lines
1.7 KiB
Python
51 lines
1.7 KiB
Python
from dataclasses import dataclass, field
|
|
from typing import Dict, List, Set
|
|
|
|
|
|
@dataclass
|
|
class PermissionProfile:
|
|
name: str
|
|
users: List[int] = field(default_factory=list)
|
|
chats: List[int] = field(default_factory=list)
|
|
|
|
|
|
class PermissionManager:
|
|
def __init__(self) -> None:
|
|
self._profiles: Dict[str, PermissionProfile] = {}
|
|
self._allowed: Set[int] = set()
|
|
self._blocked: Set[int] = set()
|
|
self._sudo: Set[int] = set()
|
|
|
|
def add_profile(self, profile: PermissionProfile) -> None:
|
|
self._profiles[profile.name] = profile
|
|
|
|
def load_from_config(self, config: Dict[str, List[int]]) -> None:
|
|
self._allowed = set(config.get("allowed_users", []) or [])
|
|
self._blocked = set(config.get("blocked_users", []) or [])
|
|
self._sudo = set(config.get("sudo_users", []) or [])
|
|
|
|
def is_allowed(self, permission: str, user_id: int, chat_id: int) -> bool:
|
|
permission = permission.lower()
|
|
if user_id in self._blocked:
|
|
return False
|
|
if permission == "core":
|
|
return False
|
|
if permission == "admin":
|
|
return user_id in self._sudo
|
|
if permission == "trusted":
|
|
return user_id in self._sudo or user_id in self._allowed
|
|
if permission == "sandbox":
|
|
return False
|
|
profile = self._profiles.get(permission)
|
|
if profile is None:
|
|
if permission != "user":
|
|
return False
|
|
if self._allowed:
|
|
return user_id in self._allowed or user_id in self._sudo
|
|
return True
|
|
if user_id in profile.users:
|
|
return True
|
|
if chat_id in profile.chats:
|
|
return True
|
|
return False
|