Skip to main content

Abilities

Struct Abilities 

Source
pub struct Abilities<M> { /* private fields */ }
Expand description

A policy that answers each Ability from the check declared for it, and denies every ability it declares none for.

A closure policy that ends in _ => true allows whatever it does not name, such as the restore a soft delete adds or an action declared later. Abilities allows only what it names:

let policy = Abilities::<Post>::new()
    .view_any(is_editor)
    .view(|_cx, _post| true)
    .create(is_editor)
    .update(|cx, post| is_editor(cx) && !post.locked);
// No delete, restore or action: each is refused until the policy declares it.

A write’s record check also allows the write at all: delete allows DeleteAny, restore allows RestoreAny, force_delete allows ForceDeleteAny, and run allows RunAny for its action, since the panel asks the record’s check before each of these writes. The _any methods narrow it; declared alone, they allow no record. view does not allow ViewAny: the list asks ViewAny alone and shows every row the resource’s query returns, so view_any is declared on its own.

Declaring an ability twice replaces its earlier check; combine the checks in one closure.

The model’s type is named on new, Abilities::<Post>::new(): a closure is checked before the chain learns the model from where the policy goes.

Implementations§

Source§

impl<M> Abilities<M>

Source

pub fn new() -> Self

A policy that denies every ability.

Source

pub fn view_any( self, check: impl Fn(&Cx) -> bool + Send + Sync + 'static, ) -> Self

Who may see the resource at all: ViewAny, which opens the list with every row of the resource’s query, the sidebar entry, the export, related tables, relationship options and header actions. Refused until declared, whether or not view is.

Source

pub fn view( self, check: impl Fn(&Cx, &M) -> bool + Send + Sync + 'static, ) -> Self

Who may see a record: View, asked by its detail page, its edit form, each write to it, the export and relationship options. The list shows a row without it, and asks it only to offer the row’s actions.

Source

pub fn create(self, check: impl Fn(&Cx) -> bool + Send + Sync + 'static) -> Self

Who may create a record: Create.

Source

pub fn update( self, check: impl Fn(&Cx, &M) -> bool + Send + Sync + 'static, ) -> Self

Who may edit a record: Update.

Source

pub fn delete_any( self, check: impl Fn(&Cx) -> bool + Send + Sync + 'static, ) -> Self

Who may delete at all: DeleteAny. Defaults to whether delete is declared.

Source

pub fn delete( self, check: impl Fn(&Cx, &M) -> bool + Send + Sync + 'static, ) -> Self

Who may delete a record: Delete.

Source

pub fn restore_any( self, check: impl Fn(&Cx) -> bool + Send + Sync + 'static, ) -> Self

Who may restore trashed records at all: RestoreAny. Defaults to whether restore is declared.

Source

pub fn restore( self, check: impl Fn(&Cx, &M) -> bool + Send + Sync + 'static, ) -> Self

Who may restore a trashed record: Restore.

Source

pub fn force_delete_any( self, check: impl Fn(&Cx) -> bool + Send + Sync + 'static, ) -> Self

Who may delete trashed records for good at all: ForceDeleteAny, which the panel also asks DeleteAny of. Defaults to whether force_delete is declared.

Source

pub fn force_delete( self, check: impl Fn(&Cx, &M) -> bool + Send + Sync + 'static, ) -> Self

Who may delete a trashed record for good: ForceDelete, which the panel also asks Delete of.

Source§

impl<M> Abilities<M>

Source

pub fn and<P>(self, other: P) -> And<Self, P>

Allows what both self and other allow.

Source

pub fn or<P>(self, other: P) -> Or<Self, P>

Allows what either self or other allows.

Source§

impl<M> Abilities<M>

Source

pub fn run<A, R>( self, check: impl Fn(&Cx, &M) -> bool + Send + Sync + 'static, ) -> Self
where A: Action<R>, R: Resource<Model = M>,

Who may run the record action A on a record: Run with A’s NAME. It also allows RunAny for A, which run_any narrows. An action no run declares is refused.

let policy = Abilities::<Post>::new()
    .view_any(|_cx| true)
    .view(|_cx, _post| true)
    .run::<Publish, _>(|_cx, post| !post.published);
Source

pub fn run_any<A, R>( self, check: impl Fn(&Cx) -> bool + Send + Sync + 'static, ) -> Self
where A: Action<R>, R: Resource<Model = M>,

Who may run the record action A at all: RunAny with A’s NAME. Defaults to whether run declares A.

Source§

impl<M> Abilities<M>

Source

pub fn run_header<A: HeaderAction>( self, check: impl Fn(&Cx) -> bool + Send + Sync + 'static, ) -> Self

Who may run the header action A from the list’s header: RunHeader with A’s NAME. A header action no run_header declares is refused.

Trait Implementations§

Source§

impl<M> Debug for Abilities<M>

Source§

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

Lists the declared abilities by method name; their checks are closures.

Source§

impl<M> Default for Abilities<M>

Source§

fn default() -> Self

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

impl<M: 'static> Policy<M> for Abilities<M>

Source§

fn allows(&self, cx: &Cx, ability: Ability<'_, M>) -> bool

Whether the current user may do ability.

Auto Trait Implementations§

§

impl<M> !RefUnwindSafe for Abilities<M>

§

impl<M> !UnwindSafe for Abilities<M>

§

impl<M> Forget for Abilities<M>

§

impl<M> Freeze for Abilities<M>
where Option<Box<dyn Fn(&Cx, &M) -> bool + Sync + Send>>: Freeze, Pair<M>: Freeze, Vec<(&'static str, Pair<M>)>: Freeze,

§

impl<M> Move for Abilities<M>

§

impl<M> Send for Abilities<M>
where Option<Box<dyn Fn(&Cx, &M) -> bool + Sync + Send>>: Send, Pair<M>: Send, Vec<(&'static str, Pair<M>)>: Send,

§

impl<M> Sync for Abilities<M>
where Option<Box<dyn Fn(&Cx, &M) -> bool + Sync + Send>>: Sync, Pair<M>: Sync, Vec<(&'static str, Pair<M>)>: Sync,

§

impl<M> Unpin for Abilities<M>
where Option<Box<dyn Fn(&Cx, &M) -> bool + Sync + Send>>: Unpin, Pair<M>: Unpin, Vec<(&'static str, Pair<M>)>: Unpin,

§

impl<M> UnsafeUnpin for Abilities<M>
where Option<Box<dyn Fn(&Cx, &M) -> bool + Sync + Send>>: UnsafeUnpin, Pair<M>: UnsafeUnpin, Vec<(&'static str, Pair<M>)>: UnsafeUnpin,

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more