针对 Ruby on Rails 的简单基于角色的授权库
Rabarber is a role-based authorization library for Ruby on Rails. It provides a set of tools for managing user roles and defining access rules, with support for multi-tenancy through context.
Example of Usage:
Consider a CRM system where users with different roles have distinct access levels. For instance, the role accountant can access and manage invoice data, while the role analyst can only view it. Here's how you'd define that with Rabarber:
…Include Rabarber::Authorization module in your controllers and configure authorization:
class ApplicationController { current_user.suspended? }
grant_access action: :show, roles: :client, if: :user_company_matches_order?
def show
# ...
end
private
def user_company_matches_order?
current_user.company == Order.find(params[:id]).company
end
endYou can pass a dynamic rule as an if or unless argument, which can be a symbol or a proc. Symbols refer to instance methods, and procs are evaluated in the controller at request time.
Dynamic rules can also be used without roles at all, allowing you to define custom logic or even delegate to custom policy objects:
class InvoicesController { invoice.period_closed? }
def update
# ...
end
grant_access action: :destroy, if: :destroy_allowed?
def destroy
# ...
end
private
def destroy_allowed?
InvoicePolicy.new(current_user).destroy?(invoice)
end
def invoice
@invoice ||= Invoice.find(params[:id])
end
endBy default, when unauthorized, Rabarber will redirect back (HTML format) or return 403 (other formats). You can override when_unauthorized method to customize unauthorized access behavior:
class ApplicationController { Project.find(params[:id]) }
def update
# Accessible to Project admin and owner of the current project
end
private
def current_project
@current_project ||= Project.find(params[:id])
end
endWhen a context object is deleted from your database, its associated roles become orphaned and ignored by Rabarber.
Ideally, the application should be designed so that this doesn't happen: whenever a context object is deleted, its roles are revoked and deleted as part of the same operation. However, if that wasn't done, you can clean up the orphaned roles as a fallback:
Rabarber.pruneWhen you rename or remove models used as contexts, you need to update Rabarber's stored context data accordingly. Use these irreversible data migrations:
# Rename a context class (e.g., when you rename your Ticket model to Task)
migrate_authorization_context!("Ticket", "Task")
# Remove context data (e.g., when you delete the Ticket model entirely)
delete_authorization_context!("Ticket")Include view helpers in your application:
module ApplicationHelper
include Rabarber::Helpers
endUse conditional rendering based on roles:
Like authorization, these helpers require an authenticated user. In views that can be rendered for both authenticated and unauthenticated visitors, wrap them in your authentication check.
Have a question or need assistance? Open a discussion in the discussions section for:
Issues track bugs, planned features, and other work. When reporting a bug, please include:
Any open issue is free to pick up or discuss. Check the project board or issues tab.
Before contributing, please read the contributing guidelines.
The gem is available as open source under the terms of the MIT License.
Everyone interacting in the Rabarber project is expected to follow the code of conduct.
Only the latest major version is actively maintained. READMEs for older versions are available here for reference:
暂无开放 Issues,或尚未同步最近议题。