domain: add Identity & Access entities (User, Role, Permission, Group)
This commit is contained in:
46
crates/domain/src/entities/group.rs
Normal file
46
crates/domain/src/entities/group.rs
Normal file
@@ -0,0 +1,46 @@
|
||||
use std::collections::HashSet;
|
||||
use crate::errors::DomainError;
|
||||
use crate::value_objects::SystemId;
|
||||
|
||||
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
|
||||
pub struct Group {
|
||||
pub group_id: SystemId,
|
||||
pub name: String,
|
||||
pub owner_user_id: SystemId,
|
||||
pub members: HashSet<SystemId>,
|
||||
}
|
||||
|
||||
impl Group {
|
||||
pub fn new(name: impl Into<String>, owner_user_id: SystemId) -> Self {
|
||||
let mut members = HashSet::new();
|
||||
members.insert(owner_user_id);
|
||||
Self {
|
||||
group_id: SystemId::new(),
|
||||
name: name.into(),
|
||||
owner_user_id,
|
||||
members,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn add_member(&mut self, user_id: SystemId) -> Result<(), DomainError> {
|
||||
if self.members.contains(&user_id) {
|
||||
return Err(DomainError::Conflict(format!("User {user_id} is already a member")));
|
||||
}
|
||||
self.members.insert(user_id);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn remove_member(&mut self, user_id: SystemId) -> Result<(), DomainError> {
|
||||
if user_id == self.owner_user_id {
|
||||
return Err(DomainError::Validation("Cannot remove the group owner".to_string()));
|
||||
}
|
||||
if !self.members.remove(&user_id) {
|
||||
return Err(DomainError::NotFound(format!("User {user_id} is not a member")));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn is_member(&self, user_id: &SystemId) -> bool {
|
||||
self.members.contains(user_id)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user