Simple, declarative, role-based access
control system for Rails and Ruby
SimonSays can be installed via your Gemfile or using Ruby gems directly.
First, we need to define some roles. Generally speaking roles will exist on either "User" models or on relationship models (like a through model linking a User to another resource).
Here are two models, Admin and User that using the concern via the has_roles method. Roles are stored as an integer and bitmasking is used to determine authorization logic. When using Roleable you need to add a roles_mask column.
The attribute name can be customized by using the :as option as seen in the Admin model below.
The Membership model also shows how to use Roleable with a through model.
Finally, also note the dynamically generated has_ methods as shown in the User model as well the ROLES constant which is used in the Membership example.
The Authorizer concern provides several methods that can be used within your controllers in declarative manner.
Please note, certain assumptions are made with Authorizer. Building upon the above User and Admin models, Authorizer would assume there is a current_user and current_admin method. If these models correspond to devise scopes this would be the case by default. Additionally there would need to be an authenticate_user! and authenticate_admin! method, which devise provides as well.
Eventually, we would like to see better customization around the authentication aspects. This library is intended to solve the problem of authorization and access control. It is not an authentication library.
In the below example we have two controllers for document and report resources. It is clear to see which resources and which are roles are required for each controller and action. Note the difference between find_resource and find_and_authorize.
Two exceptions can be raised by the finder and authorize methods. Specifically, if a the report record or the membership relationship does not exist an ActiveRecord::NotFound exception will be raised. If the records do exist, but the role conditions are not met, a SimonSays::Authorizer::Denied exception is raised.
Please refer to the docs for more information on the various declarative methods provided by the Authorizer.
Get a website like this for your open source project for free!
- Fork it
- Create your new feature branch
- Commit your changes
- Push changes to the branch
- Create a new and submit a new Pull Request!
- Add Rails generators to make installation and customization even easier
- Expand the model API via the Roleable concern
- Authentication customization and more complex user logic