Let only the author change a note

A NotePolicy compares user.id with note.user_id, and Sometimes leaves out fields a PATCH never sent.

Authentication says who you are. Authorization says what that person may do. Laravel puts the second decision in a policy. You pass the method itself into authorize, so a misspelled ability or the wrong model type fails the build.

Put the policy next to the model or in src/policies/note.rs. If you use the directory, declare pub mod policies; in src/lib.rs and re-export NotePolicy from src/policies/mod.rs. A forgotten mod line compiles the file out with no warning. The structure guide is explicit about that.

use laravel::auth::policy;

use crate::models::{Note, User};

pub struct NotePolicy;

#[policy]
impl NotePolicy {
    pub fn update(user: &User, note: &Note) -> bool {
        user.id == note.user_id
    }

    pub fn delete(user: &User, note: &Note) -> bool {
        user.id == note.user_id
    }
}

user_id is the column you kept on Note. Returning false denies the action with 403 and the default message “This action is unauthorized.” Access::deny_as_not_found() is the guide’s 404 when you would rather not reveal that the row exists. Field Notes uses the default 403 so the checkpoint is obvious.

Helpers that are not abilities go in a second impl without #[policy]. Every function inside the annotated block becomes an ability.

Call it before you write

use laravel::model::update;
use laravel::prelude::*;
use laravel::validation::Sometimes;

use crate::models::{Note, User};
use crate::policies::note::NotePolicy;

#[laravel::input(fills = Note)]
#[validate(authenticated)]
pub struct UpdateNote {
    #[validate(required, max_length(120))]
    pub title: Sometimes<String>,

    #[validate(required)]
    pub body: Sometimes<String>,
}

pub async fn update(
    user: Authenticated<User>,
    mut note: Note,
    Valid(input): Valid<UpdateNote>,
) -> laravel::Result<Redirect> {
    user.authorize(NotePolicy::update, &note).await?;

    update!(note { ..input }).await?;

    Ok(redirect!(notes::show, &note))
}

authorize returns an error when the policy says no, so ? stops the handler. can returns a bool when the page only needs to hide a button. The authorization guide renders that flag as a prop. You can do the same once the index is an Inertia page.

Register PATCH /notes/{note} inside the Authenticate group. The parameter name {note} matches Note.

Omitted is not null

An Option field writes NULL when the request leaves it out. HTML forms submit every control, so that matches a blank text input. A JSON PATCH, or an Inertia form that sends only changed fields, omits the rest. Those columns would be cleared if you typed them as Option.

Sometimes separates the two. A missing title decodes as Sometimes::Missing. ..input skips it, and the current title stays. A submitted title decodes as Sometimes::Present, the rules run, and update! writes the value. Validation skips rules on a missing Sometimes field, so required means “not empty when the client sent it,” not “the key must be present.”

Send {"body": "Revised"} and the title should be untouched. Send {"title": ""} and the title should fail validation. Do not add Sometimes to StoreNote. The models guide says creation forms should require the fields they always need. create! would skip a missing Sometimes field and fall through to the database default.

update! writes only columns that changed, plus updated_at. If nothing changed, it skips the query. After the await, note holds the new values in memory.

Check your work

Sign in as someone else and PATCH a note you do not own. The response should be 403, and the title should be unchanged.

Back to roadmap