关于 action-policy-coder
用于在 Rails 应用中实现 ActionPolicy 授权的编码技能,涵盖策略规则、控制器检查、范围过滤、缓存、本地化错误、测试及 GraphQL 或 ActionCable 集成。
主要功能
- 01策略类、规则与继承
- 02控制器授权检查
- 03授权关系范围过滤
- 04策略结果缓存
- 05本地化失败消息
- 06RSpec 与 GraphQL 指引
适用场景
- 01为 Rails 模型添加授权规则
- 02按用户权限筛选集合
- 03编写策略与请求规格测试
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ majesticlabs-dev/ majestic-marketplace --skill action-policy-coder安装后在 Claude Code 中输入/action-policy-coder确认已加载。
详细介绍
ActionPolicy 编码器
调用时
- 创建策略类,并设置适当的规则和继承关系
- 实现授权,在控制器中使用
authorize!和allowed_to? - 设置范围限定,使用
authorized_scope筛选集合 - 配置缓存,优化性能
- 添加 I18n,提供本地化的失败消息
- 编写测试,使用 ActionPolicy RSpec 匹配器
- 集成 GraphQL 和 ActionCable
安装
# Gemfile
gem "action_policy"
gem "action_policy-graphql" # For GraphQL integration
# Generate base policy
bin/rails generate action_policy:install
bin/rails generate action_policy:policy Post
策略类
ApplicationPolicy 基类
# app/policies/application_policy.rb
class ApplicationPolicy < ActionPolicy::Base
alias_rule :edit?, :destroy?, to: :update?
pre_check :allow_admins
private
def allow_admins
allow! if user.admin?
end
end
资源策略
# app/policies/post_policy.rb
class PostPolicy < ApplicationPolicy
def index? = true
def show? = true
def update? = owner?
def destroy? = owner? && !record.published?
def publish? = owner? && record.draft?
private
def owner? = user.id == record.user_id
end
控制器集成
class PostsController < ApplicationController
def show
@post = Post.find(params[:id])
authorize! @post
end
def update
@post = Post.find(params[:id])
authorize! @post
@post.update(post_params) ? redirect_to(@post) : render(:edit)
end
def publish
@post = Post.find(params[:id])
authorize! @post, to: :publish?
@post.publish!
redirect_to @post
end
end
条件渲染
<% if allowed_to?(:edit?, @post) %>
<%= link_to "Edit", edit_post_path(@post) %>
<% end %>
策略范围限定
class PostPolicy < ApplicationPolicy
relation_scope do |relation|
user.admin? ? relation.all : relation.where(user_id: user.id).or(relation.published)
end
relation_scope(:own) { |relation| relation.where(user_id: user.id) }
relation_scope(:drafts) { |relation| relation.where(user_id: user.id, status: :draft) }
end
# Controller usage
@posts = authorized_scope(Post.all)
@drafts = authorized_scope(Post.all, type: :relation, as: :drafts)
缓存
class PostPolicy < ApplicationPolicy
def update?
cache { owner_or_collaborator? } # Cache expensive checks
end
end
# config/initializers/action_policy.rb
ActionPolicy.configure do |config|
config.cache_store = Rails.cache
end
I18n 失败消息
# config/locales/action_policy.en.yml
en:
action_policy:
policy:
post_policy:
update?: "You can only edit your own posts"
destroy?: "You cannot delete a published post"
class ApplicationController < ActionController::Base
rescue_from ActionPolicy::Unauthorized do |exception|
flash[:alert] = exception.result.message
redirect_back fallback_location: root_path
end
end
交付内容
实现授权时,请提供:
- 策略类:包含规则、范围限定和缓存
- 控制器集成:使用 authorize! 和 allowed_to?
- 范围限定:用于 index 操作和筛选后的集合
- I18n:本地化错误消息
- 测试:RSpec 策略和请求规格
- GraphQL:适用时,为变更操作预先授权
评论
支持 Markdown