gitlab-org--gitlab-foss/app/controllers/profiles/passwords_controller.rb

90 lines
2.4 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
2015-04-30 14:37:25 -04:00
class Profiles::PasswordsController < Profiles::ApplicationController
skip_before_action :check_password_expiration, only: [:new, :create]
skip_before_action :check_two_factor_requirement, only: [:new, :create]
before_action :set_user
before_action :authorize_change_password!
layout :determine_layout
def new
end
def create
unless @user.password_automatically_set || @user.valid_password?(user_params[:current_password])
redirect_to new_profile_password_path, alert: 'You must provide a valid current password'
return
end
password_attributes = {
password: user_params[:password],
password_confirmation: user_params[:password_confirmation],
password_automatically_set: false
}
2017-09-27 05:48:33 -04:00
result = Users::UpdateService.new(current_user, password_attributes.merge(user: @user)).execute
if result[:status] == :success
2017-09-27 05:48:33 -04:00
Users::UpdateService.new(current_user, user: @user, password_expires_at: nil).execute
redirect_to root_path, notice: 'Password successfully changed'
else
render :new
end
end
2013-10-09 09:17:40 -04:00
def edit
end
def update
password_attributes = user_params.select do |key, value|
2013-10-09 09:17:40 -04:00
%w(password password_confirmation).include?(key.to_s)
end
password_attributes[:password_automatically_set] = false
2013-10-09 09:17:40 -04:00
unless @user.password_automatically_set || @user.valid_password?(user_params[:current_password])
2013-10-09 09:17:40 -04:00
redirect_to edit_profile_password_path, alert: 'You must provide a valid current password'
return
end
2017-09-27 05:48:33 -04:00
result = Users::UpdateService.new(current_user, password_attributes.merge(user: @user)).execute
if result[:status] == :success
2013-10-09 09:17:40 -04:00
flash[:notice] = "Password was successfully updated. Please login with it"
redirect_to new_user_session_path
else
@user.reload
2013-10-09 10:01:04 -04:00
render 'edit'
2013-10-09 09:17:40 -04:00
end
end
def reset
current_user.send_reset_password_instructions
redirect_to edit_profile_password_path, notice: 'We sent you an email with reset password instructions'
end
private
def set_user
@user = current_user
end
def determine_layout
2013-10-09 09:17:40 -04:00
if [:new, :create].include?(action_name.to_sym)
'application'
2013-10-09 09:17:40 -04:00
else
'profile'
2013-10-09 09:17:40 -04:00
end
end
def authorize_change_password!
render_404 unless @user.allow_password_authentication?
2013-10-09 09:17:40 -04:00
end
def user_params
params.require(:user).permit(:current_password, :password, :password_confirmation)
end
end