【问题标题】:CanCanCan: Allowing to manage only users profileCanCanCan:只允许管理用户个人资料
【发布时间】:2015-08-29 10:36:46
【问题描述】:

我有使用 device 和 cancancan 的用户模型。 这是我的能力:

if user.role? :patient
         can :access_profile, User, :id => user.id
end

access_profile 别名在哪里:

alias_action :show, :edit, :update, :destroy, :to => :access_profile

在我的 users_controller 中,我使用 load_and_authorize_resource。 例如,当我尝试“users/4”和 current_user.id = 3 我能够到达那里。

在日志中我看到了这个:

User Load (0.5ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1  [["id", 4]]
User Load (0.6ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1  ORDER BY "users"."id" ASC LIMIT 1  [["id", 3]]
  Rendered users/show.html.erb within layouts/application (0.4ms)

这里的第一个查询来自 set_user 方法。我猜第二个查询会加载我的资源以进行授权。在这里,由于 current_user.id = 3 并且在第二个查询中我也看到了 3,我想这就是我被授权的原因。但我猜第二个查询中的 id 必须是 4。

如何才能使 load_and_authorize 方法正常工作。

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-4 cancancan


    【解决方案1】:

    您确定自动加载实例有效吗?如果实例无法加载,Cancancan 将使用 Class 作为对象来检查能力,如果定义了对 Class 的访问将始终返回 true(不管:id 等参数)

    将 load_and_authorize_resource 更改为 load_resource 并检查在 access_profile 操作中使用的authorize! :access_profile, @user 是否也会保护访问。

    您也可以直接在实例上定义能力: can :access_profile, user 而不是在 User 类上定义。

    【讨论】:

      猜你喜欢
      • 2017-11-10
      • 2015-10-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多