【问题标题】:I want to override authenticate_user and current_user method of devise gem我想覆盖设计 gem 的 authenticate_user 和 current_user 方法
【发布时间】:2011-09-19 11:51:26
【问题描述】:

我想覆盖 authenticate_user!和我的应用程序控制器中设计 gem 的 current_user 方法你能帮我解决这个问题吗 谢谢

【问题讨论】:

    标签: ruby ruby-on-rails-3 rubygems


    【解决方案1】:

    你可以像猴子一样修补它:

    module Devise
      module Controllers
        module Helpers
          def authenticate_user!
            #do some stuff
          end
        end
      end
    end   
    

    但我会问最终目标是什么,因为 Devise 已经内置了一些可定制性,而重写这些方法让我想知道“为什么要使用 Devise?”

    【讨论】:

    • 这是管理层的要求。所以我必须跟随。我想同时进行 LDAP 和数据库身份验证,而不是默认的设计行为为我完成这项工作..
    • 你好,但是把这个文件放在哪里呢?在哪个目录?而且,在application_controller.rb 中编写自定义方法是一样的吗?谢谢!
    • 你可以把它放在任何被加载的地方。我建议也许在application_helper.rb 中,因为它覆盖了一个辅助方法。但你也可以把它放在你提到的地方。
    【解决方案2】:

    关于覆盖用户的身份验证方式:

    Devise 在后台使用 Warden https://github.com/plataformatec/devise/blob/master/lib/devise/controllers/helpers.rb

    因此,您可以在 Warden 中添加一个新策略来验证您的用户。看 https://github.com/hassox/warden/wiki/Strategies

    您不需要覆盖 current_user。你面临什么挑战? 您需要返回不同的模型吗?

    【讨论】:

    • 莫妮卡,为什么会有人退回不同的型号?我猜如果你有像用户(与设计相关)和个人资料(包含有关用户的所有人类可读信息)之类的东西。如果是这样的话,人们会怎么做呢?
    【解决方案3】:

    您必须创建一个自定义类来覆盖默认的设计行为:

      class CustomFailure < Devise::FailureApp
        def redirect_url
          #return super unless [:worker, :employer, :user].include?(scope) #make it specific to a scope
           new_user_session_url(:subdomain => 'secure')
        end
    
        # You need to override respond to eliminate recall
        def respond
          if http_auth?
            http_auth
          else
            redirect
          end
        end
      end
    

    在你的 config/initializers/devise.rb 中:

      config.warden do |manager|
        manager.failure_app = CustomFailure
      end
    

    但我建议查看 Devise 文档 :)

    https://github.com/plataformatec/devise/wiki/How-To:-Redirect-to-a-specific-page-when-the-user-can-not-be-authenticated

    【讨论】:

    • 谢谢,正是我想要的!
    【解决方案4】:

    如果你想添加代码到authenticate_user!

    class DuckController < ApplicationController
      before_action :authenticate_duck
    
      ...
    
      private
    
      def authenticate_duck
        #use Devise's method
        authenticate_user!
        #add your own stuff
        unless current_user.duck.approved?
          flash[:alert] = "Your duck is still pending. Please contact support for support."
          redirect_to :back
        end
      end
    end
    

    【讨论】:

    • 它不起作用。在authenticate_user 之后!方法它从方法中返回,之后的代码不执行。
    【解决方案5】:

    在application_controller.rb,您可以随意覆盖:

    def authenticate_user!
      super # just if want the default behavior 
      call_a_method_to_something if current_user
      # or
      call_a_method_to_something if current_user.nil?
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-23
      • 1970-01-01
      • 2013-08-22
      • 1970-01-01
      • 1970-01-01
      • 2012-03-15
      相关资源
      最近更新 更多