Struct RoleBuilder

Source
pub struct RoleBuilder { /* private fields */ }
Expand description

Builder for creating roles with a fluent API.

Implementations§

Source§

impl RoleBuilder

Source

pub fn new() -> Self

Create a new role builder.

Source

pub fn name(self, name: impl Into<String>) -> Self

Set the role name.

Source

pub fn description(self, description: impl Into<String>) -> Self

Set the role description.

Source

pub fn permission(self, permission: Permission) -> Self

Add a permission to the role.

Source

pub fn permissions( self, permissions: impl IntoIterator<Item = Permission>, ) -> Self

Add multiple permissions to the role.

Source

pub fn allow<I, S>(self, resource: impl Into<String>, actions: I) -> Self
where I: IntoIterator<Item = S>, S: Into<String>,

Add permissions with fluent allow syntax.

§Example
use role_system::role::RoleBuilder;
let role = RoleBuilder::new()
    .name("admin")
    .allow("users", ["create", "read", "update", "delete"])
    .allow("roles", ["read", "assign"])
    .build().unwrap();
Source

pub fn deny<I, S>(self, resource: impl Into<String>, actions: I) -> Self
where I: IntoIterator<Item = S>, S: Into<String>,

Add permissions with deny semantics (creates negative conditions).

§Example
use role_system::role::RoleBuilder;
let role = RoleBuilder::new()
    .name("restricted_admin")
    .allow("users", ["read", "update"])
    .deny("system", ["shutdown"])
    .build().unwrap();
Source

pub fn allow_when<I, S, F>( self, resource: impl Into<String>, actions: I, condition: F, ) -> Self
where I: IntoIterator<Item = S>, S: Into<String>, F: Fn(&HashMap<String, String>) -> bool + Send + Sync + 'static + Clone,

Add permissions with conditional access.

§Example
use role_system::role::RoleBuilder;
let role = RoleBuilder::new()
    .name("user")
    .allow_when("profile", ["update"], |ctx|
        ctx.get("user_id") == ctx.get("target_id"))
    .build().unwrap();
Source

pub fn metadata(self, key: impl Into<String>, value: impl Into<String>) -> Self

Add metadata to the role.

Source

pub fn active(self, active: bool) -> Self

Set whether the role is active.

Source

pub fn build(self) -> Result<Role>

Build the role.

Trait Implementations§

Source§

impl Debug for RoleBuilder

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for RoleBuilder

Source§

fn default() -> RoleBuilder

Returns the “default value” for a type. Read more

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, 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, U> TryFrom<U> for T
where U: Into<T>,

Source§

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>,

Source§

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.