zaun_community_protocol/permissions/
safety.rs1use crate::bits::Permissions;
4use crate::limits::LOCKDOWN_MS;
5
6use super::denial::{Denial, Invalid, Refusal};
7use super::hierarchy::{Party, Top, h3_owner_only, no_new_unknown};
8use super::lockdown::{Lockdown, LockdownRestrictions};
9
10pub const NEWCOMER_FORBIDDEN: Permissions =
12 Permissions::VIEW_CHANNEL.union(Permissions::READ_MESSAGE_HISTORY);
13
14pub fn validate_newcomer_restrictions(old: Permissions, new: Permissions) -> Result<(), Invalid> {
17 if new.intersects(NEWCOMER_FORBIDDEN) {
18 return Err(Invalid::NewcomerMaskHidesChannels);
19 }
20 no_new_unknown(old, new)
21}
22
23pub fn admission_policy_update(
26 actor: &Party,
27 cp: Permissions,
28 old: Permissions,
29 new: Permissions,
30) -> Result<(), Refusal> {
31 validate_newcomer_restrictions(old, new)?;
32 Ok(h3_owner_only(actor, old ^ new, cp)?)
33}
34
35#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
37pub struct ActiveLockdown {
38 pub enabled_by: Top,
40 pub mask: Permissions,
42}
43
44pub fn lockdown_enable(
53 actor: &Party,
54 held: Permissions,
55 restrictions: LockdownRestrictions,
56 newcomer_restrictions: Permissions,
57 expires_in_ms: u64,
58 active: Option<ActiveLockdown>,
59) -> Result<(), Refusal> {
60 if let Some(active) = active {
61 lockdown_disable(actor, held, active)?;
62 }
63 if !(LOCKDOWN_MS.0..=LOCKDOWN_MS.1).contains(&expires_in_ms) {
64 return Err(Invalid::Duration.into());
65 }
66 let mask = Lockdown::max_mask(restrictions, newcomer_restrictions);
67 Ok(h3_owner_only(actor, mask, held)?)
68}
69
70pub fn lockdown_disable(
74 actor: &Party,
75 held: Permissions,
76 active: ActiveLockdown,
77) -> Result<(), Refusal> {
78 if !actor.is_owner && actor.top < active.enabled_by {
79 return Err(Denial::hierarchy().into());
80 }
81 Ok(h3_owner_only(actor, active.mask, held)?)
82}