Struct casbin::Enforcer

source ·
pub struct Enforcer { /* private fields */ }
Expand description

Enforcer is the main interface for authorization enforcement and policy management.

Trait Implementations§

source§

impl CoreApi for Enforcer

source§

fn enforce<ARGS: EnforceArgs>(&self, rvals: ARGS) -> Result<bool>

Enforce decides whether a “subject” can access a “object” with the operation “action”, input parameters are usually: (sub, obj, act).

§Examples
use casbin::prelude::*;
#[cfg(feature = "runtime-async-std")]
#[async_std::main]
async fn main() -> Result<()> {
    let mut e = Enforcer::new("examples/basic_model.conf", "examples/basic_policy.csv").await?;
    assert_eq!(true, e.enforce(("alice", "data1", "read"))?);
    Ok(())
}

#[cfg(feature = "runtime-tokio")]
#[tokio::main]
async fn main() -> Result<()> {
    let mut e = Enforcer::new("examples/basic_model.conf", "examples/basic_policy.csv").await?;
    assert_eq!(true, e.enforce(("alice", "data1", "read"))?);

    Ok(())
}
#[cfg(all(not(feature = "runtime-async-std"), not(feature = "runtime-tokio")))]
fn main() {}
source§

fn enforce_with_context<ARGS: EnforceArgs>( &self, ctx: EnforceContext, rvals: ARGS ) -> Result<bool>

Enforce decides whether a “subject” can access a “object” with the operation “action”, input parameters are usually: (sub, obj, act). this function will add suffix to each model eg. r2, p2, e2, m2, g2,

§Examples
use casbin::prelude::*;
use casbin::EnforceContext;

#[cfg(feature = "runtime-async-std")]
#[async_std::main]
async fn main() -> Result<()> {
    let mut e = Enforcer::new("examples/multi_section_model.conf", "examples/multi_section_policy.csv").await?;
    assert_eq!(true, e.enforce(("alice", "read", "project1"))?);
    let ctx = EnforceContext::new("2");
    assert_eq!(true, e.enforce_with_context(ctx, ("james", "execute"))?);
    Ok(())
}

#[cfg(feature = "runtime-tokio")]
#[tokio::main]
async fn main() -> Result<()> {
    let mut e = Enforcer::new("examples/multi_section_model.conf", "examples/multi_section_policy.csv").await?;
    assert_eq!(true, e.enforce(("alice", "read", "project1"))?);
    let ctx = EnforceContext::new("2");
    assert_eq!(true, e.enforce_with_context(ctx, ("james", "execute"))?);

    Ok(())
}
#[cfg(all(not(feature = "runtime-async-std"), not(feature = "runtime-tokio")))]
fn main() {}
source§

fn new_raw<'async_trait, M, A>( m: M, a: A ) -> Pin<Box<dyn Future<Output = Result<Self>> + Send + 'async_trait>>
where M: 'async_trait + TryIntoModel, A: 'async_trait + TryIntoAdapter, Self: 'async_trait,

source§

fn new<'async_trait, M, A>( m: M, a: A ) -> Pin<Box<dyn Future<Output = Result<Self>> + Send + 'async_trait>>
where M: 'async_trait + TryIntoModel, A: 'async_trait + TryIntoAdapter, Self: 'async_trait,

source§

fn add_function( &mut self, fname: &str, f: fn(_: ImmutableString, _: ImmutableString) -> bool )

source§

fn get_model(&self) -> &dyn Model

source§

fn get_mut_model(&mut self) -> &mut dyn Model

source§

fn get_adapter(&self) -> &dyn Adapter

source§

fn get_mut_adapter(&mut self) -> &mut dyn Adapter

source§

fn get_role_manager(&self) -> Arc<RwLock<dyn RoleManager>>

source§

fn set_role_manager(&mut self, rm: Arc<RwLock<dyn RoleManager>>) -> Result<()>

source§

fn set_model<'life0, 'async_trait, M>( &'life0 mut self, m: M ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where M: 'async_trait + TryIntoModel, Self: 'async_trait, 'life0: 'async_trait,

source§

fn set_adapter<'life0, 'async_trait, A>( &'life0 mut self, a: A ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where A: 'async_trait + TryIntoAdapter, Self: 'async_trait, 'life0: 'async_trait,

source§

fn set_effector(&mut self, e: Box<dyn Effector>)

source§

fn enforce_mut<ARGS: EnforceArgs>(&mut self, rvals: ARGS) -> Result<bool>

source§

fn load_policy<'life0, 'async_trait>( &'life0 mut self ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

source§

fn load_filtered_policy<'a, 'life0, 'async_trait>( &'life0 mut self, f: Filter<'a> ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'a: 'async_trait, 'life0: 'async_trait,

source§

fn is_filtered(&self) -> bool

source§

fn is_enabled(&self) -> bool

source§

fn save_policy<'life0, 'async_trait>( &'life0 mut self ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

source§

fn clear_policy<'life0, 'async_trait>( &'life0 mut self ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

source§

fn enable_enforce(&mut self, enabled: bool)

source§

fn enable_auto_save(&mut self, auto_save: bool)

source§

fn has_auto_save_enabled(&self) -> bool

source§

impl EventEmitter<Event> for Enforcer

source§

fn on(&mut self, e: Event, f: fn(_: &mut Self, _: EventData))

source§

fn off(&mut self, e: Event)

source§

fn emit(&mut self, e: Event, d: EventData)

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> InternalApi for T
where T: IEnforcer,

source§

fn add_policy_internal<'life0, 'life1, 'life2, 'async_trait>( &'life0 mut self, sec: &'life1 str, ptype: &'life2 str, rule: Vec<String> ) -> Pin<Box<dyn Future<Output = Result<bool, Error>> + Send + 'async_trait>>
where 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait, T: 'async_trait,

source§

fn add_policies_internal<'life0, 'life1, 'life2, 'async_trait>( &'life0 mut self, sec: &'life1 str, ptype: &'life2 str, rules: Vec<Vec<String>> ) -> Pin<Box<dyn Future<Output = Result<bool, Error>> + Send + 'async_trait>>
where 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait, T: 'async_trait,

source§

fn remove_policy_internal<'life0, 'life1, 'life2, 'async_trait>( &'life0 mut self, sec: &'life1 str, ptype: &'life2 str, rule: Vec<String> ) -> Pin<Box<dyn Future<Output = Result<bool, Error>> + Send + 'async_trait>>
where 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait, T: 'async_trait,

source§

fn remove_policies_internal<'life0, 'life1, 'life2, 'async_trait>( &'life0 mut self, sec: &'life1 str, ptype: &'life2 str, rules: Vec<Vec<String>> ) -> Pin<Box<dyn Future<Output = Result<bool, Error>> + Send + 'async_trait>>
where 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait, T: 'async_trait,

source§

fn remove_filtered_policy_internal<'life0, 'life1, 'life2, 'async_trait>( &'life0 mut self, sec: &'life1 str, ptype: &'life2 str, field_index: usize, field_values: Vec<String> ) -> Pin<Box<dyn Future<Output = Result<(bool, Vec<Vec<String>>), Error>> + Send + 'async_trait>>
where 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait, T: 'async_trait,

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> MgmtApi for T
where T: InternalApi,

source§

fn add_named_policy<'life0, 'life1, 'async_trait>( &'life0 mut self, ptype: &'life1 str, params: Vec<String> ) -> Pin<Box<dyn Future<Output = Result<bool, Error>> + Send + 'async_trait>>
where 'life0: 'async_trait, 'life1: 'async_trait, T: 'async_trait,

source§

fn add_named_policies<'life0, 'life1, 'async_trait>( &'life0 mut self, ptype: &'life1 str, paramss: Vec<Vec<String>> ) -> Pin<Box<dyn Future<Output = Result<bool, Error>> + Send + 'async_trait>>
where 'life0: 'async_trait, 'life1: 'async_trait, T: 'async_trait,

source§

fn remove_named_policy<'life0, 'life1, 'async_trait>( &'life0 mut self, ptype: &'life1 str, params: Vec<String> ) -> Pin<Box<dyn Future<Output = Result<bool, Error>> + Send + 'async_trait>>
where 'life0: 'async_trait, 'life1: 'async_trait, T: 'async_trait,

source§

fn remove_named_policies<'life0, 'life1, 'async_trait>( &'life0 mut self, ptype: &'life1 str, paramss: Vec<Vec<String>> ) -> Pin<Box<dyn Future<Output = Result<bool, Error>> + Send + 'async_trait>>
where 'life0: 'async_trait, 'life1: 'async_trait, T: 'async_trait,

source§

fn add_named_grouping_policy<'life0, 'life1, 'async_trait>( &'life0 mut self, ptype: &'life1 str, params: Vec<String> ) -> Pin<Box<dyn Future<Output = Result<bool, Error>> + Send + 'async_trait>>
where 'life0: 'async_trait, 'life1: 'async_trait, T: 'async_trait,

source§

fn add_named_grouping_policies<'life0, 'life1, 'async_trait>( &'life0 mut self, ptype: &'life1 str, paramss: Vec<Vec<String>> ) -> Pin<Box<dyn Future<Output = Result<bool, Error>> + Send + 'async_trait>>
where 'life0: 'async_trait, 'life1: 'async_trait, T: 'async_trait,

source§

fn remove_named_grouping_policy<'life0, 'life1, 'async_trait>( &'life0 mut self, ptype: &'life1 str, params: Vec<String> ) -> Pin<Box<dyn Future<Output = Result<bool, Error>> + Send + 'async_trait>>
where 'life0: 'async_trait, 'life1: 'async_trait, T: 'async_trait,

source§

fn remove_named_grouping_policies<'life0, 'life1, 'async_trait>( &'life0 mut self, ptype: &'life1 str, paramss: Vec<Vec<String>> ) -> Pin<Box<dyn Future<Output = Result<bool, Error>> + Send + 'async_trait>>
where 'life0: 'async_trait, 'life1: 'async_trait, T: 'async_trait,

source§

fn remove_filtered_named_grouping_policy<'life0, 'life1, 'async_trait>( &'life0 mut self, ptype: &'life1 str, field_index: usize, field_values: Vec<String> ) -> Pin<Box<dyn Future<Output = Result<bool, Error>> + Send + 'async_trait>>
where 'life0: 'async_trait, 'life1: 'async_trait, T: 'async_trait,

source§

fn remove_filtered_named_policy<'life0, 'life1, 'async_trait>( &'life0 mut self, ptype: &'life1 str, field_index: usize, field_values: Vec<String> ) -> Pin<Box<dyn Future<Output = Result<bool, Error>> + Send + 'async_trait>>
where 'life0: 'async_trait, 'life1: 'async_trait, T: 'async_trait,

source§

fn get_named_policy(&self, ptype: &str) -> Vec<Vec<String>>

source§

fn get_all_policy(&self) -> Vec<Vec<String>>

source§

fn get_filtered_named_policy( &self, ptype: &str, field_index: usize, field_values: Vec<String> ) -> Vec<Vec<String>>

source§

fn has_named_policy(&self, ptype: &str, params: Vec<String>) -> bool

source§

fn get_named_grouping_policy(&self, ptype: &str) -> Vec<Vec<String>>

source§

fn get_all_grouping_policy(&self) -> Vec<Vec<String>>

source§

fn get_filtered_named_grouping_policy( &self, ptype: &str, field_index: usize, field_values: Vec<String> ) -> Vec<Vec<String>>

source§

fn has_grouping_named_policy(&self, ptype: &str, params: Vec<String>) -> bool

source§

fn get_all_named_subjects(&self, ptype: &str) -> Vec<String>

source§

fn get_all_named_objects(&self, ptype: &str) -> Vec<String>

source§

fn get_all_named_actions(&self, ptype: &str) -> Vec<String>

source§

fn get_all_named_roles(&self, ptype: &str) -> Vec<String>

source§

fn add_policy<'life0, 'async_trait>( &'life0 mut self, params: Vec<String> ) -> Pin<Box<dyn Future<Output = Result<bool>> + Send + 'async_trait>>
where Self: Send + 'async_trait, 'life0: 'async_trait,

source§

fn add_policies<'life0, 'async_trait>( &'life0 mut self, paramss: Vec<Vec<String>> ) -> Pin<Box<dyn Future<Output = Result<bool>> + Send + 'async_trait>>
where Self: Send + 'async_trait, 'life0: 'async_trait,

source§

fn remove_policy<'life0, 'async_trait>( &'life0 mut self, params: Vec<String> ) -> Pin<Box<dyn Future<Output = Result<bool>> + Send + 'async_trait>>
where Self: Send + 'async_trait, 'life0: 'async_trait,

source§

fn remove_policies<'life0, 'async_trait>( &'life0 mut self, paramss: Vec<Vec<String>> ) -> Pin<Box<dyn Future<Output = Result<bool>> + Send + 'async_trait>>
where Self: Send + 'async_trait, 'life0: 'async_trait,

source§

fn add_grouping_policy<'life0, 'async_trait>( &'life0 mut self, params: Vec<String> ) -> Pin<Box<dyn Future<Output = Result<bool>> + Send + 'async_trait>>
where Self: Send + 'async_trait, 'life0: 'async_trait,

source§

fn add_grouping_policies<'life0, 'async_trait>( &'life0 mut self, paramss: Vec<Vec<String>> ) -> Pin<Box<dyn Future<Output = Result<bool>> + Send + 'async_trait>>
where Self: Send + 'async_trait, 'life0: 'async_trait,

source§

fn remove_grouping_policy<'life0, 'async_trait>( &'life0 mut self, params: Vec<String> ) -> Pin<Box<dyn Future<Output = Result<bool>> + Send + 'async_trait>>
where Self: Send + 'async_trait, 'life0: 'async_trait,

source§

fn remove_grouping_policies<'life0, 'async_trait>( &'life0 mut self, paramss: Vec<Vec<String>> ) -> Pin<Box<dyn Future<Output = Result<bool>> + Send + 'async_trait>>
where Self: Send + 'async_trait, 'life0: 'async_trait,

source§

fn remove_filtered_policy<'life0, 'async_trait>( &'life0 mut self, field_index: usize, field_values: Vec<String> ) -> Pin<Box<dyn Future<Output = Result<bool>> + Send + 'async_trait>>
where Self: Send + 'async_trait, 'life0: 'async_trait,

source§

fn remove_filtered_grouping_policy<'life0, 'async_trait>( &'life0 mut self, field_index: usize, field_values: Vec<String> ) -> Pin<Box<dyn Future<Output = Result<bool>> + Send + 'async_trait>>
where Self: Send + 'async_trait, 'life0: 'async_trait,

source§

fn get_policy(&self) -> Vec<Vec<String>>

source§

fn get_filtered_policy( &self, field_index: usize, field_values: Vec<String> ) -> Vec<Vec<String>>

source§

fn has_policy(&self, params: Vec<String>) -> bool

source§

fn get_grouping_policy(&self) -> Vec<Vec<String>>

source§

fn get_filtered_grouping_policy( &self, field_index: usize, field_values: Vec<String> ) -> Vec<Vec<String>>

source§

fn has_grouping_policy(&self, params: Vec<String>) -> bool

source§

fn get_all_subjects(&self) -> Vec<String>

source§

fn get_all_objects(&self) -> Vec<String>

source§

fn get_all_actions(&self) -> Vec<String>

source§

fn get_all_roles(&self) -> Vec<String>

source§

impl<T> RbacApi for T
where T: MgmtApi,

source§

fn add_permission_for_user<'life0, 'life1, 'async_trait>( &'life0 mut self, user: &'life1 str, permission: Vec<String> ) -> Pin<Box<dyn Future<Output = Result<bool, Error>> + Send + 'async_trait>>
where 'life0: 'async_trait, 'life1: 'async_trait, T: 'async_trait,

source§

fn add_permissions_for_user<'life0, 'life1, 'async_trait>( &'life0 mut self, user: &'life1 str, permissions: Vec<Vec<String>> ) -> Pin<Box<dyn Future<Output = Result<bool, Error>> + Send + 'async_trait>>
where 'life0: 'async_trait, 'life1: 'async_trait, T: 'async_trait,

source§

fn add_role_for_user<'life0, 'life1, 'life2, 'life3, 'async_trait>( &'life0 mut self, user: &'life1 str, role: &'life2 str, domain: Option<&'life3 str> ) -> Pin<Box<dyn Future<Output = Result<bool, Error>> + Send + 'async_trait>>
where 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait, 'life3: 'async_trait, T: 'async_trait,

source§

fn add_roles_for_user<'life0, 'life1, 'life2, 'async_trait>( &'life0 mut self, user: &'life1 str, roles: Vec<String>, domain: Option<&'life2 str> ) -> Pin<Box<dyn Future<Output = Result<bool, Error>> + Send + 'async_trait>>
where 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait, T: 'async_trait,

source§

fn delete_role_for_user<'life0, 'life1, 'life2, 'life3, 'async_trait>( &'life0 mut self, user: &'life1 str, role: &'life2 str, domain: Option<&'life3 str> ) -> Pin<Box<dyn Future<Output = Result<bool, Error>> + Send + 'async_trait>>
where 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait, 'life3: 'async_trait, T: 'async_trait,

source§

fn delete_roles_for_user<'life0, 'life1, 'life2, 'async_trait>( &'life0 mut self, user: &'life1 str, domain: Option<&'life2 str> ) -> Pin<Box<dyn Future<Output = Result<bool, Error>> + Send + 'async_trait>>
where 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait, T: 'async_trait,

source§

fn get_roles_for_user( &mut self, name: &str, domain: Option<&str> ) -> Vec<String>

source§

fn get_users_for_role(&self, name: &str, domain: Option<&str>) -> Vec<String>

source§

fn has_role_for_user( &mut self, name: &str, role: &str, domain: Option<&str> ) -> bool

source§

fn delete_user<'life0, 'life1, 'async_trait>( &'life0 mut self, name: &'life1 str ) -> Pin<Box<dyn Future<Output = Result<bool, Error>> + Send + 'async_trait>>
where 'life0: 'async_trait, 'life1: 'async_trait, T: 'async_trait,

source§

fn delete_role<'life0, 'life1, 'async_trait>( &'life0 mut self, name: &'life1 str ) -> Pin<Box<dyn Future<Output = Result<bool, Error>> + Send + 'async_trait>>
where 'life0: 'async_trait, 'life1: 'async_trait, T: 'async_trait,

source§

fn delete_permission_for_user<'life0, 'life1, 'async_trait>( &'life0 mut self, user: &'life1 str, permission: Vec<String> ) -> Pin<Box<dyn Future<Output = Result<bool, Error>> + Send + 'async_trait>>
where 'life0: 'async_trait, 'life1: 'async_trait, T: 'async_trait,

source§

fn get_permissions_for_user( &self, user: &str, domain: Option<&str> ) -> Vec<Vec<String>>

source§

fn has_permission_for_user(&self, user: &str, permission: Vec<String>) -> bool

source§

fn get_implicit_roles_for_user( &mut self, name: &str, domain: Option<&str> ) -> Vec<String>

source§

fn get_implicit_permissions_for_user( &mut self, user: &str, domain: Option<&str> ) -> Vec<Vec<String>>

source§

fn get_implicit_users_for_permission<'life0, 'async_trait>( &'life0 self, permission: Vec<String> ) -> Pin<Box<dyn Future<Output = Vec<String>> + Send + 'async_trait>>
where 'life0: 'async_trait, T: 'async_trait,

source§

fn delete_permission<'life0, 'async_trait>( &'life0 mut self, permission: Vec<String> ) -> Pin<Box<dyn Future<Output = Result<bool>> + Send + 'async_trait>>
where Self: Send + 'async_trait, 'life0: 'async_trait,

source§

fn delete_permissions_for_user<'life0, 'life1, 'async_trait>( &'life0 mut self, user: &'life1 str ) -> Pin<Box<dyn Future<Output = Result<bool>> + Send + 'async_trait>>
where Self: Send + 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> IEnforcer for T