1use std::collections::BTreeSet;
2
3use kanidm_proto::{
4 internal::{Group as ProtoGroup, UiHint},
5 v1::UnixGroupToken,
6};
7use uuid::Uuid;
8
9use crate::{
10 entry::{Committed, Entry, EntryCommitted, EntrySealed, GetUuid},
11 prelude::*,
12 value::PartialValue,
13};
14
15use super::accountpolicy::{AccountPolicy, ResolvedAccountPolicy};
16
17pub trait GroupType {}
19
20#[derive(Debug, Clone)]
21pub(crate) struct Unix {
22 name: String,
23 gidnumber: u32,
24}
25
26impl GroupType for Unix {}
27
28impl GroupType for () {}
29
30#[derive(Debug, Clone)]
31pub struct Group<T>
32where
33 T: GroupType,
34{
35 inner: T,
36 spn: String,
37 name: Option<String>,
38 uuid: Uuid,
39 ui_hints: BTreeSet<UiHint>,
41}
42
43macro_rules! try_from_entry {
44 ($value:expr, $inner:expr) => {{
45 let spn = $value
46 .get_ava_single_proto_string(Attribute::Spn)
47 .ok_or_else(|| OperationError::MissingAttribute(Attribute::Spn))?;
48
49 let name = $value
50 .get_ava_single_iname(Attribute::Name)
51 .map(|s| s.to_string());
52
53 let uuid = $value.get_uuid();
54
55 let ui_hints = $value
56 .get_ava_uihint(Attribute::GrantUiHint)
57 .cloned()
58 .unwrap_or_default();
59
60 Ok(Self {
61 inner: $inner,
62 name,
63 spn,
64 uuid,
65 ui_hints,
66 })
67 }};
68}
69
70impl<T: GroupType> Group<T> {
71 pub fn spn(&self) -> &String {
72 &self.spn
73 }
74
75 pub fn uuid(&self) -> &Uuid {
76 &self.uuid
77 }
78
79 pub fn name(&self) -> Option<&String> {
80 self.name.as_ref()
81 }
82
83 pub fn ui_hints(&self) -> &BTreeSet<UiHint> {
84 &self.ui_hints
85 }
86
87 pub fn to_proto(&self) -> ProtoGroup {
88 ProtoGroup {
89 spn: self.spn.clone(),
90 uuid: self.uuid.as_hyphenated().to_string(),
91 }
92 }
93}
94
95macro_rules! try_from_account {
96 ($value:expr, $qs:expr) => {{
97 let Some(iter) = $value.get_ava_as_refuuid(Attribute::MemberOf) else {
98 return Ok(vec![]);
99 };
100
101 let f = filter!(f_or(
104 iter.map(|u| f_eq(Attribute::Uuid, PartialValue::Uuid(u)))
105 .collect()
106 ));
107
108 let entries = $qs.internal_search(f).map_err(|e| {
109 admin_error!(?e, "internal search failed");
110 e
111 })?;
112
113 Ok(entries
114 .iter()
115 .map(|entry| Self::try_from_entry(&entry))
116 .filter_map(|v| v.ok())
117 .collect())
118 }};
119}
120
121impl Group<()> {
122 pub fn try_from_account<'a, TXN>(
123 value: &Entry<EntrySealed, EntryCommitted>,
124 qs: &mut TXN,
125 ) -> Result<Vec<Group<()>>, OperationError>
126 where
127 TXN: QueryServerTransaction<'a>,
128 {
129 if !value.attribute_equality(Attribute::Class, &EntryClass::Account.into()) {
130 return Err(OperationError::MissingClass(ENTRYCLASS_ACCOUNT.into()));
131 }
132
133 let user_group = try_from_entry!(value, ())?;
134 Ok(std::iter::once(user_group)
135 .chain(Self::try_from_account_reduced(value, qs)?)
136 .collect())
137 }
138
139 pub fn try_from_account_reduced<'a, E, TXN>(
140 value: &Entry<E, EntryCommitted>,
141 qs: &mut TXN,
142 ) -> Result<Vec<Group<()>>, OperationError>
143 where
144 E: Committed,
145 TXN: QueryServerTransaction<'a>,
146 {
147 try_from_account!(value, qs)
148 }
149
150 pub fn try_from_entry<E>(value: &Entry<E, EntryCommitted>) -> Result<Self, OperationError>
151 where
152 E: Committed,
153 Entry<E, EntryCommitted>: GetUuid,
154 {
155 if !value.attribute_equality(Attribute::Class, &EntryClass::Group.into()) {
156 return Err(OperationError::MissingAttribute(Attribute::Group));
157 }
158
159 try_from_entry!(value, ())
160 }
161}
162
163impl Group<Unix> {
164 pub fn try_from_account<'a, TXN>(
165 value: &Entry<EntrySealed, EntryCommitted>,
166 qs: &mut TXN,
167 ) -> Result<Vec<Group<Unix>>, OperationError>
168 where
169 TXN: QueryServerTransaction<'a>,
170 {
171 if !value.attribute_equality(Attribute::Class, &EntryClass::Account.into()) {
172 return Err(OperationError::MissingClass(ENTRYCLASS_ACCOUNT.into()));
173 }
174
175 if !value.attribute_equality(Attribute::Class, &EntryClass::PosixAccount.into()) {
176 return Err(OperationError::MissingClass(
177 ENTRYCLASS_POSIX_ACCOUNT.into(),
178 ));
179 }
180
181 let name = value
182 .get_ava_single_iname(Attribute::Name)
183 .map(|s| s.to_string())
184 .ok_or_else(|| OperationError::MissingAttribute(Attribute::Name))?;
185
186 let gidnumber = value
187 .get_ava_single_uint32(Attribute::GidNumber)
188 .ok_or_else(|| OperationError::MissingAttribute(Attribute::GidNumber))?;
189
190 let user_group = try_from_entry!(value, Unix { name, gidnumber })?;
191
192 Ok(std::iter::once(user_group)
193 .chain(Self::try_from_account_reduced(value, qs)?)
194 .collect())
195 }
196
197 pub fn try_from_account_reduced<'a, E, TXN>(
198 value: &Entry<E, EntryCommitted>,
199 qs: &mut TXN,
200 ) -> Result<Vec<Group<Unix>>, OperationError>
201 where
202 E: Committed,
203 TXN: QueryServerTransaction<'a>,
204 {
205 try_from_account!(value, qs)
206 }
207
208 fn check_entry_classes<E>(value: &Entry<E, EntryCommitted>) -> Result<(), OperationError>
209 where
210 E: Committed,
211 Entry<E, EntryCommitted>: GetUuid,
212 {
213 if value.attribute_equality(Attribute::Class, &EntryClass::Account.into()) {
215 if !value.attribute_equality(Attribute::Class, &EntryClass::PosixAccount.into()) {
216 return Err(OperationError::MissingClass(
217 ENTRYCLASS_POSIX_ACCOUNT.into(),
218 ));
219 }
220 } else {
221 if !value.attribute_equality(Attribute::Class, &EntryClass::PosixGroup.into()) {
223 return Err(OperationError::MissingClass(ENTRYCLASS_POSIX_GROUP.into()));
224 }
225
226 if !value.attribute_equality(Attribute::Class, &EntryClass::Group.into()) {
227 return Err(OperationError::MissingAttribute(Attribute::Group));
228 }
229 }
230 Ok(())
231 }
232
233 pub fn try_from_entry<E>(value: &Entry<E, EntryCommitted>) -> Result<Self, OperationError>
234 where
235 E: Committed,
236 Entry<E, EntryCommitted>: GetUuid,
237 {
238 Self::check_entry_classes(value)?;
239
240 let name = value
241 .get_ava_single_iname(Attribute::Name)
242 .map(|s| s.to_string())
243 .ok_or_else(|| OperationError::MissingAttribute(Attribute::Name))?;
244
245 let gidnumber = value
246 .get_ava_single_uint32(Attribute::GidNumber)
247 .ok_or_else(|| OperationError::MissingAttribute(Attribute::GidNumber))?;
248
249 try_from_entry!(value, Unix { name, gidnumber })
250 }
251
252 pub(crate) fn to_unixgrouptoken(&self) -> UnixGroupToken {
253 UnixGroupToken {
254 name: self.inner.name.clone(),
255 spn: self.spn.clone(),
256 uuid: self.uuid,
257 gidnumber: self.inner.gidnumber,
258 }
259 }
260}
261
262pub(crate) fn load_account_policy<'a, T>(
263 value: &Entry<EntrySealed, EntryCommitted>,
264 qs: &mut T,
265) -> Result<ResolvedAccountPolicy, OperationError>
266where
267 T: QueryServerTransaction<'a>,
268{
269 let iter = match value.get_ava_as_refuuid(Attribute::MemberOf) {
270 Some(v) => v,
271 None => Box::new(Vec::<Uuid>::new().into_iter()),
272 };
273
274 let f = filter!(f_or(
277 iter.map(|u| f_eq(Attribute::Uuid, PartialValue::Uuid(u)))
278 .collect()
279 ));
280
281 let entries = qs.internal_search(f).map_err(|e| {
282 admin_error!(?e, "internal search failed");
283 e
284 })?;
285
286 Ok(ResolvedAccountPolicy::fold_from(entries.iter().filter_map(
287 |entry| {
288 let acc_pol: Option<AccountPolicy> = entry.as_ref().into();
289 acc_pol
290 },
291 )))
292}
293
294pub(crate) fn load_all_groups_from_account<'a, E, TXN>(
295 value: &Entry<E, EntryCommitted>,
296 qs: &mut TXN,
297) -> Result<(Vec<Group<()>>, Vec<Group<Unix>>), OperationError>
298where
299 E: Committed,
300 Entry<E, EntryCommitted>: GetUuid,
301 TXN: QueryServerTransaction<'a>,
302{
303 let Some(iter) = value.get_ava_as_refuuid(Attribute::MemberOf) else {
304 return Ok((vec![], vec![]));
305 };
306
307 let conditions: Vec<_> = iter
308 .map(|u| f_eq(Attribute::Uuid, PartialValue::Uuid(u)))
309 .collect();
310
311 let f = filter!(f_or(conditions));
312
313 let entries = qs.internal_search(f).map_err(|e| {
314 admin_error!(?e, "internal search failed");
315 e
316 })?;
317
318 let mut groups = vec![];
319 let mut unix_groups = Group::<Unix>::try_from_entry(value)
320 .ok()
321 .into_iter()
322 .collect::<Vec<_>>();
323
324 for entry in entries.iter() {
325 let entry = entry.as_ref();
326 if entry.attribute_equality(Attribute::Class, &EntryClass::PosixGroup.into()) {
327 unix_groups.push(Group::<Unix>::try_from_entry::<EntrySealed>(entry)?);
328 }
329
330 groups.push(Group::<()>::try_from_entry::<EntrySealed>(entry)?);
332 }
333
334 Ok((groups, unix_groups))
335}