【问题标题】:Sign out redirect path for devise注销设计的重定向路径
【发布时间】:2014-11-08 16:06:17
【问题描述】:

我已经覆盖了 Devise 的登出路径并将其设为 request.referer。问题在于,如果用户位于具有 authenticate_user 的页面上!在应用过滤器之前,我被发送到登录页面并显示错误。这并不理想,我只想在用户来自具有身份验证要求的路径时才重定向到 root_path。最好的方法是什么?

【问题讨论】:

    标签: ruby-on-rails devise


    【解决方案1】:

    我最终检查了与引荐来源网址关联的控制器的回调。如果有任何authenticate_*! 回调,则分别检查它们以查看@if 实例变量中是否指定了任何操作。如果没有,回调将应用于所有操作,并且我们知道引用 url 来自受限操作。如果指定了动作,我们使用正则表达式来检查引用动作是否受到限制。

    任何符合条件的回调都被添加到一个数组中。遍历回调后,我们检查该数组是否为空。如果是,我们将用户发送回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
    

    【讨论】:

      猜你喜欢
      • 2017-01-12
      • 2013-10-23
      • 1970-01-01
      • 2017-12-30
      • 1970-01-01
      • 2012-06-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多