Externalize authorization in Rails so policy changes don't need a redeploy
Short answer: To externalize authorization in Rails, call PERMIT.check() from your controller actions and keep the rules in Permit, so a policy change needs no redeploy. Use it when roles and permissions change more often than your releases. Don't use it when a few fixed role checks in a single Rails app cover your needs.
| Fact | Value |
|---|---|
| Question this page answers | How do I externalize authorization in Ruby on Rails so policy changes don't need a redeploy? |
| Where the check runs | the posts action in PermitController, which calls PERMIT.check(). |
| SDK | Ruby SDK, permit-sdk on RubyGems. The SDK feature parity page lists version 0.0.4. The SDK has no role assignment method, so this tutorial calls the Permit REST API to assign roles. See Check permissions with the Ruby SDK. |
| PDP | A container PDP at http://localhost:7766 in this tutorial, or the Cloud PDP. See PDP overview. |
| Models used | RBAC for the Author and Reader roles. ReBAC and ABAC are also supported. |
| Policy changes | You change roles and permissions in the Permit dashboard. The application code and the deployment stay the same. |
| Free tier | Community plan, labeled "Free Forever" on the Permit.io pricing page: MAU 1000, Tenants 20, Authorization Queries No Limit, Environments 3, PDP Instances No Limit. |
| Audit | Each check appears on the Audit Log screen of the Permit dashboard. |
| Open source | The Edge PDP (permitio/PDP), OPAL, cedar-agent, the Permit SDKs, and the Permit CLI are open source. See Open-source fallback. |
Build a Rails controller for a blogging platform that registers users in Permit.io and allows only users with the Author role to create posts. This tutorial is for Ruby on Rails developers who want to enforce Permit.io policies from controller actions.
When you finish, your Rails app has two endpoints:
| Endpoint | What it does |
|---|---|
POST /register | Syncs a user to Permit.io and assigns the user the Reader role in the default tenant |
POST /posts | Calls PERMIT.check() and returns 403 unless the user in the request has permission to create a Post |
Prerequisites
- A Permit.io account. See Create a Permit.io account.
- Ruby and a Rails app created without the
--apiflag. The controller in this tutorial skips theverify_authenticity_tokencallback, which API-only Rails apps don't define. - Node.js and npm, to install the Permit CLI.
- Docker, to run the policy decision point (PDP) container.
1. Configure the policy in Permit
Create the blogging platform policy with the Permit CLI. If your environment already has a policy with a Post resource and an Author role that can create posts, skip to 2. Get your API key.
Install the Permit CLI
The Permit CLI creates policies and runs the PDP from your terminal. Install the CLI with npm:
npm install -g @permitio/cli
Run permit to confirm that the CLI is installed.
Sign in with the Permit CLI
Authenticate the CLI with your Permit.io account:
permit login
The command opens a browser window where you sign in. After you sign in, the CLI uses your default environment. To use a different environment, run permit env select and choose the environment.
Apply the blogging platform template
Permit CLI templates create a policy with predefined resources, roles, and rules. To see the available templates, run permit env template list. The template source files are in the Permit CLI repository.

Apply the blogging-platform template to your environment:
permit env template apply --template blogging-platform
The CLI prints a success message when the template is applied.
Review the policy in the Policy Editor
In the Permit dashboard, select your project and open the Policy screen.

The blogging-platform template creates:
| Policy element | What the template defines |
|---|---|
| Resources | Post (with a premium boolean attribute) and Comment, each with create, read, update, and delete actions |
| Roles | Admin (all actions), Author (create and read posts, read comments), Reader (create and read comments), and Premium Reader (read posts and comments) |
| Relationship | A Post is the parent of its Comment instances. An Author of a post instance becomes a Moderator of the comments on that post. This rule is relationship-based access control (ReBAC). |
| Resource set | Free Post contains posts where premium is false. Readers can read free posts. This rule is attribute-based access control (ABAC). |
This tutorial uses one rule from the policy: the Author role can create a Post, and the Reader role cannot. To change which role can perform an action, check or clear the box in the Policy Editor.
2. Get your API key
Your Rails app and the PDP authenticate with Permit.io with your environment API key. Copy the API key of the environment where you applied the template. See Get your API key.
Anyone with the environment API key can change that environment's policy through the Permit API. Load the key from an environment variable, and don't commit it.
3. Run the PDP
The PDP evaluates each permission check against your policy. Start a PDP container with the Permit CLI:
permit pdp run
The command starts the PDP in Docker and prints the container ID and name. The PDP listens on port 7766, so your app connects to it at http://localhost:7766.

The Free Post resource set is an ABAC rule, and the Cloud PDP doesn't evaluate ABAC rules, so run the container PDP for this policy. To run the container with docker run instead, or to check that the PDP is healthy, see Run the PDP.
4. Build the Rails app
Install the Ruby SDK
Add the Permit Ruby SDK to your Gemfile:
gem 'permit-sdk'
Install the gem:
bundle install
For the SDK reference, see Check permissions with the Ruby SDK.
Initialize the Permit client
Create config/initializers/permit.rb with the following code:
# config/initializers/permit.rb
require 'permit'
raise "PERMIT_API_KEY is not set" unless ENV['PERMIT_API_KEY']
raise "PDP_URL is not set" unless ENV['PDP_URL']
PERMIT = Permit.new(
ENV['PERMIT_API_KEY'],
ENV['PDP_URL']
)
Rails runs the initializer at startup and stores the client in the PERMIT constant. The initializer raises an error, and the app doesn't start, when either environment variable is missing:
| Variable | Value |
|---|---|
PERMIT_API_KEY | Your environment API key from 2. Get your API key |
PDP_URL | The PDP address from 3. Run the PDP: http://localhost:7766 |
Create the Permit controller
Create app/controllers/permit_controller.rb with the following code:
# app/controllers/permit_controller.rb
require 'net/http'
class PermitController < ApplicationController
skip_before_action :verify_authenticity_token
def register
user = PERMIT.api.users.sync_user(
key: params[:email],
email: params[:email],
first_name: params[:first_name],
last_name: params[:last_name]
)
assigned_role = {
user: params[:email],
role: 'Reader',
tenant: 'default'
}
response = assign_role(assigned_role)
render json: {
message: 'User registered and role assigned',
user: user,
role_assignment: response
}, status: :created
rescue => e
render json: { error: e.message }, status: :internal_server_error
end
def posts
permitted = PERMIT.check(
params[:user],
'create',
'Post'
)
if permitted
render json: { message: "User is permitted" }
else
render json: { message: "User is not permitted" }, status: :forbidden
end
rescue => e
render json: { error: e.message }, status: :internal_server_error
end
private
# The Ruby SDK has no role assignment method, so call the Permit REST API
def assign_role(assignment)
api = URI('https://api.permit.io')
headers = { 'Authorization' => "Bearer #{ENV['PERMIT_API_KEY']}", 'Content-Type' => 'application/json' }
Net::HTTP.start(api.host, api.port, use_ssl: true) do |http|
scope = JSON.parse(http.get('/v2/api-key/scope', headers).body)
path = "/v2/facts/#{scope['project_id']}/#{scope['environment_id']}/role_assignments"
JSON.parse(http.post(path, assignment.to_json, headers).body)
end
end
end
The controller has two actions:
| Action | What it does |
|---|---|
register | Syncs the user to Permit.io with the email address as the user key, then assigns the Reader role in the default tenant with the assign_role helper, which calls the Permit REST API because the Ruby SDK has no role assignment method. Returns 201 with the user and the role assignment. |
posts | Asks the PDP whether the user key in params[:user] can create a Post. Returns "User is permitted", or 403 with "User is not permitted". |
Both actions return 500 with the error message when a call to Permit.io or the PDP raises an error.
In a production app, take the user key from your authenticated session. This example reads the user key from the request parameters so that you can test the endpoint with curl.
Add the routes
Map the two endpoints to the controller actions in config/routes.rb:
# config/routes.rb
Rails.application.routes.draw do
post '/register', to: 'permit#register'
post '/posts', to: 'permit#posts'
end
Start the Rails app
Set the environment variables and start the server, replacing <YOUR_API_KEY> with your API key:
export PERMIT_API_KEY=<YOUR_API_KEY>
export PDP_URL=http://localhost:7766
bin/rails server
The app listens on http://localhost:3000.
5. Test the permission check
Register two users, give one of them the Author role, and confirm that the PDP allows only that user to create a post.
Register two users
In a second terminal, register John and Emma:
curl -X POST http://localhost:3000/register \
-H "Content-Type: application/json" \
-d '{"email": "john@example.com", "first_name": "John", "last_name": "Doe"}'
curl -X POST http://localhost:3000/register \
-H "Content-Type: application/json" \
-d '{"email": "emma@example.com", "first_name": "Emma", "last_name": "Den"}'
Each request returns HTTP 201 with "message": "User registered and role assigned", the synced user under user, and the Reader role assignment under role_assignment.
Assign John the Author role
Both users have the Reader role, which can't create posts. Give John the Author role in the Permit dashboard:
- Open the Directory screen and select
john@example.comto open the Edit User panel. - Under Permissions Per Tenant, select the Default Tenant.
- In Top Level Access, add the Author role.
- Click Save.

For other ways to assign roles, including the API and SDK, see Sync users.
Check that John can create a post and Emma can't
Send a POST /posts request for John:
curl -X POST http://localhost:3000/posts \
-H "Content-Type: application/json" \
-d '{"user": "john@example.com"}'
The PDP allows the request because John has the Author role. The app returns {"message":"User is permitted"}.
Send the same request for Emma:
curl -X POST http://localhost:3000/posts \
-H "Content-Type: application/json" \
-d '{"user": "emma@example.com"}'
The PDP denies the request because Emma has only the Reader role. The app returns HTTP 403 with {"message":"User is not permitted"}.
Each check also appears in the Audit Log screen of the Permit dashboard, with the user, action, resource, and decision.
6. Change the policy without redeploying
Your Ruby on Rails code contains the enforcement point: the posts action in PermitController, which calls PERMIT.check(). The rule about which role may create a post is policy in Permit, not code in your app. Change the rule and watch the app respond without restarting Rails.
- Keep the app and the PDP running, with John's Author role from 5. Test the permission check.
- In the Permit dashboard, open the Policy screen. Clear the box for the create action on Post in the Author role column.
- Send John's
POST /postsrequest from step 5 again. The PDP denies it, and the app returns the same403response that Emma received. - Check the box again and send the request once more. The app returns the successful response again.
Updates reach a PDP in the background, so repeat the request if you still see the previous result. The change doesn't appear in a pull request or a build, because it is a policy change and not a code change. To review policy changes like code, see GitOps.
Frequently asked questions
How do I externalize authorization in Ruby on Rails so policy changes don't need a redeploy?
Keep the enforcement point in your code and move the rules to Permit. Your handler asks the PDP whether a user can perform an action on a resource, using the Ruby SDK, and the PDP answers from the policy you edit in the Permit dashboard. Section 6 changes a rule and sends the same request again, with no code change.
Does a role or permission change require restarting Rails?
No. Roles, permissions, and role assignments live in Permit. The PDP receives updates in the background, so the next check uses the updated policy.
Where does the authorization decision run?
In the PDP, a separate service that your app calls over the network. This tutorial runs the PDP as a container at http://localhost:7766. The Cloud PDP is a managed alternative that supports RBAC and ReBAC. See Cloud PDP capabilities.
When should I keep authorization in Ruby on Rails code instead?
When a small set of fixed roles lives in one application and changes only with a code release, checks in the application are enough. Use Permit.io when roles change more often than releases, several services share the rules, or non-developers manage access.
Which Permit SDK does this tutorial use?
Ruby SDK, permit-sdk on RubyGems. The SDK feature parity page lists version 0.0.4. The SDK has no role assignment method, so this tutorial calls the Permit REST API to assign roles. See Check permissions with the Ruby SDK.
What does the free tier include?
The Community plan on the Permit.io pricing page lists MAU 1000, Tenants 20, Authorization Queries No Limit, and PDP Instances No Limit.
Next steps
- Check permissions with the Ruby SDK: SDK installation and more
permit.check()examples. - Check permissions with permit.check(): check against tenants, resource instances, and attributes.
- Build RBAC policies: create roles and permissions for your own resources.
- Connect your authentication provider: use the identity from your sign-in flow as the Permit.io user key.