Skip to main content

zaun_community_protocol/permissions/
overrides.rs

1//! Override validation and override-edit checks (permissions §1.2, §2.2, §2.4, §4.4).
2
3use crate::bits::Permissions;
4
5use super::denial::{Invalid, Refusal};
6use super::hierarchy::{Party, act_on, h3, h4, no_new_unknown};
7use super::input::{Override, OverrideTarget};
8
9/// What the host does with a validated override.
10#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
11pub enum OverrideWrite {
12    /// Store the override.
13    Store,
14    /// `allow == 0 && deny == 0`: delete the override (tri-state `Inherit`).
15    Delete,
16}
17
18/// Validates a new override value against the stored one (`old`, if any):
19/// `allow & deny == 0`; no known bit outside `CHANNEL_SCOPED`; no unknown bit that `old`
20/// did not already hold. Unknown bits already present pass through unchanged.
21pub fn validate_override(
22    old: Option<(Permissions, Permissions)>,
23    allow: Permissions,
24    deny: Permissions,
25) -> Result<OverrideWrite, Invalid> {
26    if allow.intersects(deny) {
27        return Err(Invalid::AllowDenyOverlap);
28    }
29    // Known bits only: bitflags' `!` would truncate to known bits, so use a difference.
30    if ((allow | deny) & Permissions::KNOWN).intersects(Permissions::COMMUNITY_SCOPED) {
31        return Err(Invalid::NotChannelScoped);
32    }
33    let (old_allow, old_deny) = old.unwrap_or_default();
34    no_new_unknown(old_allow, allow)?;
35    no_new_unknown(old_deny, deny)?;
36    if allow.is_empty() && deny.is_empty() {
37        Ok(OverrideWrite::Delete)
38    } else {
39        Ok(OverrideWrite::Store)
40    }
41}
42
43/// The target of an override as the hierarchy sees it.
44#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
45pub enum OverrideSubject {
46    /// A role at this position (`@everyone` is 0).
47    Role(u32),
48    /// A member.
49    Member(Party),
50}
51
52/// One override edit in `channel.permissions.update` (or `channel.create` with initial
53/// overrides). `chp` is the actor's effective permissions in that channel; `old`/`new` are
54/// `(allow, deny)` with `(0, 0)` for none. A role target needs H4; a member target H1 and H2
55/// unless self. H3: the changed bits must be in `chp` (owner and administrator exempt).
56// Both rules (permissions §4.4): H3 on the changed bits, and specification §8.2's "may only
57// allow or deny bits it has" on the known bits of the new value.
58pub fn override_edit(
59    actor: &Party,
60    chp: Permissions,
61    subject: OverrideSubject,
62    old: (Permissions, Permissions),
63    new: (Permissions, Permissions),
64) -> Result<(), Refusal> {
65    match subject {
66        OverrideSubject::Role(position) => h4(actor, position)?,
67        OverrideSubject::Member(target) => act_on(actor, &target, true)?,
68    }
69    let changed = (old.0 ^ new.0) | (old.1 ^ new.1);
70    h3(actor, changed, chp)?;
71    h3(actor, (new.0 | new.1) & Permissions::KNOWN, chp)?;
72    Ok(())
73}
74
75/// Setting `sync_with_parent = true` (§2.4): checked as an override edit from the channel's
76/// own overrides to the parent's, for every target in either. `subject` maps a target to
77/// its hierarchy view; `None` (a deleted role or member) is skipped.
78pub fn adopt_parent(
79    actor: &Party,
80    chp: Permissions,
81    own: &[Override],
82    parent: &[Override],
83    subject: impl Fn(&OverrideTarget) -> Option<OverrideSubject>,
84) -> Result<(), Refusal> {
85    let value = |set: &[Override], t: &OverrideTarget| {
86        set.iter()
87            .filter(|o| o.target == *t)
88            .fold((Permissions::empty(), Permissions::empty()), |(a, d), o| {
89                (a | o.allow, d | o.deny)
90            })
91    };
92    for o in own.iter().chain(parent) {
93        let (old, new) = (value(own, &o.target), value(parent, &o.target));
94        if old == new {
95            continue;
96        }
97        if let Some(s) = subject(&o.target) {
98            override_edit(actor, chp, s, old, new)?;
99        }
100    }
101    Ok(())
102}