Skip to main content

Policy

Trait Policy 

Source
pub trait Policy<M>:
    Send
    + Sync
    + 'static {
    // Required method
    fn allows(&self, cx: &Cx, ability: Ability<'_, M>) -> bool;
}
Expand description

Decides which Abilitys the current user has over a resource’s records.

Every handler asks the same abilities that decide which buttons render, so a hidden button is also a refused request, answered 403:

  • a record is viewed before it is written: the edit, delete and action handlers ask View together with Update, Delete or Run;
  • a write is checked against the stored row: the update, delete and action handlers load the record inside the write’s transaction and ask about that row, not the submitted key. A bulk delete or bulk action fails as a whole when any selected record is refused View, and skips the records refused Delete or Run;
  • running an action takes the policy and the action’s own can_run together, so a panel that mounts the resource with ReadOnly offers and runs none of its actions, and can answers what the button shows;
  • a relationship choice asks the related resource’s policy for ViewAny and View;
  • a policy is asked once per row and once per button, so it cannot query: the panel reads what it needs once for the request, as a Load value loaded answers.

A closure |cx: &Cx, ability: Ability<'_, M>| -> bool is a policy, and the building blocks combine with and and or. ResourceDef::policy_fn takes a closure without naming its argument types.

Required Methods§

Source

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

Whether the current user may do ability.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§

Source§

impl<M, A: Policy<M>, B: Policy<M>> Policy<M> for And<A, B>

Source§

impl<M, A: Policy<M>, B: Policy<M>> Policy<M> for Or<A, B>

Source§

impl<M, F> Policy<M> for F
where F: Fn(&Cx, Ability<'_, M>) -> bool + Send + Sync + 'static,

Source§

impl<M, F> Policy<M> for When<F>
where F: Fn(&Cx) -> bool + Send + Sync + 'static,

Source§

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

Source§

impl<M> Policy<M> for Allow

Source§

impl<M> Policy<M> for Deny

Source§

impl<M> Policy<M> for ReadOnly