coqui_access_acl/
principal.rs1use core::fmt;
4use core::str::FromStr;
5
6use coqui_access::{Person, Subject};
7
8#[derive(Debug, Clone, PartialEq, Eq, Hash)]
12#[non_exhaustive]
13pub enum Principal {
14 Subject(Subject),
16 Everyone,
18 Authenticated,
20 Owner,
23 OwnerGroup,
25 Mask,
27}
28
29impl Principal {
30 pub fn matches(
33 &self,
34 person: Option<&Person>,
35 owner: Option<&Subject>,
36 owner_group: Option<&Subject>,
37 ) -> bool {
38 match self {
39 Self::Everyone => true,
40 Self::Mask => false,
41 Self::Authenticated => person.is_some(),
42 Self::Subject(subject) => person.is_some_and(|person| subject.matches(person)),
43 Self::Owner => matches_one(owner, person),
44 Self::OwnerGroup => matches_one(owner_group, person),
45 }
46 }
47}
48
49fn matches_one(subject: Option<&Subject>, person: Option<&Person>) -> bool {
50 subject
51 .zip(person)
52 .is_some_and(|(subject, person)| subject.matches(person))
53}
54
55impl From<Subject> for Principal {
56 fn from(subject: Subject) -> Self {
57 Self::Subject(subject)
58 }
59}
60
61impl fmt::Display for Principal {
62 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
63 match self {
64 Self::Subject(subject) => fmt::Display::fmt(subject, formatter),
65 Self::Everyone => formatter.write_str("everyone"),
66 Self::Authenticated => formatter.write_str("authenticated"),
67 Self::Owner => formatter.write_str("owner"),
68 Self::OwnerGroup => formatter.write_str("owner_group"),
69 Self::Mask => formatter.write_str("mask"),
70 }
71 }
72}
73
74#[derive(Debug, Clone, Copy, PartialEq, Eq)]
76#[non_exhaustive]
77pub struct PrincipalError;
78
79impl fmt::Display for PrincipalError {
80 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
81 formatter.write_str(
82 "a principal is everyone, authenticated, owner, owner_group, mask or a subject",
83 )
84 }
85}
86
87impl core::error::Error for PrincipalError {}
88
89impl FromStr for Principal {
90 type Err = PrincipalError;
91
92 fn from_str(text: &str) -> Result<Self, PrincipalError> {
93 Ok(match text {
94 "everyone" => Self::Everyone,
95 "authenticated" => Self::Authenticated,
96 "owner" => Self::Owner,
97 "owner_group" => Self::OwnerGroup,
98 "mask" => Self::Mask,
99 _ => Self::Subject(text.parse().map_err(|_| PrincipalError)?),
100 })
101 }
102}
103
104#[cfg(test)]
105mod tests {
106 use alloc::string::ToString;
107
108 use super::*;
109
110 #[test]
111 fn every_principal_reads_back_its_own_text() {
112 for text in [
113 "everyone",
114 "authenticated",
115 "owner",
116 "owner_group",
117 "mask",
118 "group:staff",
119 "account:alice",
120 "person:p-1",
121 ] {
122 assert_eq!(text.parse::<Principal>().unwrap().to_string(), text);
123 }
124 assert!("nobody".parse::<Principal>().is_err());
125 }
126
127 #[test]
128 fn owners_match_through_the_item_and_anonymous_only_matches_everyone() {
129 let alice = Person {
130 name: "alice".into(),
131 groups: alloc::vec!["staff".into()],
132 ..Person::default()
133 };
134 let owner: Subject = "account:alice".parse().unwrap();
135 let group: Subject = "group:staff".parse().unwrap();
136 assert!(Principal::Owner.matches(Some(&alice), Some(&owner), None));
137 assert!(!Principal::Owner.matches(Some(&alice), None, None));
138 assert!(Principal::OwnerGroup.matches(Some(&alice), None, Some(&group)));
139 assert!(Principal::Everyone.matches(None, None, None));
140 assert!(!Principal::Authenticated.matches(None, None, None));
141 assert!(!Principal::Owner.matches(None, Some(&owner), None));
142 assert!(!Principal::Mask.matches(Some(&alice), None, None));
143 }
144
145 #[test]
146 fn a_group_principal_and_the_owning_group_match_without_regard_to_case() {
147 let alice = Person {
148 name: "alice".into(),
149 groups: alloc::vec!["domain users".into()],
150 ..Person::default()
151 };
152 let named: Principal = "group:Domain Users".parse().unwrap();
153 assert!(named.matches(Some(&alice), None, None));
154 let other: Principal = "group:Domain Admins".parse().unwrap();
155 assert!(!other.matches(Some(&alice), None, None));
156 let owning: Subject = "group:DOMAIN USERS".parse().unwrap();
157 assert!(Principal::OwnerGroup.matches(Some(&alice), None, Some(&owning)));
158 let elsewhere: Subject = "group:DOMAIN ADMINS".parse().unwrap();
159 assert!(!Principal::OwnerGroup.matches(Some(&alice), None, Some(&elsewhere)));
160 }
161}