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
Viewtogether withUpdate,DeleteorRun; - 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 refusedDeleteorRun; - running an action takes the policy and the action’s own
can_runtogether, so a panel that mounts the resource withReadOnlyoffers and runs none of its actions, andcananswers what the button shows; - a relationship choice asks the related resource’s policy for
ViewAnyandView; - 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
Loadvalueloadedanswers.
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§
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".