1
use serde::{Deserialize, Serialize};
2

            
3
use crate::define_basic_unique_mapped_view;
4
use crate::document::{CollectionDocument, Emit};
5
use crate::permissions::Statement;
6
use crate::schema::{Collection, NamedCollection};
7

            
8
/// A named group of permissions statements.
9
115720
#[derive(Clone, Debug, Serialize, Deserialize, Collection)]
10
#[collection(name = "permission-group", authority="khonsulabs", views = [ByName], core = crate)]
11
#[must_use]
12
pub 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

            
19
impl PermissionGroup {
20
    /// Returns a new group with no statements and the name provided.
21
8
    pub fn named<S: Into<String>>(name: S) -> Self {
22
8
        Self {
23
8
            name: name.into(),
24
8
            statements: Vec::new(),
25
8
        }
26
8
    }
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

            
35
impl NamedCollection for PermissionGroup {
36
    type ByNameView = ByName;
37
}
38

            
39
define_basic_unique_mapped_view!(
40
    ByName,
41
    PermissionGroup,
42
    1,
43
    "by-name",
44
    String,
45
560
    |document: CollectionDocument<PermissionGroup>| {
46
560
        document.header.emit_key(document.contents.name)
47
560
    }
48
);