Skip to main content

zaun_community_protocol/permissions/
invites.rs

1//! Invitations without escalation (permissions §4.7, specification §8.6 and §8.9).
2
3use crate::bits::Permissions;
4use crate::ids::RoleId;
5use crate::model::MembershipState;
6
7use super::denial::{Denial, DenialReason, Invalid, Refusal};
8use super::hierarchy::Party;
9use super::input::Role;
10use super::roles::can_assign;
11
12/// The admission policy's `invite_creation` rule (specification §8.9).
13// `trusted` (governance.md §5, P1) is not a v1 value.
14#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
15pub enum InviteCreation {
16    /// Anyone with `CREATE_INVITE`, `Probation` members included.
17    Permission,
18    /// `Active` members with `CREATE_INVITE`.
19    Active,
20    /// Only members with `MANAGE_INVITES` (and `CREATE_INVITE`).
21    Moderators,
22}
23
24impl InviteCreation {
25    /// Parses the wire tag.
26    #[must_use]
27    pub fn from_wire(tag: &str) -> Option<Self> {
28        match tag {
29            "permission" => Some(Self::Permission),
30            "active" => Some(Self::Active),
31            "moderators" => Some(Self::Moderators),
32            _ => None,
33        }
34    }
35}
36
37/// The parts of an `invite.create` body the checks need.
38#[derive(Debug, Clone, Copy)]
39pub struct InviteRequest<'a> {
40    /// The policy's `invite_creation` rule.
41    pub rule: InviteCreation,
42    /// The roles named by `default_role_ids`, joined from the role table.
43    pub default_roles: &'a [Role],
44    /// `requires_approval`.
45    pub requires_approval: bool,
46    /// `pre_approved`.
47    pub pre_approved: bool,
48    /// Whether `admission_overrides` (if any) are all at least as strict as the policy;
49    /// the engine compares the fields.
50    pub overrides_stricter: bool,
51}
52
53/// `invite.create` beyond `CREATE_INVITE` itself (checked by the table in the right scope):
54/// the `invite_creation` rule, default roles the actor could assign, stricter-only admission
55/// overrides, and `pre_approved` needing `APPROVE_MEMBERS` and excluding `requires_approval`.
56pub fn invite_create(
57    actor: &Party,
58    state: MembershipState,
59    cp: Permissions,
60    req: &InviteRequest<'_>,
61) -> Result<(), Refusal> {
62    match req.rule {
63        InviteCreation::Permission => {}
64        // SPEC: no reason is specified for a Probation member refused by the `active` rule;
65        // `probation` is the cause, so it is reported.
66        InviteCreation::Active if !actor.is_owner && state != MembershipState::Active => {
67            return Err(Denial {
68                missing: Permissions::empty(),
69                reason: Some(DenialReason::Probation),
70            }
71            .into());
72        }
73        InviteCreation::Active => {}
74        InviteCreation::Moderators
75            if !actor.is_owner && !cp.contains(Permissions::MANAGE_INVITES) =>
76        {
77            return Err(Denial::missing(Permissions::MANAGE_INVITES).into());
78        }
79        InviteCreation::Moderators => {}
80    }
81    if req.pre_approved && req.requires_approval {
82        return Err(Invalid::PreApprovedWithApproval.into());
83    }
84    if !req.overrides_stricter {
85        return Err(Invalid::LooserAdmission.into());
86    }
87    if req.pre_approved && !actor.is_owner && !cp.contains(Permissions::APPROVE_MEMBERS) {
88        return Err(Denial::missing(Permissions::APPROVE_MEMBERS).into());
89    }
90    for role in req.default_roles {
91        if role.id.is_everyone() {
92            return Err(Invalid::Everyone.into());
93        }
94        can_assign(actor, cp, role)?;
95    }
96    Ok(())
97}
98
99/// At redemption, the default roles the issuer could still assign now; the rest are
100/// dropped, never granted (permissions §4.7, specification §8.6). A deleted role is simply
101/// absent from `roles`. An issuer who left or was banned resolves to an empty `cp`.
102// The invitation itself stays valid (permissions §4.7, governance.md §6).
103pub fn redeemable_roles<'r>(
104    issuer: &'r Party,
105    issuer_cp: Permissions,
106    roles: &'r [Role],
107) -> impl Iterator<Item = RoleId> + 'r {
108    roles
109        .iter()
110        .filter(move |r| can_assign(issuer, issuer_cp, r).is_ok())
111        .map(|r| r.id)
112}