zaun_community_protocol/permissions/
gate.rs1use crate::error_code::ErrorCode;
6use crate::model::MembershipState;
7
8pub const UNBOUND_ALLOWED: &[&str] = &[
15 "session.hello",
16 "community.create",
17 "invite.redeem",
18 "membership.request",
19 "community.rotations",
20 "migration.reauth",
21];
22
23pub const UNBOUND_ONLY: &[&str] = &[
25 "community.create",
26 "invite.redeem",
27 "membership.request",
28 "migration.reauth",
29];
30
31pub const PENDING_ALLOWED: &[&str] = &[
34 "session.hello",
35 "community.get",
36 "community.icon.get",
37 "community.rotations",
38 "admission.policy.get",
39 "community.leave",
40 "sync.ack",
41];
42
43pub const SUSPENDED_ALLOWED: &[&str] = PENDING_ALLOWED;
45
46#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
48pub enum GateDenial {
49 NotBound,
51 AlreadyBound,
53 PendingApproval,
55 Suspended,
57 Banned,
59}
60
61impl GateDenial {
62 #[must_use]
64 pub const fn conflict_reason(self) -> Option<&'static str> {
65 match self {
66 Self::AlreadyBound => Some("already_bound"),
67 _ => None,
68 }
69 }
70
71 #[must_use]
73 pub const fn error_code(self) -> ErrorCode {
74 match self {
75 Self::NotBound => ErrorCode::NotBound,
76 Self::AlreadyBound => ErrorCode::Conflict,
77 Self::PendingApproval => ErrorCode::PendingApproval,
78 Self::Suspended => ErrorCode::Suspended,
79 Self::Banned => ErrorCode::Banned,
80 }
81 }
82}
83
84pub fn gate_unbound(command: &str) -> Result<(), GateDenial> {
86 if UNBOUND_ALLOWED.contains(&command) {
87 Ok(())
88 } else {
89 Err(GateDenial::NotBound)
90 }
91}
92
93pub fn gate(state: MembershipState, command: &str) -> Result<(), GateDenial> {
96 match state {
97 MembershipState::Banned => return Err(GateDenial::Banned),
98 MembershipState::Left => return Err(GateDenial::NotBound),
99 _ => {}
100 }
101 if UNBOUND_ONLY.contains(&command) {
102 return Err(GateDenial::AlreadyBound);
103 }
104 match state {
105 MembershipState::Pending if !PENDING_ALLOWED.contains(&command) => {
106 Err(GateDenial::PendingApproval)
107 }
108 MembershipState::Suspended if !SUSPENDED_ALLOWED.contains(&command) => {
109 Err(GateDenial::Suspended)
110 }
111 _ => Ok(()),
112 }
113}