zoe_app_primitives/group/events/
roles.rs

1use forward_compatible_enum::U32Discriminants;
2use serde::{Deserialize, Serialize};
3
4use super::permissions::Permission;
5
6/// Roles within a group
7///
8/// Hierarchical roles that determine what actions a member can perform.
9/// Roles are ordered from highest (Owner) to lowest (Member) privilege.
10#[derive(
11    Debug, Clone, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord, U32Discriminants,
12)]
13#[u32_discriminants(fallback = "Member")]
14pub enum GroupRole {
15    /// Group owner (highest privilege)
16    ///
17    /// Can perform all actions including deleting the group and managing all other roles.
18    #[discriminant(9)]
19    Owner,
20    /// Administrator
21    ///
22    /// Can manage most group settings and moderate other members.
23    #[discriminant(5)]
24    Admin,
25    /// Moderator
26    ///
27    /// Can moderate discussions and manage some group settings.
28    #[discriminant(3)]
29    Moderator,
30    /// Regular member
31    ///
32    /// Basic participation rights, can post activities and read group content.
33    #[discriminant(0)]
34    Member,
35}
36
37impl GroupRole {
38    /// Check if this role has the required permission level
39    ///
40    /// Returns true if this role meets or exceeds the required permission level.
41    pub fn has_permission(&self, required: &Permission) -> bool {
42        match required {
43            Permission::OwnerOnly => matches!(self, GroupRole::Owner),
44            Permission::AdminOrAbove => matches!(self, GroupRole::Owner | GroupRole::Admin),
45            Permission::ModeratorOrAbove => matches!(
46                self,
47                GroupRole::Owner | GroupRole::Admin | GroupRole::Moderator
48            ),
49            Permission::AllMembers => true,
50        }
51    }
52
53    /// Get a human-readable name for this role
54    pub fn display_name(&self) -> &'static str {
55        match self {
56            GroupRole::Owner => "Owner",
57            GroupRole::Admin => "Administrator",
58            GroupRole::Moderator => "Moderator",
59            GroupRole::Member => "Member",
60        }
61    }
62
63    /// Check if this role can assign the target role to another member
64    ///
65    /// Generally, you can only assign roles that are lower than your own.
66    pub fn can_assign_role(&self, target_role: &GroupRole) -> bool {
67        match self {
68            GroupRole::Owner => true, // Owners can assign any role
69            GroupRole::Admin => !matches!(target_role, GroupRole::Owner),
70            GroupRole::Moderator => matches!(target_role, GroupRole::Member),
71            GroupRole::Member => false, // Members can't assign roles
72        }
73    }
74}