Ruby on rails Rails3ActiveAdminCancan。如何设置用户只能看到属于他的记录?
我设置了属于客户类(客户是公司)的管理员用户。所以客户有很多管理员用户 我试图限制对属于某个客户的装运记录的访问。我不希望客户看到其他客户的数据。所以我设置了这个,但似乎什么都没做 班级能力 包括CanCan::能力Ruby on rails Rails3ActiveAdminCancan。如何设置用户只能看到属于他的记录?,ruby-on-rails,ruby,cancan,activeadmin,Ruby On Rails,Ruby,Cancan,Activeadmin,我设置了属于客户类(客户是公司)的管理员用户。所以客户有很多管理员用户 我试图限制对属于某个客户的装运记录的访问。我不希望客户看到其他客户的数据。所以我设置了这个,但似乎什么都没做 班级能力 包括CanCan::能力 def initialize(user) user ||= AdminUser.new if user.role == "administrator" can :manage, :all else cannot
def initialize(user)
user ||= AdminUser.new
if user.role == "administrator"
can :manage, :all
else
cannot :create, :all
cannot :update, :all
cannot :destroy, :all
can :read, Shipment do |shipment|
shipment.customer == user.customer
end
end
end
end
我确实在发货中有这个。rb
ActiveAdmin.register Shipment do
menu :if => proc{ can?(:read, Shipment) }, :priority => 1
controller.authorize_resource
index do
column "File #", :sortable => :file_number do |shipment|
link_to shipment.file_number, admin_shipment_path(shipment)
end
[... more columns ...]
default_actions if can? :manage, Shipment
end
show :title => :file_number do
panel "Shipment Details" do
attributes_table_for shipment do
row("File number") {shipment.file_number}
row("Mode") {shipment.mode}
row("Ocean Rate") { number_to_currency shipment.ocean_rate}
row("Customer") { link_to shipment.customer.company_name, admin_customer_path(shipment.customer)}
row("Shipper") { link_to shipment.shipper.company_name, admin_shipper_path(shipment.shipper)}
row("Broker") { link_to shipment.broker.company_name, admin_broker_path(shipment.broker)}
end
end
[...more show action stuff...]
因此,在索引页面中,将显示所有装运,如果我以客户A的身份登录并单击客户B的装运,我可以看到它,但它应该会阻止我
更多信息
shipments_controller.rb
class ShipmentsController < InheritedResources::Base
before_filter :authenticate_admin_user!
end
u controller.rb
类ShipmentsController
我不知道为什么它不工作,但是可以吗?:读取,装运
将不会查找can:read,shipping do | shipping |…
的权限
要根据此权限进行验证,您必须指定装运的特定实例,如can:read,@shipping
在调用菜单:if=>…
行之前,您需要找到一种方法来获取访问的装运的实例
您是否尝试使用
控制器。加载和授权\u资源
而不是仅使用授权\u资源
?我的应用程序中也有类似问题。我有超级用户和管理员,管理员只能看到他们组织中的其他管理员
app/models/ability.rb
if user.organization_admin?
can :manage, User, :organization_id => user.organization_id
end
app/admin/users.rb
controller do load_and_authorize_resource :except => :index
def scoped_collection
end_of_association_chain.accessible_by(current_ability)
end
end
Active Admin有一个处理作用域的内置方法。请参见此处:使用作用域集合并根据当前登录用户运行查询
ActiveAdmin.register Shipment do
controller do
def scoped_collection
my_scope = resource_class.unscoped
return my_scope if current_admin_user.role == "administrator" # return everything
my_scope.where(customer_id: current_admin_user.customer_id) # filter by signed in user
end
end
end
根据文档尝试此操作,但也不起作用,因为任何用户都可以看到任何装运
can:show,shipping,:customer\u id=>user.customer\u id
您的权限定义很好,是您的菜单:if=>…
不起作用,也许你可以给我们提供更多关于它如何在shippings.rb
文件中使用的上下文(你确定它不在shippings\u controller.rb
?)。我在问题中添加了更多信息。menu:id=>
行所做的只是隐藏或显示菜单,如果用户应该可以访问它,我认为这不是问题所在。因此,我得到了列出的所有记录,如果我单击了其中一条用户不应该看到的记录,它仍然会显示出来,我希望CanCan告诉我访问被拒绝。如果权限定义是正确的,那么这意味着,controller.authorize\u resource
没有被读取索引或显示操作。@leonel yes,似乎是这样。让CanCan在ActiveAdmin中发挥出色似乎存在一些问题。