ForcePilot/backend/package/yuxi/channel/extensions/zalouser/security.py
Kris d5e36d33b7 feat(channel): 添加 Zalo OA、Zoom Chat 和 Zulip 渠道扩展
新增 Zalo OA、Zoom Chat、Zulip 三个渠道扩展。

Zalo OA 渠道扩展主要模块:sidecar_client, config, gateway, outbound, streaming, pairing, security, auth, dedupe, directory, monitor, status, session, reactions, tools

Zoom Chat 渠道扩展主要模块:config, gateway, webhook, outbound, streaming, pairing, security, crypto, dedupe, actions, media, mentions, monitor, status, session, reactions, threading

Zulip 渠道扩展主要模块:client, config, gateway, outbound, streaming, pairing, security, monitor, status
2026-05-21 12:06:26 +08:00

88 lines
3.3 KiB
Python

from __future__ import annotations
import logging
logger = logging.getLogger(__name__)
class ZaloUserSecurityAdapter:
def __init__(self):
self._allowlist: dict[str, set] = {}
self._dm_policy: str = "pairing"
self._group_policy: str = "allowlist"
self._allow_from: list[str] = []
self._group_allow_from: list[str] = []
self._groups: dict = {}
self._require_mention: bool = True
self._dangerously_allow_name_matching: bool = False
def set_account_config(self, account: dict) -> None:
self._dm_policy = account.get("dm_policy", "pairing")
self._group_policy = account.get("group_policy", "allowlist")
self._allow_from = account.get("allow_from", [])
self._group_allow_from = account.get("group_allow_from", [])
self._groups = account.get("groups", {})
self._dangerously_allow_name_matching = account.get("dangerously_allow_name_matching", False)
async def check_allowlist(self, peer_id: str, channel_type: str) -> bool:
if self._dm_policy == "open":
return True
if not self._allow_from:
return False
return peer_id in self._allow_from
def resolve_dm_policy(self) -> dict:
return {"mode": self._dm_policy, "allow_from": self._allow_from}
def resolve_group_policy(self) -> dict:
return {
"mode": self._group_policy,
"groups": self._groups,
"group_allow_from": self._group_allow_from,
}
def is_group_allowed(self, group_id: str) -> bool:
policy = self._group_policy
if policy == "disabled":
return False
if policy == "open":
return True
if not self._groups:
return False
if group_id in self._groups:
entry = self._groups[group_id]
if isinstance(entry, bool):
return entry
if isinstance(entry, dict):
return entry.get("enabled", True)
return bool(entry)
if "*" in self._groups:
entry = self._groups["*"]
if isinstance(entry, bool):
return entry
if isinstance(entry, dict):
return entry.get("enabled", True)
return False
def is_sender_allowed_in_group(self, sender_id: str) -> bool:
if not self._group_allow_from:
return True
return sender_id in self._group_allow_from
def group_require_mention(self, group_id: str) -> bool:
if group_id in self._groups:
entry = self._groups[group_id]
if isinstance(entry, dict):
return entry.get("require_mention", self._require_mention)
return self._require_mention
def collect_warnings(self, config: dict, account_id: str | None = None, account: dict | None = None) -> list[str]:
warnings: list[str] = []
if account:
if account.get("dm_policy") == "open":
warnings.append(
"DM policy is 'open' - anyone can message. Consider setting to 'pairing' or 'allowlist'."
)
if account.get("dangerously_allow_name_matching"):
warnings.append("dangerously_allow_name_matching is enabled - group names can be spoofed.")
return warnings