use serde::{Deserialize, Serialize};
use crate::define_basic_unique_mapped_view;
use crate::document::{CollectionDocument, Emit};
use crate::permissions::Statement;
use crate::schema::{Collection, NamedCollection};
#[derive(Clone, Debug, Serialize, Deserialize, Collection)]
#[collection(name = "permission-group", authority="khonsulabs", views = [ByName], core = crate)]
#[must_use]
pub struct PermissionGroup {
pub name: String,
pub statements: Vec<Statement>,
}
impl PermissionGroup {
pub fn named<S: Into<String>>(name: S) -> Self {
Self {
name: name.into(),
statements: Vec::new(),
}
}
pub fn with_group_ids<I: IntoIterator<Item = Statement>>(mut self, statements: I) -> Self {
self.statements = statements.into_iter().collect();
self
}
}
impl NamedCollection for PermissionGroup {
type ByNameView = ByName;
}
define_basic_unique_mapped_view!(
ByName,
PermissionGroup,
1,
"by-name",
String,
|document: CollectionDocument<PermissionGroup>| {
document.header.emit_key(document.contents.name)
}
);