因为任何人都可以注册然后登录,...并且因为用户在登录后才能识别角色,所以跳过 Devise 的授权检查是否有意义?
在此前提下,我使用此registrations_controller从Devise注册 Controller 继承并将其放置在 Controller 目录中。
class Users::RegistrationsController < Devise::RegistrationsController
skip_authorization_check
end
更改路由文件:
devise_for :users, :controllers => { :registrations => "registrations" }
不过我错过了一些东西:
This action failed the check_authorization because it does not authorize_resource. Add skip_authorization_check to bypass this check.
感谢您的帮助。
最佳答案
简单的解决方案
check_authorization :unless => :devise_controller?
如果您必须在某个时候手动将 check_authorization 放入每个 Controller 中,您将会忘记并在您的应用程序中打开一个安全漏洞。最好将不需要 cancan 身份验证的 Controller 显式列入白名单。
CANCAN 文档对此进行了明确说明
https://github.com/ryanb/cancan/wiki/Ensure-Authorization
编辑
class ApplicationController < ActionController::Base
check_authorization :unless => :do_not_check_authorization?
private
def do_not_check_authorization?
respond_to?(:devise_controller?) or
condition_one? or
condition_two?
end
def condition_one?
...
end
def condition_two?
...
end
end
关于ruby-on-rails-3 - cancan Skip_authorization_check 用于设计身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7019834/