passwords_controller.rb 1.72 KB
Newer Older
1
class PasswordsController < Devise::PasswordsController
2 3
  include Gitlab::CurrentSettings

4
  before_action :resource_from_email, only: [:create]
5
  before_action :check_password_authentication_available, only: [:create]
6
  before_action :throttle_reset,      only: [:create]
7

8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
  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.'
23
        redirect_to(new_user_password_url(user_email: user['email']))
24 25 26
      end
    end
  end
27

28 29
  def update
    super do |resource|
30
      if resource.valid? && resource.require_password_creation?
31 32 33 34 35
        resource.update_attribute(:password_automatically_set, false)
      end
    end
  end

36 37 38 39 40 41 42
  protected

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

43 44
  def check_password_authentication_available
    return if current_application_settings.password_authentication_enabled? && (resource.nil? || resource.allow_password_authentication?)
45 46

    redirect_to after_sending_reset_password_instructions_path_for(resource_name),
47
      alert: "Password authentication is unavailable."
48 49 50 51
  end

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

53 54 55 56
    # 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')
57
  end
58
end