pub struct AsyncRoleSystem<S>{ /* private fields */ }Expand description
Async wrapper around the role system for non-blocking operations.
Implementations§
Source§impl<S> AsyncRoleSystem<S>
impl<S> AsyncRoleSystem<S>
Sourcepub fn new(role_system: RoleSystem<S>) -> Self
pub fn new(role_system: RoleSystem<S>) -> Self
Create a new async role system.
Sourcepub async fn register_role(&self, role: Role) -> Result<()>
pub async fn register_role(&self, role: Role) -> Result<()>
Register a new role in the system.
Sourcepub async fn add_role_inheritance(
&self,
child: &str,
parent: &str,
) -> Result<()>
pub async fn add_role_inheritance( &self, child: &str, parent: &str, ) -> Result<()>
Add role inheritance (child inherits from parent).
Sourcepub async fn remove_role_inheritance(
&self,
child: &str,
parent: &str,
) -> Result<()>
pub async fn remove_role_inheritance( &self, child: &str, parent: &str, ) -> Result<()>
Remove role inheritance.
Sourcepub async fn assign_role(
&self,
subject: &Subject,
role_name: &str,
) -> Result<()>
pub async fn assign_role( &self, subject: &Subject, role_name: &str, ) -> Result<()>
Assign a role to a subject.
Sourcepub async fn remove_role(
&self,
subject: &Subject,
role_name: &str,
) -> Result<()>
pub async fn remove_role( &self, subject: &Subject, role_name: &str, ) -> Result<()>
Remove a role from a subject.
Sourcepub async fn elevate_role(
&self,
subject: &Subject,
role_name: &str,
duration: Option<Duration>,
) -> Result<()>
pub async fn elevate_role( &self, subject: &Subject, role_name: &str, duration: Option<Duration>, ) -> Result<()>
Temporarily elevate a subject’s role.
Sourcepub async fn check_permission(
&self,
subject: &Subject,
action: &str,
resource: &Resource,
) -> Result<bool>
pub async fn check_permission( &self, subject: &Subject, action: &str, resource: &Resource, ) -> Result<bool>
Check if a subject has a specific permission on a resource.
Sourcepub async fn check_permission_with_context(
&self,
subject: &Subject,
action: &str,
resource: &Resource,
context: &HashMap<String, String>,
) -> Result<bool>
pub async fn check_permission_with_context( &self, subject: &Subject, action: &str, resource: &Resource, context: &HashMap<String, String>, ) -> Result<bool>
Check permission with additional context.
Sourcepub async fn get_subject_roles(
&self,
subject: &Subject,
) -> Result<HashSet<String>>
pub async fn get_subject_roles( &self, subject: &Subject, ) -> Result<HashSet<String>>
Get all roles assigned to a subject.
Sourcepub async fn batch_check_permissions(
&self,
subject: &Subject,
checks: &[(String, Resource)],
) -> Result<Vec<(String, Resource, bool)>>
pub async fn batch_check_permissions( &self, subject: &Subject, checks: &[(String, Resource)], ) -> Result<Vec<(String, Resource, bool)>>
Batch check multiple permissions for a subject.
Sourcepub async fn atomic_role_operations<F, R>(&self, operations: F) -> Result<R>
pub async fn atomic_role_operations<F, R>(&self, operations: F) -> Result<R>
Perform multiple role operations atomically.
Sourcepub async fn with_read_access<F, R>(&self, operation: F) -> R
pub async fn with_read_access<F, R>(&self, operation: F) -> R
Get a read-only reference to the role system for complex queries.
Sourcepub async fn get_hierarchy_tree(
&self,
config: Option<HierarchyConfig>,
) -> Result<RoleHierarchyTree>
pub async fn get_hierarchy_tree( &self, config: Option<HierarchyConfig>, ) -> Result<RoleHierarchyTree>
Get the complete hierarchy tree structure.
This method provides a structured view of the entire role hierarchy, useful for visualization, API responses, and external system integration.
§Arguments
config- Optional hierarchy configuration. If None, uses default settings.
§Returns
A RoleHierarchyTree containing the complete hierarchy structure with metadata.
§Example
let storage = MemoryStorage::new();
let role_sys = RoleSystem::with_storage(storage, RoleSystemConfig::default());
let role_system = AsyncRoleSystem::new(role_sys);
let config = HierarchyConfigBuilder::new()
.enable_hierarchy_access(true)
.max_depth(10)
.build();
let tree = role_system.get_hierarchy_tree(Some(config)).await?;
println!("Total roles: {}, Max depth: {}", tree.total_roles, tree.max_depth);Sourcepub async fn get_role_ancestors(
&self,
role_id: &str,
_include_inherited: bool,
) -> Result<Vec<String>>
pub async fn get_role_ancestors( &self, role_id: &str, _include_inherited: bool, ) -> Result<Vec<String>>
Get all parent roles for a given role (ancestors).
This method returns all roles that the specified role inherits from, including both direct parents and inherited ancestors.
§Arguments
role_id- The ID of the role to get ancestors for_include_inherited- Whether to include inherited (indirect) parents
§Returns
A vector of role IDs representing all ancestor roles.
§Example
let ancestors = role_system.get_role_ancestors("junior_dev", true).await?;
for ancestor_id in ancestors {
println!("Inherits from: {}", ancestor_id);
}Sourcepub async fn get_role_descendants(
&self,
role_id: &str,
_include_inherited: bool,
) -> Result<Vec<String>>
pub async fn get_role_descendants( &self, role_id: &str, _include_inherited: bool, ) -> Result<Vec<String>>
Get all child roles for a given role (descendants).
This method returns all roles that inherit from the specified role, including both direct children and inherited descendants.
§Arguments
role_id- The ID of the role to get descendants for_include_inherited- Whether to include inherited (indirect) children
§Returns
A vector of role IDs representing all descendant roles.
§Example
let storage = MemoryStorage::new();
let role_sys = RoleSystem::with_storage(storage, RoleSystemConfig::default());
let role_system = AsyncRoleSystem::new(role_sys);
let descendants = role_system.get_role_descendants("team_lead", true).await?;
for descendant_id in descendants {
println!("Has child: {}", descendant_id);
}Sourcepub async fn get_role_siblings(&self, role_id: &str) -> Result<Vec<String>>
pub async fn get_role_siblings(&self, role_id: &str) -> Result<Vec<String>>
Get all sibling roles for a given role.
Sibling roles are roles that share the same parent in the hierarchy.
§Arguments
role_id- The ID of the role to get siblings for
§Returns
A vector of role IDs representing all sibling roles.
§Example
let storage = MemoryStorage::new();
let role_sys = RoleSystem::with_storage(storage, RoleSystemConfig::default());
let role_system = AsyncRoleSystem::new(role_sys);
let siblings = role_system.get_role_siblings("senior_dev").await?;
for sibling_id in siblings {
println!("Sibling role: {}", sibling_id);
}Sourcepub async fn get_role_relationships(
&self,
_relationship_type: Option<RelationshipType>,
) -> Result<Vec<RoleRelationship>>
pub async fn get_role_relationships( &self, _relationship_type: Option<RelationshipType>, ) -> Result<Vec<RoleRelationship>>
Get all role relationships in the hierarchy.
This method returns all parent-child relationships, useful for database storage, API responses, and external system integration.
§Arguments
relationship_type- Optional filter for relationship type
§Returns
A vector of RoleRelationship objects representing all relationships.
§Example
let storage = MemoryStorage::new();
let role_sys = RoleSystem::with_storage(storage, RoleSystemConfig::default());
let role_system = AsyncRoleSystem::new(role_sys);
// Get all relationships
let all_relationships = role_system.get_role_relationships(None).await?;
// Get only direct relationships
let direct_relationships = role_system
.get_role_relationships(Some(RelationshipType::Direct))
.await?;Sourcepub async fn is_role_ancestor(
&self,
ancestor_id: &str,
descendant_id: &str,
) -> Result<bool>
pub async fn is_role_ancestor( &self, ancestor_id: &str, descendant_id: &str, ) -> Result<bool>
Check if one role is an ancestor of another.
This method checks if ancestor_id is in the inheritance chain of descendant_id.
§Arguments
ancestor_id- The potential ancestor role IDdescendant_id- The potential descendant role ID
§Returns
true if ancestor_id is an ancestor of descendant_id.
§Example
let storage = MemoryStorage::new();
let role_sys = RoleSystem::with_storage(storage, RoleSystemConfig::default());
let role_system = AsyncRoleSystem::new(role_sys);
let is_ancestor = role_system
.is_role_ancestor("admin", "junior_dev")
.await?;
if is_ancestor {
println!("admin is an ancestor of junior_dev");
}Sourcepub async fn get_role_depth(&self, role_id: &str) -> Result<usize>
pub async fn get_role_depth(&self, role_id: &str) -> Result<usize>
Get the hierarchy depth of a role.
The depth is the number of levels from the root of the hierarchy. Root roles have depth 0.
§Arguments
role_id- The ID of the role to get depth for
§Returns
The depth of the role in the hierarchy.
§Example
let storage = MemoryStorage::new();
let role_sys = RoleSystem::with_storage(storage, RoleSystemConfig::default());
let role_system = AsyncRoleSystem::new(role_sys);
let depth = role_system.get_role_depth("senior_dev").await?;
println!("Role depth: {}", depth);