astrbot_plugin_moviepilot/access_policy.py

101 行
3.5 KiB
Python

2026-07-20 22:17:04 +08:00
"""Platform-aware access policy for MoviePilot commands."""
from __future__ import annotations
from dataclasses import dataclass
from typing import Any, Mapping
@dataclass(frozen=True, slots=True)
class PlatformIdentity:
"""Normalized identity extracted from an AstrBot message event."""
platform_name: str
platform_id: str
user_id: str
group_id: str
session_origin: str
@property
def is_private(self) -> bool:
return not self.group_id
def _string(value: Any) -> str:
return str(value).strip() if value is not None else ""
def identity_from_event(event: Any) -> PlatformIdentity:
"""Extract a platform-neutral identity from an AstrMessageEvent-like object."""
return PlatformIdentity(
platform_name=_string(event.get_platform_name()),
platform_id=_string(event.get_platform_id()),
user_id=_string(event.get_sender_id()),
group_id=_string(event.get_group_id()),
session_origin=_string(event.unified_msg_origin),
)
def _normalized_ids(values: Any) -> set[str]:
if not isinstance(values, (list, tuple, set)):
return set()
return {_string(value) for value in values if _string(value)}
class AccessPolicy:
"""Whitelist policy for private and group MoviePilot operations."""
def __init__(self, config: Mapping[str, Any]) -> None:
self._allowed_platforms = _normalized_ids(
config.get("allowed_platforms", ["aiocqhttp", "telegram"])
)
self._private_user_ids = _normalized_ids(config.get("private_user_ids", []))
self._group_permissions = self._parse_group_permissions(
config.get("group_permissions", [])
)
@staticmethod
def _parse_group_permissions(value: Any) -> dict[str, set[str]]:
permissions: dict[str, set[str]] = {}
if isinstance(value, Mapping):
for group_id, user_ids in value.items():
normalized_group = _string(group_id)
if normalized_group:
permissions[normalized_group] = _normalized_ids(user_ids)
return permissions
if not isinstance(value, (list, tuple)):
return permissions
for item in value:
if not isinstance(item, Mapping):
continue
group_id = _string(item.get("group_id"))
if group_id:
permissions[group_id] = _normalized_ids(item.get("user_ids", []))
return permissions
def is_allowed(self, event: Any) -> bool:
"""Return whether an event may operate MoviePilot."""
identity = identity_from_event(event)
if (
not identity.platform_name
or identity.platform_name not in self._allowed_platforms
):
2026-07-20 22:17:04 +08:00
return False
if not identity.user_id:
return False
if identity.is_private:
return identity.user_id in self._private_user_ids
return (
identity.group_id in self._group_permissions
and identity.user_id in self._group_permissions[identity.group_id]
)
2026-07-20 22:17:04 +08:00
def is_allowed_identity(self, identity: PlatformIdentity) -> bool:
"""Return whether a normalized identity may operate MoviePilot."""
if identity.platform_name not in self._allowed_platforms or not identity.user_id:
return False
if identity.is_private:
return identity.user_id in self._private_user_ids
return identity.user_id in self._group_permissions.get(identity.group_id, set())