【问题标题】:Bypassing Devise database authentication for specific users绕过特定用户的设计数据库身份验证
【发布时间】:2019-01-31 16:31:23
【问题描述】:

我在开发环境中为我公司的用户设置了 LDAP 身份验证,我需要能够允许这些通过 LDAP 身份验证的用户绕过 Devise 数据库身份验证检查。我一直在为通过 LDAP 身份验证的用户使用 create_or_find_by,但我要么为新用户返回“无效的电子邮件或密码”,要么为已经在数据库中的 ldap 用户返回“该密码以前使用过”。我在 devise/sessions_controller.rb 中进行了这些更改:

def create
    if params[:user][:email].match?(/mycompany.com/)
      ldap_user = LdapAuth.authenticate(params[:user][:email], params[:user][:password])
      if ldap_user.authenticated
        User.find_or_create_by(first_name: params[:user][:email]) do |user|
          user.password = params[:user][:password]
          user.password_confirmation = params[:user][:password]
          user.role = 2
          user.confirmed_at = Time.zone.now
          user.confirmation_sent_at = Time.zone.now
          user.unconfirmed_email = false
          user.first_name = 'Ldap'
          user.last_name = 'User'
          user.save
        end
        super do |user|
          user.user_session_event_logs.create username: user.email,
                                              ip_address: user.current_sign_in_ip,
                                              event_type: :login
        end
      end
    else
      super do |user|
        user.user_session_event_logs.create username: user.email,
                                            ip_address: user.current_sign_in_ip,
                                            event_type: :login
      end
    end
  end

有没有更好的方法来做到这一点?

【问题讨论】:

    标签: ruby-on-rails devise


    【解决方案1】:

    这可能听起来很简单,但你应该能够做这样的事情

    ...
    if ldap_user.authenticated
      sign_in(ldap_user), :bypass => true)
    else
      ...
    end
    ...
    

    希望对你有帮助

    快乐编码

    【讨论】:

    • 感谢您的评论。我为此构建了一个自定义工作并让它工作。不过,这是一个不错的解决方案。我会认为这已被接受。
    猜你喜欢
    • 2019-08-26
    • 2016-03-19
    • 1970-01-01
    • 1970-01-01
    • 2021-09-17
    • 1970-01-01
    • 2019-07-09
    • 2014-12-20
    • 1970-01-01
    相关资源
    最近更新 更多