*CURRENTLY UNMAINTAINED*. Authority helps you authorize actions in your Rails app. It's ORM-neutral and has very little fancy syntax; just group your models und
*CURRENTLY UNMAINTAINED*. Authority helps you authorize actions in your Rails app. It's ORM-neutral and has very little fancy syntax; just group your models und
Authority is now unmaintained. Users who have installed it decided to trust me, and I'm not comfortable transferring that trust to someone else on their behalf. However, if you'd like to fork it, feel free.
Gary Foster has provided a script to migrate to Pundit.
Authority helps you authorize actions in your Ruby app. It's ORM-neutral and has very little fancy syntax; just group your models under one or more Authorizer classes and write plain Ruby methods on them.
Authority will work fine with a standalone app or a single sign-on system. You can check roles in a database or permissions in a YAML file. It doesn't care! What it does do is give you an easy way to organize your logic and handle unauthorized actions.
If you're using it with Rails controllers, it requires that you already have some kind of user object in your application, accessible via a method like current_user (configurable).
Overview
The flow of Authority
Installation
Defining Your Abilities
Wiring It Together
Users
Models
Authorizers
Passing Options
Default methods
Testing Authorizers
Controllers
Views
The Generic can?
Security Violations & Logging
Credits
Contributing
Using Authority, you have:
link_to 'Edit Widget', edit_widget_path(@widget) if current_user.can_update?(@widget)link_to 'Keelhaul Scallywag', keelhaul_scallywag_path(@scallywag) if current_user.can_keelhaul?(@scallywag)Most importantly, you have total flexibility: Authority does not constrain you into using a particular scheme of roles and/or permissions.
Authority lets you control access based on:
All you have to do is define the methods you need on your authorizers. You have all the flexibility of normal Ruby classes.
You make the rules; Authority enforces them.
Authority encapsulates all authorization logic in Authorizer classes. Want to do something with a model? Ask its authorizer.
You can specify a model's authorizer one of two ways:
authorizer = SomeAuthorizerauthorizer_name = 'SomeAuthorizer' (useful if the constant isn't yet loaded)If you don't specify an authorizer, the model will:
Comment will look for CommentAuthorizer.ApplicationAuthorizer.Models that have the same authorization rules should use the same authorizer. In other words, if you would write the exact same methods on two models to determine who can create them, who can edit them, etc, then they should use the same authorizer.
Some example groupings:
Simplest case Logical groups Most granular
ApplicationAuthorizer ApplicationAuthorizer ApplicationAuthorizer
+ + +
| +--------+-------+ +-------------------+-------------------+
| + + + + +
| BasicAuthorizer AdminAuthorizer CommentAuthorizer ArticleAuthorizer EditionAuthorizer
| + + + + +
+-------+-------+ +-+ +------+ | | |
+ + + + + + + + +
Comment Article Edition Comment Article Edition Comment Article EditionThe authorization process generally flows like this:
current_user.can_create?(Article) # You ask this question, and the user
+ # automatically asks the model...
|
v
Article.creatable_by?(current_user) # The model automatically asks
+ # its authorizer...
|
v
AdminAuthorizer.creatable_by?(current_user) # *You define this method.*
+ # If you don't, the inherited one
| # calls `default`...
v
AdminAuthorizer.default(:creatable, current_user) # *You define this method.*
# If you don't, it will use the one
# inherited from ApplicationAuthorizer.
# (Its parent, Authority::Authorizer,
# defines the method as `return false`.)If the answer is false and the original caller was a controller, this is treated as a SecurityViolation. If it was a view, maybe you just don't show a link.
The authorization process for instances is different in that it calls the instance's default method before calling the class default method. This allows you to define default behaviour that requires access to the model instance to be determined (eg, assume any action on a blog post is allowed if that post is marked 'wiki').
(Diagrams made with AsciiFlow)
Starting from a clean commit status, add authority to your Gemfile, then bundle.
If you're using Rails, run rails g authority:install. Otherwise, pass a block to Authority.configure with configuration options somewhere when your application boots up.
Edit config/initializers/authority.rb. That file documents all your options, but one of particular interest is config.abilities, which defines the verbs and corresponding adjectives in your app. The defaults are:
config.abilities = {
:create => 'creatable',
:read => 'readable',
:update => 'updatable',
:delete => 'deletable'
}This option determines what methods are added to your users, models and authorizers. If you need to ask user.can_deactivate?(Satellite) and @satellite.deactivatable_by?(user), add :deactivate => 'deactivatable' to the hash.
# Whatever class represents a logged-in user in your app
class User
# Adds `can_create?(resource)`, etc
include Authority::UserAbilities
...
endclass Article
# Adds `creatable_by?(user)`, etc
include Authority::Abilities
# Without this, 'ArticleAuthorizer' is assumed;
# if that doesn't exist, 'ApplicationAuthorizer'
self.authorizer_name = 'AdminAuthorizer'
...
endAdd your authorizers under app/authorizers, subclassing the generated ApplicationAuthorizer.
These are where your actual authorization logic goes. Here's how it works:
resource).def deletable_by?(user)) will fall back to the corresponding class method. In other words, if you haven't said whether a user can update this particular widget, we'll decide by checking whether they can update any widget.def self.updatable_by?(user)) will call that authorizer's default method.For example:
# app/authorizers/schedule_authorizer.rb
class ScheduleAuthorizer @post)This would ultimately call creatable_by? on the designated authorizer with two arguments: the user and {:for => @post}. If you've defined that method yourself, you'd need to ensure that it accepts the options hash before doing this, or you'd get a "wrong number of arguments" error.
There's nothing special about the hash key :for; I just think it reads well in this case. You can pass any options that make sense in your case.
If you don't pass options, none will be passed to your authorizer, either.
And you could always handle the case above without options if you don't mind creating an extra model instance:
user.can_create?(Comment.new(:post => @post))Any class method you don't define on an authorizer will call the default method on that authorizer. This method is defined on Authority::Authorizer to simply return false. This is a 'whitelisting' approach; any permission you haven't specified (which falls back to the default method) is considered forbidden.
You can override this method in your ApplicationAuthorizer and/or per authorizer. For example, you might want one that looks up the user's roles and correlates them with permissions:
# app/authorizers/application_authorizer.rb
class ApplicationAuthorizer 'read', # `index` controller action will check `readable_by?`
:show => 'read',
:new => 'create', # `new` controller action will check `creatable_by?`
:create => 'create', # ...etc
:edit => 'update',
:update => 'update',
:destroy => 'delete'
}They are also configurable per controller, as follows:
…You can pass extra arguments to your authorization checks in these controller helpers:
authorize_actions_for(Llama, args: [{:mamma => true}]authorize_action_for(@llama, :sporting => @hat_style)Generally, though, your authorization will depend on some attribute or association of the model instance, so the authorizer can check @llama.neck_strength and @llama.owner.nationality, etc, without needing any additional information.
Note that you can also call authority_actions as many times as you like, so you can specify one mapping at a time if you prefer:
class LlamasController 'create'
def vaporize
# some code
end
authority_actions :vaporize => 'delete'
endIf you have a controller that dynamically determines the class it's working with, you can pass the name of a controller instance method to authorize_actions_for instead of a class, and the class will be looked up when a request is made.
class LlamasController [:index, :search], :if => :auditing_security?, :unless => :devise_controllerNo open issues yet, or sync has not completed.