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

37 lines
1.1 KiB
Python
Raw Normal View History

import logging
logger = logging.getLogger(__name__)
class ConfluenceCommentPolicy:
VALID_POLICIES = frozenset({"open", "pairing", "allowlist", "disabled"})
def __init__(self, policy: str = "open", allow_from: list[str] | None = None):
self._policy = policy if policy in self.VALID_POLICIES else "open"
self._allowlist: set[str] = set(allow_from or [])
def is_allowed(self, author_id: str) -> bool:
if self._policy == "disabled":
return False
if self._policy == "open":
return True
if self._policy == "allowlist":
if not self._allowlist:
logger.warning("Comment policy is 'allowlist' but allowlist is empty")
return False
return author_id in self._allowlist
return True
def add_to_allowlist(self, account_id: str):
self._allowlist.add(account_id)
def remove_from_allowlist(self, account_id: str):
self._allowlist.discard(account_id)
@property
def policy(self) -> str:
return self._policy
@property
def allowlist(self) -> frozenset[str]:
return frozenset(self._allowlist)