Understanding Rails

Understanding Rails · Season 4: Controllers, routing and auth

Authorization without a gem

Episode 31 · deep dive · about 3 minutes · Rails 8.1 · by

How was this episode?

The series' author reads every note, and no one else sees it. When something is wrong or unclear, it gets fixed and the episode re-cut. How this series stays right.

Was it clear?

Not endorsed yet. Are you a Rails developer? What's an endorsement?

Endorse this version To endorse: create an account, confirm your email, then link GitHub.

What changed

  • Version 2, November 20, 2026: The extra query comes from loading Current.user, not from the admin check.

Who may do what, with plain Ruby and Current.

Free to watch. If it helped, support the series.

Transcript

0:00 Authorization without a gem

Episode thirty one. Authorization, without a gem. Signing in says who you are. Authorization decides what you may do. Today: how to keep each person to their own data, with plain Ruby and the Current user from episode thirty.

0:19 Giving data an owner

First, data needs an owner. Notebooks now belong to a user. Episode seventeen's three steps again: add the column, fill it in, then make it required. Sam owns Work and Home. Alex owns Ideas.

0:34 Signed in isn't allowed

Now sign in as Alex, and try Sam's things. The app only checks that you're signed in. So Alex opens Sam's note: two hundred. Alex changes it, and it saves: the body now says Alex was here. And Alex plants a new note in Sam's notebook. Signed in isn't the same as allowed.

0:53 Find through the user

The fix is to look things up through the current user. Instead of note dot find, current user dot notes dot find. A note that isn't Alex's simply isn't found.

1:05 Not found, not forbidden

Try again. Sam's note: four oh four. Changing it: four oh four. In production, Rails turns record not found into a not found page. That's on purpose. Forbidden would tell Alex the note exists. Not found tells Alex nothing.

1:21 Ids from params

The plant came through strong parameters, which allowed any notebook id, as episode twenty eight warned. So check the id the same way: find it through the current user's notebooks. Someone else's notebook isn't found, and that's a four oh four too.

1:38 The list

The list gets the same treatment. Current user dot notes. Alex's notes page now shows two notes, Alex's own.

1:47 An admin role

Some people need to see more. Add an admin flag to users, false by default. Put the rule on the user, in one place: visible notes. An admin sees every note. Everyone else sees their own.

2:00 The SQL

The SQL shows the difference. For Alex, the query joins notebooks, and keeps only rows where the user id is Alex's. For an admin, there's no join at all. One method, so every controller asks the same question, and gets the same answer.

2:17 A test that guards it

Guard it with a test. Sign in as one user, ask for another user's note, and expect not found. Try to plant a note in someone else's notebook, and expect no new note. One forgotten find, anywhere, and these fail.

2:32 One more query

There's a small cost. Finding things through Current dot user loads the user. Episode twenty's query count, five after sign in, is now six.

2:43 Hiding a button

And one warning. Hiding a button isn't authorization. Alex never needed an edit link. Alex just sent the request. Every check belongs in the controller, where the request arrives.

2:56 When rules grow

When the rules grow, with teams, shared notebooks, and many roles, an authorization gem can organize them. The idea underneath stays the same: look things up through what the user may see.

3:09 The rules

So: give data an owner. Find records through the current user. Check ids that come in from params. Keep each rule in one method. And test that someone else's record is not found.

3:22 Next

Next: rate limiting.

Check your understanding

  1. Alex is signed in and requests /notes/10, which is Sam's. The controller does Current.user.notes.find(params[:id]). What happens?

    • Alex sees the note, because Alex is signed in
    • The find raises RecordNotFound, and production answers 404
    • Rails answers 403 Forbidden
    Show answer

    The find raises RecordNotFound, and production answers 404. The lookup only searches Alex's own notes, so Sam's note isn't found. Active Record maps RecordNotFound to 404. Source: Securing Rails Applications: privilege escalation (Rails Guides 8.1).

  2. User has_many :notebooks, and has_many :notes, through: :notebooks. How does user.notes find its notes?

    • It loads every note and filters them in Ruby
    • One query that joins notebooks and keeps the rows where the notebook's user_id is the user's
    • A separate query for each notebook
    Show answer

    One query that joins notebooks and keeps the rows where the notebook's user_id is the user's. A has_many :through association goes through the middle model in SQL, here notebooks, so only that user's notes come back. Source: Active Record Associations: has_many :through (Rails Guides 8.1).

  3. The edit button only shows on your own notes. Is that enough to stop others editing?

    • Yes, nobody can click a button that isn't there
    • No: anyone can send the request directly, so the controller must check
    • Only if the form also uses strong parameters
    Show answer

    No: anyone can send the request directly, so the controller must check. Every parameter can be changed, however well it's hidden. The check belongs where the request arrives. Source: Securing Rails Applications: privilege escalation (Rails Guides 8.1).

  4. A form sends notebook_id. What stops someone posting a note into another user's notebook?

    • params.expect alone
    • Finding the notebook through Current.user.notebooks before saving
    • The foreign key on notes
    Show answer

    Finding the notebook through Current.user.notebooks before saving. Strong parameters allow the key, not the value. Looking the notebook up through the user's own notebooks rejects anyone else's. Source: Securing Rails Applications: privilege escalation (Rails Guides 8.1).

Sources

Every example in this episode was run on Rails 8.1.4.1 and Ruby 4.0.2.

Comments

Sign in to comment or sign up.