pub struct PolicyManager { /* private fields */ }Expand description
Policy manager wrapping Casbin enforcer
Implementations§
Source§impl PolicyManager
impl PolicyManager
Sourcepub async fn new(policies_dir: impl AsRef<Path>) -> Result<Self, PolicyError>
pub async fn new(policies_dir: impl AsRef<Path>) -> Result<Self, PolicyError>
Create a new PolicyManager loading policies from the given directory
If the policies directory doesn’t exist, creates it with default policies.
Sourcepub async fn permissive() -> Result<Self, PolicyError>
pub async fn permissive() -> Result<Self, PolicyError>
Create a PolicyManager with allow-all policies (for tests only).
WARNING: Not for production use. Production uses new() which loads
deny-by-default policies from disk.
Sourcepub async fn check(
&self,
user: &str,
resource: &str,
operation: Operation,
) -> bool
pub async fn check( &self, user: &str, resource: &str, operation: Operation, ) -> bool
Check if a user is allowed to perform an operation on a resource
Uses wildcard “*” for domain. For domain-specific checks, use check_with_domain().
§Arguments
user- The user or role requesting accessresource- The resource being accessed (e.g., “model:qwen3-small”)operation- The operation being requested
§Returns
true if access is allowed, false otherwise
Sourcepub async fn check_with_domain(
&self,
user: &str,
domain: &str,
resource: &str,
operation: &str,
) -> bool
pub async fn check_with_domain( &self, user: &str, domain: &str, resource: &str, operation: &str, ) -> bool
Check if a user is allowed to perform an operation on a resource in a specific domain
Accepts any &str for operation, supporting both classic enum-backed actions
(e.g., "infer", "train") and dot-namespaced actions (e.g., "ttt.writeback").
§Arguments
user- The user or role requesting accessdomain- The domain (e.g., “HfModel”, “HfDataset”)resource- The resource being accessed (e.g., “model:qwen3-small”)operation- The operation string (e.g., “infer”, “ttt.writeback”, “*”)
§Returns
true if access is allowed, false otherwise
Sourcepub fn check_sync(
&self,
user: &str,
resource: &str,
operation: Operation,
) -> bool
pub fn check_sync( &self, user: &str, resource: &str, operation: Operation, ) -> bool
Synchronous policy check (blocks on the async check)
Use this in synchronous contexts. For async code, prefer check().
Sourcepub async fn check_all(&self, user: &str, resource: &str) -> u32
pub async fn check_all(&self, user: &str, resource: &str) -> u32
Check multiple operations at once, returning a bitmask
Sourcepub async fn add_policy(
&self,
user: &str,
resource: &str,
operation: &str,
) -> Result<bool, PolicyError>
pub async fn add_policy( &self, user: &str, resource: &str, operation: &str, ) -> Result<bool, PolicyError>
Add a policy rule (uses wildcard “*” for domain, “allow” for effect)
For domain-specific policies, use add_policy_with_domain().
All inputs are validated for:
- Maximum length (256 characters)
- No null bytes
- No line breaks (CSV injection prevention)
Sourcepub async fn add_policy_with_domain(
&self,
user: &str,
domain: &str,
resource: &str,
operation: &str,
effect: &str,
) -> Result<bool, PolicyError>
pub async fn add_policy_with_domain( &self, user: &str, domain: &str, resource: &str, operation: &str, effect: &str, ) -> Result<bool, PolicyError>
Add a policy rule with domain and effect
§Arguments
user- The user or role this policy applies todomain- The domain (e.g., “HfModel”, “HfDataset”, “*” for all)resource- The resource pattern (e.g., “model:”, “data:sales-”)operation- The operation (e.g., “infer”, “train”, “*” for all)effect- “allow” or “deny”
Sourcepub async fn remove_policy(
&self,
user: &str,
resource: &str,
operation: &str,
) -> Result<bool, PolicyError>
pub async fn remove_policy( &self, user: &str, resource: &str, operation: &str, ) -> Result<bool, PolicyError>
Remove a policy rule (uses wildcard “*” for domain, “allow” for effect)
For domain-specific policies, use remove_policy_with_domain().
Sourcepub async fn remove_policy_with_domain(
&self,
user: &str,
domain: &str,
resource: &str,
operation: &str,
effect: &str,
) -> Result<bool, PolicyError>
pub async fn remove_policy_with_domain( &self, user: &str, domain: &str, resource: &str, operation: &str, effect: &str, ) -> Result<bool, PolicyError>
Remove a policy rule with domain and effect
All inputs are validated for security.
Sourcepub async fn add_role_for_user(
&self,
user: &str,
role: &str,
) -> Result<bool, PolicyError>
pub async fn add_role_for_user( &self, user: &str, role: &str, ) -> Result<bool, PolicyError>
Add a role for a user
All inputs are validated for security.
Sourcepub async fn remove_role_for_user(
&self,
user: &str,
role: &str,
) -> Result<bool, PolicyError>
pub async fn remove_role_for_user( &self, user: &str, role: &str, ) -> Result<bool, PolicyError>
Remove a role from a user
All inputs are validated for security.
Sourcepub async fn add_role_for_user_in_domain(
&self,
user: &str,
role: &str,
domain: &str,
) -> Result<bool, PolicyError>
pub async fn add_role_for_user_in_domain( &self, user: &str, role: &str, domain: &str, ) -> Result<bool, PolicyError>
Add a role membership scoped to one verified tenant/domain.
Sourcepub async fn remove_role_for_user_in_domain(
&self,
user: &str,
role: &str,
domain: &str,
) -> Result<bool, PolicyError>
pub async fn remove_role_for_user_in_domain( &self, user: &str, role: &str, domain: &str, ) -> Result<bool, PolicyError>
Remove only the role membership belonging to one tenant/domain.
Sourcepub async fn get_domain_grouping_policy(&self) -> Vec<Vec<String>>
pub async fn get_domain_grouping_policy(&self) -> Vec<Vec<String>>
Get domain-scoped role membership records (user, role, domain).
Sourcepub async fn get_roles_for_user(&self, user: &str) -> Vec<String>
pub async fn get_roles_for_user(&self, user: &str) -> Vec<String>
Get all roles for a user
Sourcepub async fn get_users_for_role(&self, role: &str) -> Vec<String>
pub async fn get_users_for_role(&self, role: &str) -> Vec<String>
Get all users with a role
Sourcepub async fn get_policy(&self) -> Vec<Vec<String>>
pub async fn get_policy(&self) -> Vec<Vec<String>>
Get all policy rules
Sourcepub async fn get_grouping_policy(&self) -> Vec<Vec<String>>
pub async fn get_grouping_policy(&self) -> Vec<Vec<String>>
Get all grouping rules (role assignments)
Sourcepub async fn save(&self) -> Result<(), PolicyError>
pub async fn save(&self) -> Result<(), PolicyError>
Save policies to disk
Sourcepub async fn reload(&self) -> Result<(), PolicyError>
pub async fn reload(&self) -> Result<(), PolicyError>
Reload policies from disk and re-inject base rules.
load_policy() wipes in-memory additions, so base rules must be
re-injected after every reload to guarantee they’re always present.
Sourcepub async fn apply_template(
&self,
template: &PolicyTemplate,
) -> Result<(), PolicyError>
pub async fn apply_template( &self, template: &PolicyTemplate, ) -> Result<(), PolicyError>
Apply a template’s policies and groupings to the enforcer, then persist to disk.
Uses the Casbin enforcer’s add_policies() + add_grouping_policies() APIs,
then calls save_policy() to write everything (including base rules) to disk.
Sourcepub fn policies_dir(&self) -> &Path
pub fn policies_dir(&self) -> &Path
Get the policies directory path
Sourcepub fn model_conf_path(&self) -> PathBuf
pub fn model_conf_path(&self) -> PathBuf
Get the model.conf path
Sourcepub fn policy_csv_path(&self) -> PathBuf
pub fn policy_csv_path(&self) -> PathBuf
Get the policy.csv path
Sourcepub async fn format_policy(&self) -> String
pub async fn format_policy(&self) -> String
Format policies as a human-readable string for display
Auto Trait Implementations§
impl Freeze for PolicyManager
impl !RefUnwindSafe for PolicyManager
impl Send for PolicyManager
impl Sync for PolicyManager
impl Unpin for PolicyManager
impl UnsafeUnpin for PolicyManager
impl !UnwindSafe for PolicyManager
Blanket Implementations§
§impl<T> AnySync for T
impl<T> AnySync for T
§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
§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
§impl<T> FutureExt for T
impl<T> FutureExt for T
§fn with_context(self, otel_cx: Context) -> WithContext<Self>
fn with_context(self, otel_cx: Context) -> WithContext<Self>
§fn with_current_context(self) -> WithContext<Self>
fn with_current_context(self) -> WithContext<Self>
§impl<T> Identity for Twhere
T: ?Sized,
impl<T> Identity for Twhere
T: ?Sized,
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§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 moreSource§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::Request