use actionable::Permissions;
use serde::{Deserialize, Serialize};
use crate::admin::group;
use crate::connection::{AsyncStorageConnection, Connection, IdentityReference, StorageConnection};
use crate::define_basic_unique_mapped_view;
use crate::document::{CollectionDocument, Emit};
use crate::schema::{Collection, Nameable, NamedCollection, SerializedCollection};
#[derive(Clone, Debug, Serialize, Deserialize, Collection)]
#[collection(name = "role", authority="khonsulabs", views = [ByName], core = crate)]
#[must_use]
pub struct Role {
pub name: String,
pub groups: Vec<u64>,
}
impl Role {
pub fn named<S: Into<String>>(name: S) -> Self {
Self {
name: name.into(),
groups: Vec::new(),
}
}
pub fn with_group_ids<I: IntoIterator<Item = u64>>(mut self, ids: I) -> Self {
self.groups = ids.into_iter().collect();
self
}
pub fn assume_identity<'name, Storage: StorageConnection>(
name_or_id: impl Nameable<'name, u64>,
storage: &Storage,
) -> Result<Storage::Authenticated, crate::Error> {
storage.assume_identity(IdentityReference::Role(name_or_id.name()?))
}
pub async fn assume_identity_async<'name, Storage: AsyncStorageConnection>(
name_or_id: impl Nameable<'name, u64> + Send,
storage: &Storage,
) -> Result<Storage::Authenticated, crate::Error> {
storage
.assume_identity(IdentityReference::Role(name_or_id.name()?))
.await
}
pub fn effective_permissions<C: Connection>(
&self,
admin: &C,
inherit_permissions: &Permissions,
) -> Result<Permissions, crate::Error> {
let groups = group::PermissionGroup::get_multiple(&self.groups, admin)?;
let merged_permissions = Permissions::merged(
groups
.into_iter()
.map(|group| Permissions::from(group.contents.statements))
.collect::<Vec<_>>()
.iter()
.chain(std::iter::once(inherit_permissions)),
);
Ok(merged_permissions)
}
}
impl NamedCollection for Role {
type ByNameView = ByName;
}
define_basic_unique_mapped_view!(
ByName,
Role,
1,
"by-name",
String,
|document: CollectionDocument<Role>| { document.header.emit_key(document.contents.name) }
);