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>) -> AsyncRoleSystem<S>
pub fn new(role_system: RoleSystem<S>) -> AsyncRoleSystem<S>
Create a new async role system.
Sourcepub async fn register_role(&self, role: Role) -> Result<(), Error>
pub async fn register_role(&self, role: Role) -> Result<(), Error>
Register a new role in the system.
Sourcepub async fn add_role_inheritance(
&self,
child: &str,
parent: &str,
) -> Result<(), Error>
pub async fn add_role_inheritance( &self, child: &str, parent: &str, ) -> Result<(), Error>
Add role inheritance (child inherits from parent).
Sourcepub async fn remove_role_inheritance(
&self,
child: &str,
parent: &str,
) -> Result<(), Error>
pub async fn remove_role_inheritance( &self, child: &str, parent: &str, ) -> Result<(), Error>
Remove role inheritance.
Sourcepub async fn assign_role(
&self,
subject: &Subject,
role_name: &str,
) -> Result<(), Error>
pub async fn assign_role( &self, subject: &Subject, role_name: &str, ) -> Result<(), Error>
Assign a role to a subject.
Sourcepub async fn remove_role(
&self,
subject: &Subject,
role_name: &str,
) -> Result<(), Error>
pub async fn remove_role( &self, subject: &Subject, role_name: &str, ) -> Result<(), Error>
Remove a role from a subject.
Sourcepub async fn elevate_role(
&self,
subject: &Subject,
role_name: &str,
duration: Option<Duration>,
) -> Result<(), Error>
pub async fn elevate_role( &self, subject: &Subject, role_name: &str, duration: Option<Duration>, ) -> Result<(), Error>
Temporarily elevate a subject’s role.
Sourcepub async fn check_permission(
&self,
subject: &Subject,
action: &str,
resource: &Resource,
) -> Result<bool, Error>
pub async fn check_permission( &self, subject: &Subject, action: &str, resource: &Resource, ) -> Result<bool, Error>
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, Error>
pub async fn check_permission_with_context( &self, subject: &Subject, action: &str, resource: &Resource, context: &HashMap<String, String>, ) -> Result<bool, Error>
Check permission with additional context.
Sourcepub async fn get_subject_roles(
&self,
subject: &Subject,
) -> Result<HashSet<String>, Error>
pub async fn get_subject_roles( &self, subject: &Subject, ) -> Result<HashSet<String>, Error>
Get all roles assigned to a subject.
Sourcepub async fn batch_check_permissions(
&self,
subject: &Subject,
checks: &[(String, Resource)],
) -> Result<Vec<(String, Resource, bool)>, Error>
pub async fn batch_check_permissions( &self, subject: &Subject, checks: &[(String, Resource)], ) -> Result<Vec<(String, Resource, bool)>, Error>
Batch check multiple permissions for a subject.
Sourcepub async fn atomic_role_operations<F, R>(
&self,
operations: F,
) -> Result<R, Error>
pub async fn atomic_role_operations<F, R>( &self, operations: F, ) -> Result<R, Error>
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, Error>
pub async fn get_hierarchy_tree( &self, config: Option<HierarchyConfig>, ) -> Result<RoleHierarchyTree, Error>
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>, Error>
pub async fn get_role_ancestors( &self, role_id: &str, _include_inherited: bool, ) -> Result<Vec<String>, Error>
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>, Error>
pub async fn get_role_descendants( &self, role_id: &str, _include_inherited: bool, ) -> Result<Vec<String>, Error>
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>, Error>
pub async fn get_role_siblings( &self, role_id: &str, ) -> Result<Vec<String>, Error>
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>, Error>
pub async fn get_role_relationships( &self, _relationship_type: Option<RelationshipType>, ) -> Result<Vec<RoleRelationship>, Error>
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, Error>
pub async fn is_role_ancestor( &self, ancestor_id: &str, descendant_id: &str, ) -> Result<bool, Error>
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, Error>
pub async fn get_role_depth(&self, role_id: &str) -> Result<usize, Error>
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);
Trait Implementations§
Auto Trait Implementations§
impl<S> Freeze for AsyncRoleSystem<S>
impl<S> !RefUnwindSafe for AsyncRoleSystem<S>
impl<S> Send for AsyncRoleSystem<S>
impl<S> Sync for AsyncRoleSystem<S>
impl<S> Unpin for AsyncRoleSystem<S>
impl<S> !UnwindSafe for AsyncRoleSystem<S>
Blanket Implementations§
Source§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
Source§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more