2018-09-14 01:42:05 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2014-03-18 07:25:49 -04:00
|
|
|
class PasswordsController < Devise::PasswordsController
|
2021-11-02 11:12:22 -04:00
|
|
|
include GitlabRecaptcha
|
|
|
|
|
2017-12-31 00:08:15 -05:00
|
|
|
skip_before_action :require_no_authentication, only: [:edit, :update]
|
|
|
|
|
2021-11-02 11:12:22 -04:00
|
|
|
prepend_before_action :check_recaptcha, only: :create
|
|
|
|
before_action :load_recaptcha, only: :new
|
2015-10-01 21:47:27 -04:00
|
|
|
before_action :resource_from_email, only: [:create]
|
2017-11-23 08:16:14 -05:00
|
|
|
before_action :check_password_authentication_available, only: [:create]
|
2019-01-16 07:09:29 -05:00
|
|
|
before_action :throttle_reset, only: [:create]
|
2015-05-11 14:31:31 -04:00
|
|
|
|
2020-10-08 14:08:32 -04:00
|
|
|
feature_category :authentication_and_authorization
|
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2015-05-13 22:29:15 -04:00
|
|
|
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?
|
2019-04-08 10:17:45 -04:00
|
|
|
flash[:alert] = _('Your password reset token has expired.')
|
2015-05-13 23:57:16 -04:00
|
|
|
redirect_to(new_user_password_url(user_email: user['email']))
|
2015-05-13 22:29:15 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2015-09-30 15:38:21 -04:00
|
|
|
|
2016-03-02 17:48:49 -05:00
|
|
|
def update
|
|
|
|
super do |resource|
|
2020-09-01 08:11:01 -04:00
|
|
|
if resource.valid?
|
|
|
|
resource.password_automatically_set = false
|
|
|
|
resource.password_expires_at = nil
|
|
|
|
resource.save(validate: false) if resource.changed?
|
2016-03-02 17:48:49 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-10-01 21:47:27 -04:00
|
|
|
protected
|
|
|
|
|
|
|
|
def resource_from_email
|
|
|
|
email = resource_params[:email]
|
|
|
|
self.resource = resource_class.find_by_email(email)
|
|
|
|
end
|
|
|
|
|
2017-11-23 08:16:14 -05:00
|
|
|
def check_password_authentication_available
|
|
|
|
if resource
|
|
|
|
return if resource.allow_password_authentication?
|
|
|
|
else
|
2018-02-02 13:39:55 -05:00
|
|
|
return if Gitlab::CurrentSettings.password_authentication_enabled?
|
2017-11-23 08:16:14 -05:00
|
|
|
end
|
2015-10-01 21:47:27 -04:00
|
|
|
|
|
|
|
redirect_to after_sending_reset_password_instructions_path_for(resource_name),
|
2019-04-08 10:17:45 -04:00
|
|
|
alert: _("Password authentication is unavailable.")
|
2015-10-01 21:47:27 -04:00
|
|
|
end
|
|
|
|
|
2021-11-02 11:12:22 -04:00
|
|
|
def check_recaptcha
|
|
|
|
return unless resource_params[:email].present?
|
|
|
|
|
|
|
|
super
|
|
|
|
end
|
|
|
|
|
2015-10-01 21:47:27 -04:00
|
|
|
def throttle_reset
|
|
|
|
return unless resource && resource.recently_sent_password_reset?
|
2015-09-30 15:38:21 -04:00
|
|
|
|
2015-12-09 12:45:26 -05:00
|
|
|
# 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')
|
2015-09-30 15:38:21 -04:00
|
|
|
end
|
2021-06-07 11:09:56 -04:00
|
|
|
|
|
|
|
def context_user
|
|
|
|
resource
|
|
|
|
end
|
2014-03-18 07:25:49 -04:00
|
|
|
end
|
2019-09-13 09:26:31 -04:00
|
|
|
|
2021-05-11 17:10:21 -04:00
|
|
|
PasswordsController.prepend_mod_with('PasswordsController')
|