百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
R

rabarber

> 后端框架
开源

针对 Ruby on Rails 的简单基于角色的授权库

190 stars0 点赞3 次浏览
访问官网GitHub

工具介绍

针对 Ruby on Rails 的简单基于角色的授权库

Rabarber: Simple Authorization for 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:

…

Authorization

Setup

Include Rabarber::Authorization module in your controllers and configure authorization:

rb
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
end

You 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:

rb
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
end

When Unauthorized

By 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:

rb
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
end

Orphaned Contextual Roles

When 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:

rb
Rabarber.prune

Context Migrations

When you rename or remove models used as contexts, you need to update Rabarber's stored context data accordingly. Use these irreversible data migrations:

rb
# 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")

View Helpers

Include view helpers in your application:

rb
module ApplicationHelper
  include Rabarber::Helpers
end

Use conditional rendering based on roles:

erb

  

  

  

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.

Getting Help and Contributing

Getting Help

Have a question or need assistance? Open a discussion in the discussions section for:

  • Usage questions
  • Implementation guidance
  • Open-ended ideas or suggestions

Issues

Issues track bugs, planned features, and other work. When reporting a bug, please include:

  • A clear description of the problem
  • Steps to reproduce the issue
  • Your environment details (Rails and Ruby versions, OS, etc.)

Contributing Code

Any open issue is free to pick up or discuss. Check the project board or issues tab.

Before contributing, please read the contributing guidelines.

License

The gem is available as open source under the terms of the MIT License.

Code of Conduct

Everyone interacting in the Rabarber project is expected to follow the code of conduct.

Old Versions

Only the latest major version is actively maintained. READMEs for older versions are available here for reference:

v5.x.x | v4.x.x | v3.x.x | v2.x.x | v1.x.x

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Rubyauthorizationgemmultitenancyrails

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类后端框架
定价开源

> 相关工具

N
Node.js
基于 V8 的 JavaScript 运行时
D
Django
Python 高级 Web 框架
S
Spring Boot
Java 生态主流微服务框架