Bound mutations
#[authorize(Action, bind = Service)] on an operation: the row a caller names by id, loaded under its grant, authorized and handed over as proof.
A bound mutation acts on one existing row the caller already named by
id — publish, archive, confirm. It declares its posture and the row it
loads in one attribute, #[authorize(Action, bind = Service)], and receives
the row as an Authorized<Action, E> parameter to hand straight to the
service — on HTTP, from the route’s path:
#[post("/{id}/publish")] #[authorize(Update, bind = PostsService)] #[use_guards(PostAuthorGuard)] // … async fn publish(&self, post: Authorized<Update, PostEntity>) -> Result<Json<Post>, PostError> { Ok(Json(self.svc.publish(post.into_inner()).await?)) }and on GraphQL, from an id: String! argument the attribute synthesises:
#[mutation] #[authorize(Update, bind = PostsService)] #[use_guards(PostAuthorGuard)] async fn publish_post(&self, post: Authorized<Update, PostEntity>) -> Result<Post, PostError> { self.svc.publish(post.into_inner()).await }PostAuthorGuard refuses a token naming no user, on both edges. The service writes
the publication’s audit row through Creatable, the Create grant on it pinning
the actor to the caller, so no parameter carries who is publishing; its
PostError answers its 409 or its CONFLICT by its own ToProblem.
The posture and the value
Section titled “The posture and the value”Two pieces, each with one job:
#[authorize(Update, bind = PostsService)]is the posture — the class gate before the body and the caller’s ability for its reads, the entity derived fromPostsService::Entity— and the binding: it parses the id, runs the row-level access check forUpdate(404 on a missing row,FORBIDDENon a denied one) after the gate, and loads the row. This is the only thing that decides authorization, and it is greppable.Authorized<Update, PostEntity>is the value — the loaded row, which derefs to theModeland gives it up withinto_inner().
The action lives in the type, so the proof is action-true: a service
method that takes Authorized<Update, E> cannot be handed an
Authorized<Read, E> — a compile error, not a runtime surprise. The
Authorized<A, E> value is also proof of authorization: its constructor
is sealed, mintable only by the binding, so the mutation body can neither
reach a row the caller could not load nor act under an action it was not
granted.
Resolving the service
Section titled “Resolving the service”The service is resolved through the request’s scope, and the boot checks it
like an injected one: a controller or resolver whose module does not import
the service’s own fails the boot rather than its first call. On GraphQL the
synthesised id argument means the method must not declare an id
parameter of its own: writing id: String alongside it is E0415: identifier 'id' is bound more than once in this parameter list, reported against the
#[crud(…)] attribute on the impl block rather than the method — drop the
parameter, not the attribute; id_arg = … renames the argument. On HTTP the
id is the one parameter the route’s path names, its controller’s included, so
the whole path names it alone.
By-id binding goes through the service
Section titled “By-id binding goes through the service”Every generated get / update / delete calls
CrudService::access(action, id) — not a raw find_by_id. The
authorization is decided in SQL, by the same condition_for the list
path joins, so the row a list returns and the row access grants cannot
diverge. Denied vs missing is the
reference for how; here is what a handler sees:
Access outcome | Meaning | HTTP |
|---|---|---|
Access::Found(m) | Row exists, action allowed | proceed |
Access::Missing | Row does not exist | 404 |
Access::Denied | Row exists, action denied by a row-level or field-level rule | 403 |
#[crud] also rejects non-UUID-v7 ids before any load (route-model
binding’s validation half) — a malformed id is 400 Bad Request, never a
DB round-trip.
Going further
Section titled “Going further”- CRUD — the generated by-id operations beside a bound one.
- Route model binding — the
outcomes of the by-id load, and when to hide existence behind a
404. - Queries and mutations — the GraphQL field
the binding synthesises its
idargument on.