2018-09-23 15:44:14 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-04-30 14:37:25 -04:00
|
|
|
class Profiles::PasswordsController < Profiles::ApplicationController
|
2015-04-16 08:03:37 -04:00
|
|
|
skip_before_action :check_password_expiration, only: [:new, :create]
|
2018-03-01 11:28:15 -05:00
|
|
|
skip_before_action :check_two_factor_requirement, only: [:new, :create]
|
2013-06-13 13:16:48 -04:00
|
|
|
|
2015-04-16 08:03:37 -04:00
|
|
|
before_action :set_user
|
|
|
|
before_action :authorize_change_password!
|
2013-06-13 12:53:04 -04:00
|
|
|
|
2015-05-01 04:39:11 -04:00
|
|
|
layout :determine_layout
|
|
|
|
|
2013-06-13 12:53:04 -04:00
|
|
|
def new
|
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
2015-02-13 07:33:28 -05:00
|
|
|
unless @user.password_automatically_set || @user.valid_password?(user_params[:current_password])
|
2014-07-29 08:28:20 -04:00
|
|
|
redirect_to new_profile_password_path, alert: 'You must provide a valid current password'
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2017-06-15 06:36:46 -04:00
|
|
|
password_attributes = {
|
|
|
|
password: user_params[:password],
|
|
|
|
password_confirmation: user_params[:password_confirmation],
|
2015-02-13 07:33:28 -05:00
|
|
|
password_automatically_set: false
|
2017-06-15 06:36:46 -04:00
|
|
|
}
|
|
|
|
|
2017-09-27 05:48:33 -04:00
|
|
|
result = Users::UpdateService.new(current_user, password_attributes.merge(user: @user)).execute
|
2017-06-15 06:36:46 -04:00
|
|
|
|
|
|
|
if result[:status] == :success
|
2017-09-27 05:48:33 -04:00
|
|
|
Users::UpdateService.new(current_user, user: @user, password_expires_at: nil).execute
|
2013-06-13 12:53:04 -04:00
|
|
|
|
2013-06-13 13:21:51 -04:00
|
|
|
redirect_to root_path, notice: 'Password successfully changed'
|
2013-06-13 12:53:04 -04:00
|
|
|
else
|
|
|
|
render :new
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-10-09 09:17:40 -04:00
|
|
|
def edit
|
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
2014-06-26 08:11:45 -04:00
|
|
|
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
|
2015-02-13 07:33:28 -05:00
|
|
|
password_attributes[:password_automatically_set] = false
|
2013-10-09 09:17:40 -04:00
|
|
|
|
2015-02-13 07:33:28 -05: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
|
2017-06-15 06:36:46 -04:00
|
|
|
|
|
|
|
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
|
2016-08-08 10:03:30 -04:00
|
|
|
@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
|
|
|
|
|
2013-06-13 12:53:04 -04:00
|
|
|
private
|
|
|
|
|
|
|
|
def set_user
|
|
|
|
@user = current_user
|
|
|
|
end
|
|
|
|
|
2015-05-01 04:39:11 -04:00
|
|
|
def determine_layout
|
2013-10-09 09:17:40 -04:00
|
|
|
if [:new, :create].include?(action_name.to_sym)
|
2015-05-01 04:39:11 -04:00
|
|
|
'application'
|
2013-10-09 09:17:40 -04:00
|
|
|
else
|
2015-05-01 04:39:11 -04:00
|
|
|
'profile'
|
2013-10-09 09:17:40 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def authorize_change_password!
|
2017-11-23 08:16:14 -05:00
|
|
|
render_404 unless @user.allow_password_authentication?
|
2013-10-09 09:17:40 -04:00
|
|
|
end
|
2014-06-26 08:11:45 -04:00
|
|
|
|
|
|
|
def user_params
|
2014-06-26 16:40:08 -04:00
|
|
|
params.require(:user).permit(:current_password, :password, :password_confirmation)
|
2014-06-26 08:11:45 -04:00
|
|
|
end
|
2013-06-13 12:53:04 -04:00
|
|
|
end
|