#[derive(Debug, Clone)]
pub struct Role {
pub name: String,
pub parent_role: Option<Box<Role>>, }
impl Role {
pub fn new(name: &str, parent: Option<Box<Role>>) -> Self {
Role {
name: name.to_string(),
parent_role: parent,
}
}
pub fn is_higher_or_equal(&self, other: &Role) -> bool {
if self.name == other.name {
true
} else if let Some(ref parent) = self.parent_role {
parent.is_higher_or_equal(other)
} else {
false
}
}
pub fn has_circular_dependency(&self, child: &Role) -> bool {
if self.name == child.name {
return true;
}
if let Some(ref parent) = child.parent_role {
self.has_circular_dependency(parent)
} else {
false
}
}
pub fn add_child_role(&mut self, child: Role) -> Result<(), RoleError> {
if self.has_circular_dependency(&child) {
return Err(RoleError::CircularDependency);
}
Ok(())
}
}
pub struct RoleDelegation {
pub delegator: String,
pub delegatee: String,
pub role: Role,
pub expiration: u64, }
impl RoleDelegation {
pub fn new(delegator: &str, delegatee: &str, role: Role, expiration: u64) -> Self {
RoleDelegation {
delegator: delegator.to_string(),
delegatee: delegatee.to_string(),
role,
expiration,
}
}
pub fn is_valid(&self) -> bool {
std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap().as_secs() < self.expiration
}
}