0
我重写了设计的注销路径并将其设置为request.referer。问题在于如果用户在具有authenticate_user的页面上!在应用过滤器之前,我被发送到登录页面并显示错误。这并不理想,我想只在用户来自具有验证要求的路径时才重定向到root_path。做这个的最好方式是什么?注销设计的重定向路径
我重写了设计的注销路径并将其设置为request.referer。问题在于如果用户在具有authenticate_user的页面上!在应用过滤器之前,我被发送到登录页面并显示错误。这并不理想,我想只在用户来自具有验证要求的路径时才重定向到root_path。做这个的最好方式是什么?注销设计的重定向路径
我最终检查了引用者url所关联的控制器的回调。如果有任何authenticate_*!
回调,则会分别检查它们以查看@if
实例变量中是否指定了任何操作。如果没有,则回调将应用于所有操作,并且我们知道引用网址来自受限操作。如果有指定的动作,我们使用正则表达式来检查引用动作是否受限制。
满足条件的任何回调都会添加到数组中。在循环回调之后,我们检查该数组是否为空。如果是,我们将用户发回request.referer
。如果它不是空的,这意味着将它们发送到request.referer
会将它们重定向到登录页面并显示错误。在这种情况下,我们将用户发送至root_path
。
如果发生任何错误,用户将被发送到root_path
。
下面是代码:
def after_sign_out_path_for(resource_or_scope)
begin
controller_name = Rails.application.routes.recognize_path(request.referer)[:controller]
controller_class = "#{controller_name}_controller".camelize.constantize
authentication_callbacks = controller_class._process_action_callbacks.select { |callback| callback.filter.match /authenticate_\w+!/ }
action_name = Rails.application.routes.recognize_path(request.referer)[:action]
restricting_callbacks = []
authentication_callbacks.each do |callback|
callback_scope = callback.instance_variable_get(:@if)
if callback_scope.empty? || callback_scope.first.match(/action_name\s==\s(\'|\")#{Regexp.quote(action_name)}(\'|\")/)
restricting_callbacks << callback
end
end
restricting_callbacks.empty? ? request.referer : root_path
rescue Exception => e
request.referer
end
end