Reading the ability
current_ability and Repo::can — the caller's ability in a check of your own — and every method an Ability exposes.
The framework reads the caller’s ability at the query, the binding and the wire row. A service reads it too, when a decision of its own needs it.
Reading the ambient ability
Section titled “Reading the ambient ability”A query of a service’s own starts from Repo::scoped, which joins the
Condition for it (a custom query).
A service method or any code a unit naming its read reaches — a dataloader
batch included — reads the ability directly:
use nest_rs::authz::current_ability;use nest_rs::authz::Action;
if let Some(ab) = current_ability() { if !ab.can::<user::Entity>(Action::Update, &model) { return Err(ServiceError::forbidden("not permitted")); }}can decides on the model alone: synchronous, no query. What the
model cannot show — a relation (p.related(…)) above all — grants
nothing there, and a denial resting on it refuses.
So a relation-gated Update grant answers false on a row you read for
Read. When such a rule decides, ask SQL: Repo::can re-checks the
row’s primary key under the action’s own condition, one indexed query.
use nest_rs::seaorm::Repo;use nest_rs::authz::Action;
if !Repo::<user::Entity>::can(Action::Update, &model).await? { return Err(ServiceError::forbidden("not permitted"));}On an entity declared soft_delete, a deleted row answers false for
every action but Create and Delete, as every Repo read leaves it out;
Delete stays true where the grant holds, since the hard purge reaches it.
A row a route bound through #[authorize(Update, bind = S)] was already
authorized for Update in SQL, and needs neither check.
current_ability() returns None outside a request (worker job, cron
tick, init or shutdown hook). That work runs unscoped by design — system
work should not be filtered by a caller’s ability. See
Threat model for what that buys and what
it does not.
It returns None as well on a unit whose posture names no read, every #[sse]
stream among them, and for a visitor anywhere but a unit naming what it reads: the ability is
withheld there, so a Repo read fails closed. A check of your own that must hold
on such a unit asks Repo::can, which refuses there, rather than skipping on None.
What an Ability exposes
Section titled “What an Ability exposes”ability.can_class(Action::Read, TypeId::of::<user::Entity>())ability.condition_for::<user::Entity>(Action::Read)ability.condition_for_columns::<user::Entity>(Action::Update, &[user::Column::Name])?ability.can::<user::Entity>(Action::Update, &model)Repo::<user::Entity>::can(Action::Update, &model).await?ability.permitted_fields::<user::Entity>(Action::Read, &model)Each method targets one layer of enforcement — the framework picks
which one to call where, but the surface is documented because your
own services may reach for can directly when they cannot fit through
Repo or a bound subject. Repo::can is the one that reads the database: it
answers what Ability::can cannot see on the model.
Action::Manage is the wildcard: a grant on Manage matches every
other action. A grant on Action::Read does not imply Update. Asked of a
site, Manage means every action at once: can_class and can hold each of
the four, condition_for conjoins their conditions, and a wire row and
permitted_fields read Read’s
(Policies).
Going further
Section titled “Going further”- Authorization — the engine overview.
- Row-level filtering —
what
Repo::scopedjoins for a query of your own. - Policies — the rules an
Abilityholds.