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 应用程序。
登录后查看全文
热门项目推荐
kernelopenEuler内核是openEuler操作系统的核心,既是系统性能与稳定性的基石,也是连接处理器、设备与服务的桥梁。C091
baihu-dataset异构数据集“白虎”正式开源——首批开放10w+条真实机器人动作数据,构建具身智能标准化训练基座。00
mindquantumMindQuantum is a general software library supporting the development of applications for quantum computation.Python058
PaddleOCR-VLPaddleOCR-VL 是一款顶尖且资源高效的文档解析专用模型。其核心组件为 PaddleOCR-VL-0.9B,这是一款精简却功能强大的视觉语言模型(VLM)。该模型融合了 NaViT 风格的动态分辨率视觉编码器与 ERNIE-4.5-0.3B 语言模型,可实现精准的元素识别。Python00
GLM-4.7GLM-4.7上线并开源。新版本面向Coding场景强化了编码能力、长程任务规划与工具协同,并在多项主流公开基准测试中取得开源模型中的领先表现。 目前,GLM-4.7已通过BigModel.cn提供API,并在z.ai全栈开发模式中上线Skills模块,支持多模态任务的统一规划与协作。Jinja00
AgentCPM-Explore没有万亿参数的算力堆砌,没有百万级数据的暴力灌入,清华大学自然语言处理实验室、中国人民大学、面壁智能与 OpenBMB 开源社区联合研发的 AgentCPM-Explore 智能体模型基于仅 4B 参数的模型,在深度探索类任务上取得同尺寸模型 SOTA、越级赶上甚至超越 8B 级 SOTA 模型、比肩部分 30B 级以上和闭源大模型的效果,真正让大模型的长程任务处理能力有望部署于端侧。Jinja00
项目优选
收起
deepin linux kernel
C
27
11
OpenHarmony documentation | OpenHarmony开发者文档
Dockerfile
473
3.52 K
openEuler内核是openEuler操作系统的核心,既是系统性能与稳定性的基石,也是连接处理器、设备与服务的桥梁。
C
223
90
暂无简介
Dart
721
174
Ascend Extension for PyTorch
Python
283
316
React Native鸿蒙化仓库
JavaScript
286
338
本项目是CANN提供的数学类基础计算算子库,实现网络在NPU上加速计算。
C++
849
438
🎉 (RuoYi)官方仓库 基于SpringBoot,Spring Security,JWT,Vue3 & Vite、Element Plus 的前后端分离权限管理系统
Vue
1.27 K
699
Nop Platform 2.0是基于可逆计算理论实现的采用面向语言编程范式的新一代低代码开发平台,包含基于全新原理从零开始研发的GraphQL引擎、ORM引擎、工作流引擎、报表引擎、规则引擎、批处理引引擎等完整设计。nop-entropy是它的后端部分,采用java语言实现,可选择集成Spring框架或者Quarkus框架。中小企业可以免费商用
Java
10
1
🔥LeetCode solutions in any programming language | 多种编程语言实现 LeetCode、《剑指 Offer(第 2 版)》、《程序员面试金典(第 6 版)》题解
Java
65
19