问题描述
TL; DR (下面的完整问题)
cancan随附check_authorization
,可让您知道是否忘记向应用程序的任何部分添加授权。本质上,我只想知道是否有必要向应用程序的“设计”部分添加授权-还是devise开箱即用? (例如,如果一个用户可以更改另一位用户的密码,那将是非常糟糕的!-是否需要通过康康来阻止该操作,还是要确保已开箱即用?)
完整问题
我已经设计并可以设置,一切似乎都可以正常工作。
当我添加check_authorization
时,除了登录和注销不再起作用之外,所有其他功能都将继续起作用
CanCan::AuthorizationNotPerformed in Devise::SessionsController#destroy
This action failed the check_authorization because it does not authorize_resource. Add skip_authorization_check to bypass this check.
Extracted source (around line #266):
264
265
266
267
268
269
next if options[:unless] && controller.send(options[:unless])
raise AuthorizationNotPerformed,'This action failed the check_authorization because it does not authorize_resource. '\
'Add skip_authorization_check to bypass this check.'
end
奇怪的是,当用户更新其信息时,它可以正常工作(没有错误)
任何想法/指针都非常感谢
我还应该skip_authorization_check
在设计控制器上吗?尽管错误消息表明了这一点,但听起来还是有风险的
最后,我不完全知道如何访问devise控制器以添加skip_authorization_check
。这是我的目录(使用用户控制器进行设计)
解决方法
答案是您不必担心设计控制器上的skip_authorization_check
。
如果其他任何控制器都收到此错误消息:
CanCan::AuthorizationNotPerformed (This action failed the check_authorization because it does not authorize_resource. Add skip_authorization_check to bypass this check.):
然后解决方案是
- 向需要资源的资源(或其一部分)添加授权,或者
- 如果资源不需要授权,只需跳过授权检查。
可以通过将授权检查添加到资源中来跳过
skip_authorization_check
您可以像这样使用only
或except
skip_authorization_check except: [:index]
和
skip_authorization_check only: [:show,:edit]