BigW Consortium Gitlab

passwords_controller.rb 1.36 KB
Newer Older
1
class PasswordsController < Devise::PasswordsController
2 3 4
  before_action :resource_from_email, only: [:create]
  before_action :prevent_ldap_reset,  only: [:create]
  before_action :throttle_reset,      only: [:create]
5

6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
  def edit
    super
    reset_password_token = Devise.token_generator.digest(
      User,
      :reset_password_token,
      resource.reset_password_token
    )

    unless reset_password_token.nil?
      user = User.where(
        reset_password_token: reset_password_token
      ).first_or_initialize

      unless user.reset_password_period_valid?
        flash[:alert] = 'Your password reset token has expired.'
21
        redirect_to(new_user_password_url(user_email: user['email']))
22 23 24
      end
    end
  end
25

26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
  protected

  def resource_from_email
    email = resource_params[:email]
    self.resource = resource_class.find_by_email(email)
  end

  def prevent_ldap_reset
    return unless resource && resource.ldap_user?

    redirect_to after_sending_reset_password_instructions_path_for(resource_name),
      alert: "Cannot reset password for LDAP user."
  end

  def throttle_reset
    return unless resource && resource.recently_sent_password_reset?
42

43 44 45 46
    # Throttle reset attempts, but return a normal message to
    # avoid user enumeration attack.
    redirect_to new_user_session_path,
      notice: I18n.t('devise.passwords.send_paranoid_instructions')
47
  end
48
end