ForcePilot/backend/package/yuxi/channel/extensions/pinduoduo/security.py

63 lines
2.1 KiB
Python
Raw Normal View History

from __future__ import annotations
import logging
from yuxi.channel.extensions.pinduoduo.types import PddDmPolicy, PinduoduoAccount
logger = logging.getLogger(__name__)
class PinduoduoSecurity:
def __init__(self, account: PinduoduoAccount):
self._account = account
self._policy = PddDmPolicy(account.dm_policy)
self._allowlist: set[str] = set()
self._paired: set[str] = set()
@property
def policy(self) -> PddDmPolicy:
return self._policy
def resolve_dm_policy(self) -> str:
return self._policy.value
def check_dm_access(self, buyer_id: str) -> tuple[bool, str | None]:
policy = self._resolve_effective_policy(buyer_id)
if policy == "open":
return True, None
if policy == "disabled":
return False, "DM 功能已禁用"
if policy == "pairing":
return False, "请先输入配对码(发送 #pair XXXXXX"
if policy == "blocked":
return False, "您不在白名单中,无法访问"
return False, "未知策略"
def _resolve_effective_policy(self, buyer_id: str) -> str:
normalized = buyer_id.strip().lower()
if self._policy == PddDmPolicy.OPEN:
return "open"
if self._policy == PddDmPolicy.DISABLED:
return "disabled"
if self._policy == PddDmPolicy.PAIRING:
if normalized in self._paired:
return "open"
return "pairing"
if self._policy == PddDmPolicy.ALLOWLIST:
if normalized in self._allowlist:
return "open"
return "blocked"
return "blocked"
def mark_paired(self, buyer_id: str) -> None:
self._paired.add(buyer_id.strip().lower())
def add_to_allowlist(self, buyer_id: str) -> None:
self._allowlist.add(buyer_id.strip().lower())
def remove_from_allowlist(self, buyer_id: str) -> None:
self._allowlist.discard(buyer_id.strip().lower())
def is_in_allowlist(self, buyer_id: str) -> bool:
return buyer_id.strip().lower() in self._allowlist