Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Abilities

An ability is one act a module declares by name, with a typed input and a permission, for any caller to run as the signed-in person.

Declare an ability

use laterite_admin::ability::{Ability, AbilityCx, AbilityError, AbilityHandler, Effect, Link, Outcome};
use laterite_admin::AdminRegistry;
use laterite_core::serde_json::Value;
use laterite_core::t;
use std::sync::Arc;

#[derive(serde::Deserialize, schemars::JsonSchema)]
struct AddLead {
    /// The person's full name.
    name: String,
    /// A phone number with its country code.
    phone: Option<String>,
}

struct AddLeadHandler;

#[laterite_core::strata::async_trait]
impl AbilityHandler for AddLeadHandler {
    async fn run(&self, cx: &AbilityCx<'_>, input: Value) -> Result<Outcome, AbilityError> {
        let lead: AddLead = laterite_core::serde_json::from_value(input)
            .map_err(|e| AbilityError::invalid(e.to_string()))?;
        let id = acme::leads::add(cx.db(), &lead.name, lead.phone.as_deref(), cx.actor())
            .await
            .map_err(|e| AbilityError::Failed(e.to_string()))?;
        Ok(Outcome::new(t!("Added the lead."))
            .link(Link::new(lead.name, format!("/admin/acme/crm/leads/{id}/edit"))))
    }
}

fn register(&self, registry: &mut laterite_core::Registry) {
    registry.add_ability(
        Ability::new("acme.crm.add_lead", t!("Add a lead"), Arc::new(AddLeadHandler))
            .describe(t!("Adds a person to the leads list."))
            .permission("acme.crm.leads.create")
            .input::<AddLead>()
            .sensitive(["phone"])
            .effect(Effect::Create),
    );
}
MethodSetsDefault
new(code, label, handler)the dotted code with the module’s id first, the name people see, the code behind itrequired
describewhat it does and when to use it, for a caller choosing among abilitiesnone
groupthe shelf it sits on in a cataloguethe code without its last segment
permissionthe permission a person needs to see and run itopen to every operator
input::<T>()the input as a type deriving schemars::JsonSchemano input
input_schema(value)the input as a JSON Schema objectno input
effectwhat running it doesEffect::Run
untrusted()marks the result as carrying text written by people outside the paneltrusted
sensitive([..])input fields left out of the audit entrynone
EffectMeaning
Readanswers a question and changes nothing
Createadds a record
Updatechanges a record
Deleteremoves a record
Sendsends something to a person outside the panel
Runany other change: publishing, importing, a rebuild

An ability with an effect other than Read names a permission. A doc comment on an input field is that field’s description in the schema.

Say what it would do

use laterite_admin::ability::{Draft, Preview};

async fn preview(&self, cx: &AbilityCx<'_>, input: &Value) -> Result<Preview, AbilityError> {
    let version = acme::documents::draft_version(cx.db(), input).await?;
    Ok(Preview::new(t!("Publishes the draft of Terms of Use."))
        .record(Link::new(version.title, version.href))
        .count(1))
}
MethodSets
Preview::new(summary)the act in a sentence
record(link)a record the act touches
count(n)how many records it touches
draft(Draft::new(path, id, values))a resource’s form, filled in, for the person to save

Answer from the handler

ReturnWhen
Ok(Outcome::new(message))it ran; add .link(..) for records and .data(..) for a result as JSON
Ok(Outcome::new(message).go(href))it ran, and the person is taken to href, a path on the site
Err(AbilityError::Refused(text))it declined, with a reason the person reads
Err(AbilityError::invalid(message))the input is wrong in a way the schema cannot state
Err(AbilityError::Failed(message))it broke; the message goes to the log

AbilityCx gives db(), ctx(), user() and actor(). Pass actor() to FormHandle::create and FormHandle::update, and to AuditEntry::new.

Run an ability

use laterite_core::serde_json::json;

let abilities = ctx.abilities();

for ability in abilities.for_user(&user) {
    println!("{} ({})", ability.code(), ability.effect_kind().as_str());
}
let found = abilities.search(&user, "add a lead", 5);

let preview = abilities
    .preview(&user, "acme.crm.add_lead", &json!({ "name": "Asha Rao" }), None)
    .await?;
let outcome = abilities
    .run(&user, "acme.crm.add_lead", json!({ "name": "Asha Rao" }), Some("acme.assistant"))
    .await?;
MethodAnswers
for_user(&user)every ability the person may run
get(&user, code)one ability, when the person may run it
search(&user, words, limit)the abilities matching the words, best first
preview(&user, code, &input, channel)the handler’s Preview
run(&user, code, input, channel)the handler’s Outcome

channel names what acts for the person (acme.assistant), or None. The Audit Log shows it under Through.

ErrorCause
AbilityError::Unknown(code)no ability has the code, or the person lacks its permission
AbilityError::Invalid(faults)the input does not fit the schema; one message per fault

A run with an effect other than Read writes an ability.run audit entry.

Act for a person

use laterite_admin::ability::Confirm;

let abilities = ctx.abilities();
const CHANNEL: &str = "acme.assistant";

for (ability, confirm) in abilities.offered(&user).await? {
    println!("{} ({})", ability.code(), confirm.as_str());
}

match abilities.confirm(&user, "acme.crm.add_lead").await? {
    Confirm::Auto => {
        abilities.run(&user, "acme.crm.add_lead", input, Some(CHANNEL)).await?;
    }
    _ => {
        let preview = abilities
            .preview(&user, "acme.crm.add_lead", &input, Some(CHANNEL))
            .await?;
        // Show the preview; run once the person agrees.
    }
}
AnswerMeansDefault for
Confirm::Autorun without askingEffect::Read
Confirm::Askshow the preview, run when the person agreesevery other effect
Confirm::Offnot offered: confirm, preview and run answer AbilityError::UnknownEffect::Delete, Effect::Send

A call with channel set to None is the person themself and is not asked.

use laterite_admin::ability::{Confirm, OwnPolicy, PanelPolicy, PolicyRule};
use laterite_admin::settings::store;

store::save(ctx.db(), &PanelPolicy {
    rules: vec![
        PolicyRule::new("effect:delete", Confirm::Ask),
        PolicyRule::new("acme.crm", Confirm::Ask),
        PolicyRule::new("acme.crm.add_lead", Confirm::Auto),
    ],
}).await?;

store::save_for(ctx.db(), user.user.id, &OwnPolicy {
    rules: vec![PolicyRule::new("acme.crm.add_lead", Confirm::Ask)],
}).await?;
RuleCoversWins over
acme.crm.add_leadthat abilityevery other rule
acme.crmevery ability whose code starts acme.crm.a shorter start, an effect
effect:createevery ability with that effectthe default
Set byWhereReach
An administrator holding backend.manage_abilitiesSettings, Abilities (PanelPolicy)the furthest anyone goes
Each personPreferences, Acting for me (OwnPolicy)tighter than the panel’s rule, or as loose as it and no looser

A run through a channel records its answer as confirm in the audit entry.

Offer a resource’s records

entity: posts
path: /posts
title: Posts
permission: acme.posts.manage
abilities:
  describe: Blog posts. A post has a title, a body in markdown and a publish date.
  update: false
  hide: [internal_notes]
use laterite_admin::records::RecordAbilities;

Resource::new("/posts", "Posts", list)
    .abilities(RecordAbilities::new().describe(t!("Blog posts.")).hide(["internal_notes"]));
KeySetsDefault
abilities: falsekeeps the resource out of every record abilityoffered
describewhat the records are, for a caller choosing where to looknone
createa new record may be draftedtrue, where the list is creatable
updatea change may be draftedtrue
deletea record may be deletedtrue, where the list is deletable
hidefields and columns kept from a callernone
untrustedthe records carry text written by people outside the panelfalse
AbilityInputAnswers
backend.records.kindsresource (optional)the kinds open to the person; one kind with its fields, columns, filters and sorts
backend.records.findresource, search, filters, sort, dir, limitmatching rows with their ids, 25 at most
backend.records.openresource, search, filters, sort, dirthe list’s address with all of it set, as Outcome::go
backend.records.readresource, idone record’s field values
backend.records.createresource, valuespreview: the filled form as a Draft; run: the new id
backend.records.updateresource, id, valuespreview: the changed form as a Draft; run: the id
backend.records.deleteresource, idpreview: the record named; run: the id, moved to the trash on a soft_delete resource
{ "resource": "/posts", "filters": { "status": "live", "views_min": 1000 }, "sort": "views", "dir": "desc" }

filters names the list’s own filters, the ones the person may use. resource is the resource’s path. Each ability asks for the resource’s permission, and a create or an update for its permissions.create or permissions.update, and a delete for permissions.delete. password and sensitive fields are never read or set.

Act for a person in your own writes

use laterite_core::Actor;

let actor = Actor::from(&user).through("acme.assistant");
form.create(values, &actor).await?;

Actor::channel() reads it back in a model listener.