zaun_community_protocol/permissions/hierarchy.rs
1//! The management hierarchy primitives H1–H4 (permissions §4.1).
2//!
3//! Every check here is additional to the command's required bit (§6) and passes for the owner.
4
5use crate::bits::Permissions;
6use crate::ids::MemberId;
7
8use super::denial::{Denial, Invalid};
9use super::input::Role;
10
11/// A member's top position (§2.1). The owner is above every role.
12#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
13pub enum Top {
14 /// The highest position among the member's roles; 0 with none.
15 Position(u32),
16 /// The owner: +∞.
17 Owner,
18}
19
20impl Top {
21 /// `top(m)`: +∞ for the owner, else the highest role position (0 with no role).
22 #[must_use]
23 pub fn of(is_owner: bool, roles: &[Role]) -> Self {
24 if is_owner {
25 Self::Owner
26 } else {
27 Self::Position(roles.iter().map(|r| r.position).max().unwrap_or(0))
28 }
29 }
30
31 /// Whether a role at `position` is strictly below this top (H4's comparison).
32 #[must_use]
33 pub const fn above(self, position: u32) -> bool {
34 match self {
35 Self::Owner => true,
36 Self::Position(top) => position < top,
37 }
38 }
39}
40
41/// An actor or target member as the hierarchy sees it. Build it with
42/// [`Resolver::party`](super::Resolver::party).
43#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
44pub struct Party {
45 /// The member id.
46 pub id: MemberId,
47 /// Whether this is the owner bound in the current descriptor.
48 pub is_owner: bool,
49 /// Whether the member holds `ADMINISTRATOR` (exempt from H3 only).
50 pub is_admin: bool,
51 /// The member's top position.
52 pub top: Top,
53}
54
55/// H1: nobody acts on the owner, except the owner on themself.
56pub fn h1(actor: &Party, target: &Party) -> Result<(), Denial> {
57 if target.is_owner && !(actor.is_owner && actor.id == target.id) {
58 Err(Denial::hierarchy())
59 } else {
60 Ok(())
61 }
62}
63
64/// H2: acting on another member requires `top(T) < top(A)`; equal positions block and
65/// `ADMINISTRATOR` does not lift it. The owner passes.
66pub fn h2(actor: &Party, target: &Party) -> Result<(), Denial> {
67 if actor.is_owner || target.top < actor.top {
68 Ok(())
69 } else {
70 Err(Denial::hierarchy())
71 }
72}
73
74/// H1 then H2, with H2 skipped when the actor targets themself and `self_ok` is set.
75pub fn act_on(actor: &Party, target: &Party, self_ok: bool) -> Result<(), Denial> {
76 h1(actor, target)?;
77 if self_ok && actor.id == target.id {
78 Ok(())
79 } else {
80 h2(actor, target)
81 }
82}
83
84/// H3: every changed bit must be held (`changed ⊆ held`, `held` being `cp` or `chp`).
85///
86/// The owner and `ADMINISTRATOR` holders are exempt: permissions.md says H3 never blocks an
87/// administrator, and specification §8 exempts them explicitly.
88// permissions §4.1: the exemption holds even when step 7 removed bits from an admin's `cp`.
89pub fn h3(actor: &Party, changed: Permissions, held: Permissions) -> Result<(), Denial> {
90 let missing = changed - (held & Permissions::KNOWN);
91 if actor.is_owner || actor.is_admin || missing.is_empty() {
92 Ok(())
93 } else {
94 Err(Denial::missing(missing))
95 }
96}
97
98/// H3 where only the owner is exempt: restriction masks (`newcomer_restrictions`, a lockdown's
99/// mask), for which permissions.md §4.6 says "owner exempt" and the specification is silent.
100/// So an administrator in probation cannot lift the newcomer mask that binds it (7b).
101pub fn h3_owner_only(actor: &Party, changed: Permissions, held: Permissions) -> Result<(), Denial> {
102 h3(
103 &Party {
104 is_admin: false,
105 ..*actor
106 },
107 changed,
108 held,
109 )
110}
111
112/// H4: a role is manageable only strictly below the actor's top; the owner is exempt.
113pub fn h4(actor: &Party, position: u32) -> Result<(), Denial> {
114 if actor.top.above(position) {
115 Ok(())
116 } else {
117 Err(Denial::hierarchy())
118 }
119}
120
121/// §1.2: a command may not set an unknown bit that `old` did not already hold.
122pub fn no_new_unknown(old: Permissions, new: Permissions) -> Result<(), Invalid> {
123 let unknown = new - Permissions::KNOWN;
124 if (unknown - old).is_empty() {
125 Ok(())
126 } else {
127 Err(Invalid::SetsUnknownBits)
128 }
129}