pub struct RoleSystem<S = MemoryStorage>where
S: Storage,{ /* private fields */ }Expand description
The main role-based access control system.
Implementations§
Source§impl RoleSystem<MemoryStorage>
impl RoleSystem<MemoryStorage>
Sourcepub fn with_config(config: RoleSystemConfig) -> Self
pub fn with_config(config: RoleSystemConfig) -> Self
Create a new role system with custom configuration and memory storage.
Source§impl<S> RoleSystem<S>where
S: Storage,
impl<S> RoleSystem<S>where
S: Storage,
Sourcepub fn with_storage(storage: S, config: RoleSystemConfig) -> Self
pub fn with_storage(storage: S, config: RoleSystemConfig) -> Self
Create a new role system with custom storage.
Sourcepub fn register_role(&mut self, role: Role) -> Result<()>
pub fn register_role(&mut self, role: Role) -> Result<()>
Register a new role in the system.
Sourcepub fn update_role(&mut self, role: Role) -> Result<()>
pub fn update_role(&mut self, role: Role) -> Result<()>
Update an existing role.
Sourcepub fn add_role_inheritance(&mut self, child: &str, parent: &str) -> Result<()>
pub fn add_role_inheritance(&mut self, child: &str, parent: &str) -> Result<()>
Add role inheritance (child inherits from parent).
Sourcepub fn remove_role_inheritance(
&mut self,
child: &str,
parent: &str,
) -> Result<()>
pub fn remove_role_inheritance( &mut self, child: &str, parent: &str, ) -> Result<()>
Remove role inheritance.
Sourcepub fn assign_role(&mut self, subject: &Subject, role_name: &str) -> Result<()>
pub fn assign_role(&mut self, subject: &Subject, role_name: &str) -> Result<()>
Assign a role to a subject.
Sourcepub fn remove_role(&mut self, subject: &Subject, role_name: &str) -> Result<()>
pub fn remove_role(&mut self, subject: &Subject, role_name: &str) -> Result<()>
Remove a role from a subject.
Sourcepub fn elevate_role(
&mut self,
subject: &Subject,
role_name: &str,
duration: Option<Duration>,
) -> Result<()>
pub fn elevate_role( &mut self, subject: &Subject, role_name: &str, duration: Option<Duration>, ) -> Result<()>
Temporarily elevate a subject’s role.
Sourcepub fn check_permission(
&self,
subject: &Subject,
action: &str,
resource: &Resource,
) -> Result<bool>
pub fn check_permission( &self, subject: &Subject, action: &str, resource: &Resource, ) -> Result<bool>
Check if a subject has a specific permission on a resource.
Sourcepub fn check_permission_with_context(
&self,
subject: &Subject,
action: &str,
resource: &Resource,
context: &HashMap<String, String>,
) -> Result<bool>
pub fn check_permission_with_context( &self, subject: &Subject, action: &str, resource: &Resource, context: &HashMap<String, String>, ) -> Result<bool>
Check permission with additional context.
Sourcepub fn get_subject_roles(&self, subject: &Subject) -> Result<HashSet<String>>
pub fn get_subject_roles(&self, subject: &Subject) -> Result<HashSet<String>>
Get all roles assigned to a subject (including inherited roles).
Sourcepub fn create_standard_roles(&mut self) -> Result<()>
pub fn create_standard_roles(&mut self) -> Result<()>
Creates standard roles commonly used in applications.
This method creates the following roles:
- admin: Full system access
- editor: Create and edit content
- viewer: Read-only access
- guest: Limited read access
Sourcepub fn create_application_roles(
&mut self,
app_type: ApplicationType,
) -> Result<()>
pub fn create_application_roles( &mut self, app_type: ApplicationType, ) -> Result<()>
Creates roles appropriate for specific application types.
Source§impl<S: Storage> RoleSystem<S>
impl<S: Storage> RoleSystem<S>
Sourcepub fn subject_roles(&self) -> &DashMap<String, HashSet<String>>
pub fn subject_roles(&self) -> &DashMap<String, HashSet<String>>
Get access to the subject roles mapping (for query operations).
Sourcepub fn role_hierarchy(&self) -> &DashMap<String, HashSet<String>>
pub fn role_hierarchy(&self) -> &DashMap<String, HashSet<String>>
Get access to the role hierarchy mapping (for query operations).
Sourcepub fn config(&self) -> &RoleSystemConfig
pub fn config(&self) -> &RoleSystemConfig
Get access to the configuration (for query operations).
Source§impl<S: Storage> RoleSystem<S>
impl<S: Storage> RoleSystem<S>
Sourcepub fn assign_roles<I>(&mut self, subject: &Subject, roles: I) -> Result<()>
pub fn assign_roles<I>(&mut self, subject: &Subject, roles: I) -> Result<()>
Assign multiple roles to a subject in a single operation.
Sourcepub fn remove_roles<I>(&mut self, subject: &Subject, roles: I) -> Result<()>
pub fn remove_roles<I>(&mut self, subject: &Subject, roles: I) -> Result<()>
Remove multiple roles from a subject in a single operation.
Sourcepub fn check_permissions_batch(
&self,
subject: &Subject,
permissions: &[(&str, &Resource)],
) -> Result<Vec<(String, String, bool)>>
pub fn check_permissions_batch( &self, subject: &Subject, permissions: &[(&str, &Resource)], ) -> Result<Vec<(String, String, bool)>>
Check multiple permissions for a subject in a single operation.
Sourcepub fn bulk_assign_roles(
&mut self,
assignments: &[(Subject, Vec<String>)],
) -> Result<Vec<Result<()>>>
pub fn bulk_assign_roles( &mut self, assignments: &[(Subject, Vec<String>)], ) -> Result<Vec<Result<()>>>
Bulk role assignment with validation.
Sourcepub fn get_permission_summary(
&self,
subject: &Subject,
) -> Result<PermissionSummary>
pub fn get_permission_summary( &self, subject: &Subject, ) -> Result<PermissionSummary>
Get detailed permission summary for a subject.
Source§impl<S: Storage> RoleSystem<S>
Health check extension for RoleSystem.
impl<S: Storage> RoleSystem<S>
Health check extension for RoleSystem.
Sourcepub fn health_check(&self) -> HealthReport
pub fn health_check(&self) -> HealthReport
Perform a health check on the role system.
Sourcepub fn health_check_with_config(
&self,
config: HealthCheckConfig,
) -> HealthReport
pub fn health_check_with_config( &self, config: HealthCheckConfig, ) -> HealthReport
Perform a health check with custom configuration.
Sourcepub fn is_healthy(&self) -> bool
pub fn is_healthy(&self) -> bool
Get a simple health status (useful for load balancer health checks).
Trait Implementations§
Source§impl BatchOperations for RoleSystem
impl BatchOperations for RoleSystem
Source§fn batch_check_permissions(
&self,
checks: Vec<BatchPermissionCheck>,
) -> Result<BatchResult<bool>, Error>
fn batch_check_permissions( &self, checks: Vec<BatchPermissionCheck>, ) -> Result<BatchResult<bool>, Error>
Source§fn batch_role_operations(
&mut self,
operations: Vec<BatchRoleAssignment>,
) -> Result<BatchResult<()>, Error>
fn batch_role_operations( &mut self, operations: Vec<BatchRoleAssignment>, ) -> Result<BatchResult<()>, Error>
Source§impl<S, T> ContextualPermissions<T> for RoleSystem<S>where
S: Storage,
T: AuthenticationContext,
impl<S, T> ContextualPermissions<T> for RoleSystem<S>where
S: Storage,
T: AuthenticationContext,
Source§fn check_contextual_permission(
&self,
context: &T,
action: &str,
resource: &Resource,
_additional_context: Option<HashMap<String, String>>,
) -> Result<bool>
fn check_contextual_permission( &self, context: &T, action: &str, resource: &Resource, _additional_context: Option<HashMap<String, String>>, ) -> Result<bool>
Check permission using an authentication context.
This method combines role-based permissions with any scopes/permissions granted directly by the authentication context (e.g., JWT token scopes).
§Arguments
context- The authentication context to useaction- The action to check permission forresource- The resource to check permission foradditional_context- Additional context values for conditional permissions
§Returns
true if permission is granted, false otherwise
Source§fn check_scope_permission(
&self,
context: &T,
required_scopes: &[String],
) -> Result<bool>
fn check_scope_permission( &self, context: &T, required_scopes: &[String], ) -> Result<bool>
Check permission against a list of required scopes.
This is useful for API endpoints that require specific scopes.
§Arguments
context- The authentication context to userequired_scopes- List of required scopes (any match grants permission)
§Returns
true if any required scope is granted, false otherwise