debian-mirror-gitlab/app/controllers/profiles/passwords_controller.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

97 lines
2.6 KiB
Ruby
Raw Normal View History

2018-12-05 23:21:45 +05:30
# frozen_string_literal: true
2015-09-11 14:41:01 +05:30
class Profiles::PasswordsController < Profiles::ApplicationController
skip_before_action :check_password_expiration, only: [:new, :create]
2018-03-27 19:54:05 +05:30
skip_before_action :check_two_factor_requirement, only: [:new, :create]
2014-09-02 18:07:02 +05:30
2015-09-11 14:41:01 +05:30
before_action :set_user
before_action :authorize_change_password!
2014-09-02 18:07:02 +05:30
2015-09-11 14:41:01 +05:30
layout :determine_layout
2014-09-02 18:07:02 +05:30
2021-01-03 14:25:43 +05:30
feature_category :authentication_and_authorization
2014-09-02 18:07:02 +05:30
def new
end
def create
2021-11-18 22:05:49 +05:30
unless @user.password_automatically_set || @user.valid_password?(user_params[:password])
2019-09-04 21:01:54 +05:30
redirect_to new_profile_password_path, alert: _('You must provide a valid current password')
2014-09-02 18:07:02 +05:30
return
end
2018-03-17 18:26:18 +05:30
result = Users::UpdateService.new(current_user, password_attributes.merge(user: @user)).execute
2017-09-10 17:25:29 +05:30
if result[:status] == :success
2018-03-17 18:26:18 +05:30
Users::UpdateService.new(current_user, user: @user, password_expires_at: nil).execute
2014-09-02 18:07:02 +05:30
2019-09-04 21:01:54 +05:30
redirect_to root_path, notice: _('Password successfully changed')
2014-09-02 18:07:02 +05:30
else
render :new
end
end
def edit
end
def update
2021-11-18 22:05:49 +05:30
unless @user.password_automatically_set || @user.valid_password?(user_params[:password])
2021-09-30 23:02:18 +05:30
handle_invalid_current_password_attempt!
2019-09-04 21:01:54 +05:30
redirect_to edit_profile_password_path, alert: _('You must provide a valid current password')
2014-09-02 18:07:02 +05:30
return
end
2018-03-17 18:26:18 +05:30
result = Users::UpdateService.new(current_user, password_attributes.merge(user: @user)).execute
2017-09-10 17:25:29 +05:30
if result[:status] == :success
2020-10-24 23:57:45 +05:30
flash[:notice] = _('Password was successfully updated. Please sign in again.')
2014-09-02 18:07:02 +05:30
redirect_to new_user_session_path
else
2019-07-31 22:56:46 +05:30
@user.reset
2014-09-02 18:07:02 +05:30
render 'edit'
end
end
def reset
current_user.send_reset_password_instructions
2019-09-04 21:01:54 +05:30
redirect_to edit_profile_password_path, notice: _('We sent you an email with reset password instructions')
2014-09-02 18:07:02 +05:30
end
private
def set_user
@user = current_user
end
def determine_layout
if [:new, :create].include?(action_name.to_sym)
2015-09-11 14:41:01 +05:30
'application'
2014-09-02 18:07:02 +05:30
else
'profile'
end
end
def authorize_change_password!
2018-03-17 18:26:18 +05:30
render_404 unless @user.allow_password_authentication?
2014-09-02 18:07:02 +05:30
end
2021-09-30 23:02:18 +05:30
def handle_invalid_current_password_attempt!
Gitlab::AppLogger.info(message: 'Invalid current password when attempting to update user password', username: @user.username, ip: request.remote_ip)
@user.increment_failed_attempts!
end
2014-09-02 18:07:02 +05:30
def user_params
2021-11-18 22:05:49 +05:30
params.require(:user).permit(:password, :new_password, :password_confirmation)
end
def password_attributes
{
password: user_params[:new_password],
password_confirmation: user_params[:password_confirmation],
password_automatically_set: false
}
2014-09-02 18:07:02 +05:30
end
end