pub enum Ability<'a, M> {
}Expand description
One thing a policy is asked to allow; record abilities are asked once per loaded row, and a record that cannot be viewed cannot be written by guessing its key.
Variants§
ViewAny
Read the resource’s rows: asked by the list and its live refresh, the sidebar entry, the
export, related tables, relationship options and relation columns, and the global search.
All but the global search show every row of the resource’s query, asking no record
ability of it: a
policy cannot run in the database, so the rows a user must not see belong out of
ResourceDef::query.
View(&'a M)
Open one record: asked by the detail page, the edit page and POST, deletes, record pages and each global search result, which leaves out a record it refuses. Every write asks it of the records it loads, and a list row it refuses shows without its actions or checkbox.
Create
Open the create form and submit it: asked by the create page and POST, and the Create button.
Update(&'a M)
Edit one record: asked by the edit page and POST, and the row’s Edit action.
DeleteAny
Delete at all: asked by the delete and bulk-delete POSTs before any record loads, the Delete action and the bulk column.
Delete(&'a M)
Delete one record: asked of each record a delete removes, and by the row’s Delete action and checkbox.
RestoreAny
Restore trashed records at all, on a resource that soft-deletes: asked by the restore POSTs and the Restore buttons.
Restore(&'a M)
Restore one trashed record: asked of each record a restore brings back, and by its Restore button and checkbox.
ForceDeleteAny
Delete trashed records for good at all, on a resource that
soft-deletes. Allowed only with
DeleteAny. Asked by the force-delete POSTs and the Delete
permanently buttons.
ForceDelete(&'a M)
Delete one trashed record for good. Allowed only with Delete of it, so a
policy refusing a record’s delete refuses its force delete too. Asked of each trashed
record a force delete removes, and by its button and checkbox.
RunAny
Run the custom Action whose NAME is action
at all, before Run is asked of each record: asked by the action’s POSTs,
before any record loads, its buttons, and the bulk column for a bulk action.
Run
Run the custom Action whose NAME is action
on one record: asked of each record the action runs on, and by its button on the record’s
row and pages and, for a bulk action, the record’s checkbox.
RunHeader
Run the HeaderAction whose NAME is action from the list’s
header. It acts on no record, so nothing else of the policy is asked but ViewAny.
It is not RunAny: a policy that grants every record action with
RunAny { .. } => true grants no header action by it.
Implementations§
Source§impl<'a, M> Ability<'a, M>
impl<'a, M> Ability<'a, M>
Sourcepub fn written_record(self) -> Option<&'a M>
pub fn written_record(self) -> Option<&'a M>
The record the ability writes: an update, a delete, a restore, a force delete or a
record action’s run on it. None for a read and for an ability over no existing record:
Create, the _any abilities and RunHeader.
A policy refusing every write to some records reads it, and so also refuses the record
actions and soft-delete writes a resource gains later. A header action writes no record
it is handed, so such a policy allows it unless it matches RunHeader too:
fn keep_root(_cx: &Cx, ability: Ability<'_, User>) -> bool {
ability
.written_record()
.is_none_or(|user| user.name != "root")
}Source§impl<M> Ability<'_, M>
impl<M> Ability<'_, M>
Sourcepub fn is_action<A, R>(self) -> bool
pub fn is_action<A, R>(self) -> bool
Whether the ability is RunAny or Run for the action
A.
It compares against A::NAME, so renaming the action keeps the policy matching it, and a
type that is not an action of the resource does not compile. R is inferred when A is an
action of one resource.
fn post_policy(cx: &Cx, ability: Ability<'_, Post>) -> bool {
match ability {
Ability::Run { record, .. } if ability.is_action::<Publish, _>() => !record.published,
Ability::ViewAny | Ability::View(_) => true,
_ => is_editor(cx),
}
}Source§impl<M> Ability<'_, M>
impl<M> Ability<'_, M>
Sourcepub fn is_header_action<A: HeaderAction>(self) -> bool
pub fn is_header_action<A: HeaderAction>(self) -> bool
Whether the ability is RunHeader for the header action A, which
a resource’s policy answers to let the request run it.
fn post_policy(cx: &Cx, ability: Ability<'_, Post>) -> bool {
match ability {
_ if ability.is_header_action::<RecountTags>() => is_admin(cx),
Ability::ViewAny | Ability::View(_) => true,
_ => false,
}
}