2015-09-11 14:41:01 +05:30
|
|
|
class Profiles::PasswordsController < Profiles::ApplicationController
|
|
|
|
skip_before_action :check_password_expiration, 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
|
|
|
|
|
|
|
def new
|
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
2015-04-26 12:48:37 +05:30
|
|
|
unless @user.password_automatically_set || @user.valid_password?(user_params[:current_password])
|
2014-09-02 18:07:02 +05:30
|
|
|
redirect_to new_profile_password_path, alert: 'You must provide a valid current password'
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
password_attributes = {
|
|
|
|
password: user_params[:password],
|
|
|
|
password_confirmation: user_params[:password_confirmation],
|
2015-04-26 12:48:37 +05:30
|
|
|
password_automatically_set: false
|
2017-09-10 17:25:29 +05:30
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
redirect_to root_path, notice: 'Password successfully changed'
|
|
|
|
else
|
|
|
|
render :new
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def edit
|
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
|
|
|
password_attributes = user_params.select do |key, value|
|
|
|
|
%w(password password_confirmation).include?(key.to_s)
|
|
|
|
end
|
2015-04-26 12:48:37 +05:30
|
|
|
password_attributes[:password_automatically_set] = false
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2015-04-26 12:48:37 +05:30
|
|
|
unless @user.password_automatically_set || @user.valid_password?(user_params[:current_password])
|
2014-09-02 18:07:02 +05:30
|
|
|
redirect_to edit_profile_password_path, alert: 'You must provide a valid current password'
|
|
|
|
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
|
2014-09-02 18:07:02 +05:30
|
|
|
flash[:notice] = "Password was successfully updated. Please login with it"
|
|
|
|
redirect_to new_user_session_path
|
|
|
|
else
|
2016-09-13 17:45:13 +05:30
|
|
|
@user.reload
|
2014-09-02 18:07:02 +05:30
|
|
|
render 'edit'
|
|
|
|
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
|
|
|
|
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
|
|
|
|
|
|
|
|
def user_params
|
|
|
|
params.require(:user).permit(:current_password, :password, :password_confirmation)
|
|
|
|
end
|
|
|
|
end
|