bonsaidb_core/admin/
group.rs

1use serde::{Deserialize, Serialize};
2
3use crate::define_basic_unique_mapped_view;
4use crate::document::{CollectionDocument, Emit};
5use crate::permissions::Statement;
6use crate::schema::{Collection, NamedCollection};
7
8/// A named group of permissions statements.
9#[derive(Clone, Debug, Serialize, Deserialize, Collection)]
10#[collection(name = "permission-group", authority="khonsulabs", views = [ByName], core = crate)]
11#[must_use]
12pub struct PermissionGroup {
13    /// The name of the group. Must be unique.
14    pub name: String,
15    /// The permission statements.
16    pub statements: Vec<Statement>,
17}
18
19impl PermissionGroup {
20    /// Returns a new group with no statements and the name provided.
21    pub fn named<S: Into<String>>(name: S) -> Self {
22        Self {
23            name: name.into(),
24            statements: Vec::new(),
25        }
26    }
27
28    /// Builder-style method. Returns self after replacing the current statements with `statements`.
29    pub fn with_group_ids<I: IntoIterator<Item = Statement>>(mut self, statements: I) -> Self {
30        self.statements = statements.into_iter().collect();
31        self
32    }
33}
34
35impl NamedCollection for PermissionGroup {
36    type ByNameView = ByName;
37}
38
39define_basic_unique_mapped_view!(
40    ByName,
41    PermissionGroup,
42    1,
43    "by-name",
44    String,
45    |document: CollectionDocument<PermissionGroup>| {
46        document.header.emit_key(document.contents.name)
47    }
48);