我在app的admin部分使用pundit进行访问控制.我有一个仪表板控制器,如下所示:
class Admin::DashboardsController < AdminController
def index
@total_revenue = Order.total_revenue
authorize :dashboards, :index?
end
...
end
Run Code Online (Sandbox Code Playgroud)
和一个看起来像这样的政策:
class DashboardPolicy < Struct.new(:user, :dashboard)
def index?
true
end
end
Run Code Online (Sandbox Code Playgroud)
当我试图访问时,/admin/dashboards/
我得到一个Pundit::NotDefinedError, unable to find policy SymbolPolicy for dashboards
我也试过命名空间策略并得到了同样的错误.
我有一个has_many关联的多选框.params进来如下:
foo_ids: ["1", "2", "3"]
Run Code Online (Sandbox Code Playgroud)
使用强参数,我不允许这个属性,因为我想自己授权,所以人们不能只是把它们想要的东西放进去.
def update
bar.foos = authorized_foos
bar.update(baz_params)
respond_with bar
end
private
def authorized_foos
foos = Foo.find(params[:baz][:foo_ids])
foos.each do |foo|
authorize foo, :manage?
end
end
Run Code Online (Sandbox Code Playgroud)
这种方法将迫使我找到所有的foos,循环遍历它们,并单独授权每个foos.是否有更简单的方法来管理has_many授权,最好是使用Pundit gem?
所以,我正在尝试使用宝石评论员.我只想弄清楚如何为用户和管理员提供索引视图.我想为管理员呈现所有结果,仅为用户呈现相关帖子.我在github上搜索和搜索,但我找不到任何运气.我必须在政策和控制器中加入什么?
原始代码
class PostsPolicy
attr_reader :current_user, :model
def initialize(current_user, model)
@current_user = current_user
@post = model
end
def index?
@current_user.admin?
end
end
Run Code Online (Sandbox Code Playgroud)
调节器
class PostsController < ApplicationController
before_filter :load_user
before_filter :authenticate_user!
after_action :verify_authorized
def index
@posts = Post.order('title').page(params[:page]).per(25)
authorize User
end
private
def load_user
@user = User.find_by_id(params[:user_id])
end
end
Run Code Online (Sandbox Code Playgroud)
第二次更新
class PostsPolicy
class Scope
attr_reader :user, :scope
def initialize(user, scope)
@user = user
@scope = scope
end
def resolve
if user.admin?
scope.all
else
scope.where(user: user)
end
end
end
end
Run Code Online (Sandbox Code Playgroud)
调节器 …
我正在使用宝石pundit
和devise
.我有一个删除链接,只有在你是管理员时才显示.我有一个集成测试,我想验证删除链接只显示给管理员.
test 'comment delete link shows when it should' do
log_in_as @admin
get movie_path(@movie)
assert_select 'a[href=?]', movie_comment_path(comments(:one), @movie.id)
end
Run Code Online (Sandbox Code Playgroud)
我test_helper.rb
看起来像这样:
...
class ActiveSupport::TestCase
...
def log_in_as(user, options = {})
password = options[:password] || 'password'
if integration_test?
post user_session_path, 'user[email]' => user.email, 'user[password]' => user.password
else
Devise::TestHelpers.sign_in user
end
end
private
# Returns true inside an integration test.
def integration_test?
defined?(post_via_redirect)
end
end
Run Code Online (Sandbox Code Playgroud)
该response.body
看上去一切正常,但确确实实没有删除链接.当我运行开发服务器并自己访问该页面时,有一个.我已经把这个缩小到current_user
政策中的权威人士使用的值为nil
.这是我的comment_policy.rb
:
class CommentPolicy
attr_reader …
Run Code Online (Sandbox Code Playgroud) 我喜欢 Pundit gem 的简单性,我希望通过将它们存储到数据库来使策略动态化。
基本上,我正在寻找一种无需重新部署应用程序即可更改策略的方法。
在我的rails应用程序网站中,访问者可以注册并创建内容.它使用设计用户模型,一切运作良好.
现在我想使用rails_admin来管理网站资源和用户等,只有拥有管理上限的人才能访问它.
我应该为管理面板访问创建单独的AdminUser模型,还是使用admin角色的用户模型,并使用一些授权库来管理访问.
如果我只使用一个模型,那么我希望用户在登录后重定向到管理面板,如果用户是管理员,如果不是,那么我希望用户被重定向到他们的个人资料.哪个授权库cancan或专家将更适合我的情况.
谢谢!
在带有activeadmin gem(当前主分支)的Rails 4应用程序中,我使用Pundit进行授权.它适用于资源,但我无法使其适用于页面.
举个例子:
ActiveAdmin.register_page "Home" do
content do
para "some text"
end
end
Run Code Online (Sandbox Code Playgroud)
我如何为特定用户授权?
通过阅读Pundit自述文件我尝试使用以下代码,但它不起作用
class HomePolicy < Struct.new(:user, :home)
def index?
true
end
def show?
true
end
end
Run Code Online (Sandbox Code Playgroud)
任何的想法 ?
我正在尝试用Rails 4创建一个应用程序.
我已经用Rolify gem定义了一系列角色.
现在,我想使用权威来允许有角色的用户做某些事情.在不止一种角色可以做某件事的情况下,我已经定义了一组角色.
在我的application_policy中,我定义了私有方法,这些方法列出了我想在权威权限中使用的角色组.
我的应用程序策略实例化用户和记录.然后我将记录定义为相关模型的名称(与该模型的策略名称相同).
我有:
class ApplicationPolicy
attr_reader :user, :record
def initialize(user, record)
@user = user
@record = record
end
def index?
false
end
def show?
scope.where(:id => record.id).exists?
end
def create?
false
end
def new?
create?
end
def update?
false
end
def edit?
update?
end
def destroy?
false
end
def scope
Pundit.policy_scope!(user, record.class)
end
class Scope
attr_reader :user, :scope
def initialize(user, scope)
@user = user
@scope = scope
end
def resolve
scope
end
end
private
def …
Run Code Online (Sandbox Code Playgroud) 我将 ActiveAdmin gem 与 Pundit(和 Rolify)gem 一起使用。
这就是我编写策略的方式(取自: https: //github.com/activeadmin/activeadmin/blob/master/spec/support/templates/policies/application_policy.rb):
class ApplicationPolicy
attr_reader :user, :record
def initialize(user, record)
@user = user
@record = record
end
def show?
scope.where(id: record.id).exists?
end
def create?
user.has_role?(:staff, record.company)
end
def update?
scope.where(id: record.id).exists?
end
def destroy?
scope.where(id: record.id).exists?
end
def destroy_all?
true
end
def scope
Pundit.policy_scope!(user, record.class)
end
class Scope
attr_reader :user, :scope
def initialize(user, scope)
@user = user
@scope = scope
end
def resolve
if user.admin?
scope.all
else
company_ids = Company.with_role(:staff, user).map(&:id) …
Run Code Online (Sandbox Code Playgroud) 我已经在我的 Rails 应用程序中安装了 gem Pundit,并仔细按照自述文件中的说明进行操作。
但是,当我在任何控制器中使用授权时,我收到错误消息“未定义的方法‘授权’为 .
此外,当我尝试在视图中使用“policy”时,我收到错误“未定义的方法‘policy’”。
就好像根本没有安装 Pundit 一样!
我在我的应用程序控制器中包含了 Pundit。
我有一个包含我所有策略的 ApplicationPolicy 文件,然后是从应用程序策略继承的每种记录类型的策略文件。
出于某种原因,我无法将我的任何代码粘贴到这个问题中,所以我希望我已经包含了足够的信息!
我很感激任何想法。
pundit ×10
activeadmin ×2
devise ×2
rolify ×2
ruby ×2
cancan ×1
permissions ×1
rails-admin ×1
roles ×1
rubygems ×1