繁体   English   中英

CanCanCan:仅允许管理用户个人资料

[英]CanCanCan: Allowing to manage only users profile

我有使用设备和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方法正常工作该怎么做。

您确定自动加载实例有效吗? 如果无法加载实例,Cancancan将使用Class作为对象来检查功能,并且如果定义,对Class的访问将始终返回true(无论诸如:id那样的参数)

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM