weirdnet-collab 0.1.0

Collaboration control for Weirdnet apps: project members and roles, signed allow and deny rules, owner consent grants, a decision with a reason chain, invites and a signed, mergeable audit log. App-agnostic: the app defines what a project, role and permission are.
Documentation
//! Role presets and gates, as data the app supplies.
use crate::ids::{Permission, RoleId};
use serde::{Deserialize, Serialize};
use std::collections::{BTreeMap, BTreeSet};

/// A gate a permission needs on top of the rules, which the host cannot grant or waive.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum Gate {
    /// The audio's owner must have granted the caller consent for this permission (once or
    /// always, for all their streams or one). The owner needs no consent for their own audio.
    OwnerConsent,
    /// A switch only the person's own device can check (their mic switch, the OS permission).
    /// The decision is allowed and its chain says which switch to check.
    Local(String),
}

/// What one role gets.
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct RoleSpec {
    /// Every permission (the host). Gates still apply.
    pub all: bool,
    /// The permissions the role has by default.
    pub permissions: BTreeSet<Permission>,
    /// The roles a holder may put on an invite or link they create (needs `session.invite`).
    pub may_invite_as: BTreeSet<RoleId>,
}

/// The role table: presets and gates for one app.
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct RoleTable {
    host: Option<RoleId>,
    roles: BTreeMap<RoleId, RoleSpec>,
    gates: BTreeMap<Permission, Gate>,
    default_role: Option<RoleId>,
}

impl RoleTable {
    /// An empty table.
    pub fn new() -> Self {
        Self::default()
    }

    /// Define a role. The first role marked `host` is the project owner's role.
    pub fn role(mut self, id: &str, spec: RoleSpec) -> Self {
        self.roles.insert(RoleId::new(id), spec);
        self
    }

    /// Say which role the owner of a project has.
    pub fn with_host(mut self, id: &str) -> Self {
        self.host = Some(RoleId::new(id));
        self
    }

    /// Say which role an invitee gets when the invite names none.
    pub fn with_default_role(mut self, id: &str) -> Self {
        self.default_role = Some(RoleId::new(id));
        self
    }

    /// Put a gate on a permission.
    pub fn gate(mut self, permission: &str, gate: Gate) -> Self {
        self.gates.insert(Permission::new(permission), gate);
        self
    }

    /// The owner's role.
    pub fn host_role(&self) -> Option<&RoleId> {
        self.host.as_ref()
    }

    /// The role an invitee gets by default.
    pub fn default_role(&self) -> Option<&RoleId> {
        self.default_role.as_ref()
    }

    /// The spec for `role`.
    pub fn spec(&self, role: &RoleId) -> Option<&RoleSpec> {
        self.roles.get(role)
    }

    /// Whether `role` exists.
    pub fn knows(&self, role: &RoleId) -> bool {
        self.roles.contains_key(role)
    }

    /// Whether `role` has `perm` by default.
    pub fn grants(&self, role: &RoleId, perm: &Permission) -> bool {
        self.roles.get(role).is_some_and(|r| r.all || r.permissions.contains(perm))
    }

    /// The gate on `perm`, if any.
    pub fn gate_of(&self, perm: &Permission) -> Option<&Gate> {
        self.gates.get(perm)
    }

    /// Whether a holder of `creator` may put `target` on an invite.
    pub fn may_invite_as(&self, creator: &RoleId, target: &RoleId) -> bool {
        self.roles.get(creator).is_some_and(|r| r.may_invite_as.contains(target))
    }

    /// Every role id.
    pub fn role_ids(&self) -> impl Iterator<Item = &RoleId> {
        self.roles.keys()
    }

    /// Every permission any role names (the host's `all` adds none), for views.
    pub fn catalog(&self) -> BTreeSet<Permission> {
        self.roles.values().flat_map(|r| r.permissions.iter().cloned()).chain(self.gates.keys().cloned()).collect()
    }
}

impl RoleSpec {
    /// A role with these permissions.
    pub fn with(perms: &[&str]) -> Self {
        RoleSpec { all: false, permissions: perms.iter().map(|p| Permission::new(*p)).collect(), may_invite_as: BTreeSet::new() }
    }

    /// The owner's role: every permission.
    pub fn everything() -> Self {
        RoleSpec { all: true, ..Default::default() }
    }

    /// Allow holders to put these roles on invites.
    pub fn inviting(mut self, roles: &[&str]) -> Self {
        self.may_invite_as = roles.iter().map(|r| RoleId::new(*r)).collect();
        self
    }
}