Six - Ruby 授权库使用指南
2024-12-26 00:31:03作者:庞队千Virginia
Six 是一个简单的 Ruby 授权库,适用于 Rails 应用程序或其他任何框架。它基于纯 Ruby 实现,提供了灵活的授权机制。本文将详细介绍如何安装、使用 Six,并解释其 API 的使用方法。
1. 安装指南
Six 可以通过 RubyGems 进行安装。在终端中执行以下命令即可安装 Six:
gem install six
2. 项目的使用说明
快速开始
使用 Six 进行授权管理只需四个步骤:
-
创建 abilities 对象
abilities = Six.new -
创建包含
allowed方法的对象或类在这个对象或类中,你可以定义授权规则:
class BookRules def self.allowed(author, book) [:read_book, :edit_book] end end -
将规则对象添加到 abilities 中
abilities << BookRules # true -
检查授权
现在你可以检查用户是否具有某项权限:
abilities.allowed?(@user, :read_book, @book) # true
在 Rails 中使用
在 Rails 中,你可以将 Six 集成到控制器和视图中:
# application_controller.rb
class ApplicationController < ActionController::Base
protect_from_forgery
helper_method :abilities, :can?
protected
def abilities
@abilities ||= Six.new
end
def can?(object, action, subject)
abilities.allowed?(object, action, subject)
end
end
# books_controller.rb
class BooksController < ApplicationController
before_action :add_abilities
before_action :load_author
def show
@book = Book.find(params[:id])
head(404) and return unless can?(:guest, :read_book, @book)
end
def edit
@book = Book.find(params[:id])
head(404) and return unless can?(@author, :edit_book, @book)
end
protected
def add_abilities
abilities << Book
end
def load_author
@author = Author.find_by_id(params[:author_id])
end
end
# Model
class Book < ActiveRecord::Base
belongs_to :author
def self.allowed(object, subject)
rules = []
return rules unless subject.instance_of?(Book)
rules << :read_book if subject.public?
rules << :edit_book if object && object.id == subject.author_id
rules
end
end
# View
link_to 'Edit', edit_book_path(book) if can?(@author, :edit_book, @book)
Ruby 使用示例
class BookRules
def self.allowed(author, book)
rules = []
return rules unless book.instance_of?(Book)
rules << :read_book if book.published?
rules << :edit_book if book.author?(author)
if book.author?(author) && book.is_approved?
rules << :publish_book
end
rules
end
end
abilities = Six.new
abilities << BookRules
abilities.allowed? guest, :read_book, unpublished_book # false
abilities.allowed? guest, :read_book, published_book # true
abilities.allowed? guest, :edit_book, book # false
abilities.allowed? author, :edit_book, book # true
abilities.allowed? guest, :remove_book, book # false
3. 项目 API 使用文档
初始化
# 简单初始化
abilities = Six.new
# 带规则初始化
abilities = Six.new(:book_rules => BookRules)
# 多规则初始化
abilities = Six.new(:book => BookRules, :auth => AuthRules, :managment => ManagerRules)
添加规则
abilities = Six.new
# 简单添加
abilities << BookRules
# 高级添加(带命名空间)
abilities.add(:book_rules, BookRules)
检查权限
abilities = Six.new
abilities << BookRules
abilities.allowed? @guest, :read_book, @book # true
abilities.allowed? @guest, :edit_book, @book # false
abilities.allowed? @guest, [:read_book, :edit_book], @book # false
使用特定规则集
abilities.add(:book_rules, BookRules)
abilities.add(:car_rules, CarRules)
abilities.use(:book_rules)
abilities.allowed? ... # 仅使用 BookRules 中的规则
命名空间
class BookRules
def self.allowed(author, book)
[:read_book, :edit_book, :publish_book]
end
end
class CarRules
def self.allowed(driver, car)
[:drive, :sell]
end
end
abilities = Six.new
abilities.add(:book, BookRules)
abilities.add(:car, CarRules)
abilities.use(:book)
abilities.allowed? :anyone, :read_book, book # true
abilities.allowed? :anyone, :drive, car # false
abilities.reset_use
abilities.allowed? :anyone, :drive, :any # true
abilities.allowed? :anyone, :read_book, :any # true
4. 项目安装方式
Six 可以通过 RubyGems 进行安装,命令如下:
gem install six
通过本文,你应该能够顺利安装并使用 Six 进行授权管理。Six 提供了灵活的授权机制,适用于各种 Ruby 应用程序。
登录后查看全文
热门项目推荐
Kimi-K2.5Kimi K2.5 是一款开源的原生多模态智能体模型,它在 Kimi-K2-Base 的基础上,通过对约 15 万亿混合视觉和文本 tokens 进行持续预训练构建而成。该模型将视觉与语言理解、高级智能体能力、即时模式与思考模式,以及对话式与智能体范式无缝融合。Python00- QQwen3-Coder-Next2026年2月4日,正式发布的Qwen3-Coder-Next,一款专为编码智能体和本地开发场景设计的开源语言模型。Python00
xw-cli实现国产算力大模型零门槛部署,一键跑通 Qwen、GLM-4.7、Minimax-2.1、DeepSeek-OCR 等模型Go06
PaddleOCR-VL-1.5PaddleOCR-VL-1.5 是 PaddleOCR-VL 的新一代进阶模型,在 OmniDocBench v1.5 上实现了 94.5% 的全新 state-of-the-art 准确率。 为了严格评估模型在真实物理畸变下的鲁棒性——包括扫描伪影、倾斜、扭曲、屏幕拍摄和光照变化——我们提出了 Real5-OmniDocBench 基准测试集。实验结果表明,该增强模型在新构建的基准测试集上达到了 SOTA 性能。此外,我们通过整合印章识别和文本检测识别(text spotting)任务扩展了模型的能力,同时保持 0.9B 的超紧凑 VLM 规模,具备高效率特性。Python00
KuiklyUI基于KMP技术的高性能、全平台开发框架,具备统一代码库、极致易用性和动态灵活性。 Provide a high-performance, full-platform development framework with unified codebase, ultimate ease of use, and dynamic flexibility. 注意:本仓库为Github仓库镜像,PR或Issue请移步至Github发起,感谢支持!Kotlin08
VLOOKVLOOK™ 是优雅好用的 Typora/Markdown 主题包和增强插件。 VLOOK™ is an elegant and practical THEME PACKAGE × ENHANCEMENT PLUGIN for Typora/Markdown.Less00
项目优选
收起
deepin linux kernel
C
27
11
OpenHarmony documentation | OpenHarmony开发者文档
Dockerfile
537
3.75 K
暂无简介
Dart
773
191
Ascend Extension for PyTorch
Python
343
406
🎉 (RuoYi)官方仓库 基于SpringBoot,Spring Security,JWT,Vue3 & Vite、Element Plus 的前后端分离权限管理系统
Vue
1.34 K
754
🍒 Cherry Studio 是一款支持多个 LLM 提供商的桌面客户端
TypeScript
1.07 K
97
React Native鸿蒙化仓库
JavaScript
303
355
openEuler内核是openEuler操作系统的核心,既是系统性能与稳定性的基石,也是连接处理器、设备与服务的桥梁。
C
337
179
AscendNPU-IR
C++
86
141
openJiuwen agent-studio提供零码、低码可视化开发和工作流编排,模型、知识库、插件等各资源管理能力
TSX
986
248