Skip to main content

zaun_community_protocol/permissions/
roles.rs

1//! Role management checks (permissions §4.2, specification §8.4). `cp` is the actor's
2//! effective community permissions; the required `MANAGE_ROLES` bit is checked by the table.
3
4use crate::bits::Permissions;
5use crate::ids::RoleId;
6
7use super::denial::{Denial, Invalid, Refusal};
8use super::hierarchy::{Party, Top, act_on, h3, h4, no_new_unknown};
9use super::input::Role;
10
11/// `role.create`: H3 on the new role's bits. Returns the new role's position; the engine
12/// shifts every role at or above it up by one.
13///
14/// Specification §8.4 and permissions §4.2 place it "directly below the actor's highest
15/// role": the actor's top position
16/// (the actor's role moves up), or `max_position + 1` for the owner. An actor holding no
17/// role has nothing to place it below and is refused (`hierarchy`).
18pub fn role_create(
19    actor: &Party,
20    cp: Permissions,
21    permissions: Permissions,
22    max_position: u32,
23) -> Result<u32, Refusal> {
24    no_new_unknown(Permissions::empty(), permissions)?;
25    h3(actor, permissions, cp)?;
26    match actor.top {
27        Top::Owner => Ok(max_position.saturating_add(1)),
28        Top::Position(0) => Err(Denial::hierarchy().into()),
29        Top::Position(top) => Ok(top),
30    }
31}
32
33/// `role.update`: H4 on the role, H3 on `old XOR new`. `@everyone` may change only its
34/// permissions (`other_fields` is true when the body changes anything else).
35pub fn role_update(
36    actor: &Party,
37    cp: Permissions,
38    role: &Role,
39    new_permissions: Permissions,
40    other_fields: bool,
41) -> Result<(), Refusal> {
42    if role.id.is_everyone() && other_fields {
43        return Err(Invalid::Everyone.into());
44    }
45    h4(actor, role.position)?;
46    no_new_unknown(role.permissions, new_permissions)?;
47    h3(actor, role.permissions ^ new_permissions, cp)?;
48    Ok(())
49}
50
51/// `role.delete`: not `@everyone`; H4.
52pub fn role_delete(actor: &Party, role: &Role) -> Result<(), Refusal> {
53    if role.id.is_everyone() {
54        return Err(Invalid::Everyone.into());
55    }
56    Ok(h4(actor, role.position)?)
57}
58
59/// `role.reorder`: `roles` is the community's role table, `moves` the request's
60/// `(role_id, new_position)` pairs. Every role whose position changes must be below the
61/// actor before and after; positions must stay unique and non-zero; `@everyone` cannot be
62/// listed. The owner may move any other role.
63pub fn role_reorder(actor: &Party, roles: &[Role], moves: &[(RoleId, u32)]) -> Result<(), Refusal> {
64    for (i, (id, to)) in moves.iter().enumerate() {
65        if id.is_everyone() {
66            return Err(Invalid::Everyone.into());
67        }
68        if moves[..i].iter().any(|(other, _)| other == id) {
69            return Err(Invalid::DuplicateRole.into());
70        }
71        if *to == 0 {
72            return Err(Invalid::BadPosition.into());
73        }
74        let role = roles
75            .iter()
76            .find(|r| r.id == *id)
77            .ok_or(Refusal::NotFound)?;
78        if role.position != *to {
79            h4(actor, role.position)?;
80            h4(actor, *to)?;
81        }
82    }
83    let mut positions: Vec<u32> = roles
84        .iter()
85        .filter(|r| !r.id.is_everyone())
86        .map(|r| {
87            moves
88                .iter()
89                .find(|(id, _)| *id == r.id)
90                .map_or(r.position, |(_, to)| *to)
91        })
92        .collect();
93    positions.sort_unstable();
94    if positions.windows(2).any(|w| w[0] == w[1]) {
95        return Err(Invalid::BadPosition.into());
96    }
97    Ok(())
98}
99
100/// `member.roles.update` for one role `role` on `target`: H1; H2 unless self; H4 on the
101/// role; when assigning, `role.permissions ⊆ cp` (H3). `@everyone` is never listed.
102// SPEC: unknown bits of the role grant nothing, so the assignment check ignores them;
103// otherwise no one but the owner could assign a role a newer peer extended.
104pub fn member_role_change(
105    actor: &Party,
106    cp: Permissions,
107    target: &Party,
108    role: &Role,
109    assign: bool,
110) -> Result<(), Refusal> {
111    if role.id.is_everyone() {
112        return Err(Invalid::Everyone.into());
113    }
114    act_on(actor, target, true)?;
115    h4(actor, role.position)?;
116    if assign {
117        h3(actor, role.permissions & Permissions::KNOWN, cp)?;
118    }
119    Ok(())
120}
121
122/// Whether `actor` could assign `role` right now: the check behind invitation default roles
123/// and approval (permissions §4.7, specification §8.6 and §8.9). `cp` must hold
124/// `MANAGE_ROLES` unless the actor is the owner.
125pub fn can_assign(actor: &Party, cp: Permissions, role: &Role) -> Result<(), Denial> {
126    if !actor.is_owner && !cp.contains(Permissions::MANAGE_ROLES) {
127        return Err(Denial::missing(Permissions::MANAGE_ROLES));
128    }
129    can_grant(actor, cp, role)
130}
131
132/// Hierarchy over a role being granted, without `MANAGE_ROLES`: not `@everyone`, H4, and
133/// its known bits held (H3). Used by `membership.approve` (specification §8.9).
134pub fn can_grant(actor: &Party, cp: Permissions, role: &Role) -> Result<(), Denial> {
135    if role.id.is_everyone() {
136        return Err(Denial::hierarchy());
137    }
138    h4(actor, role.position)?;
139    h3(actor, role.permissions & Permissions::KNOWN, cp)
140}