2013-06-24 11:24:14 -04:00
|
|
|
class Profiles::PasswordsController < ApplicationController
|
2013-10-09 09:17:40 -04:00
|
|
|
layout :determine_layout
|
2013-06-13 12:53:04 -04:00
|
|
|
|
2013-10-09 10:01:04 -04:00
|
|
|
skip_before_filter :check_password_expiration, only: [:new, :create]
|
2013-06-13 13:16:48 -04:00
|
|
|
|
2013-06-13 12:53:04 -04:00
|
|
|
before_filter :set_user
|
|
|
|
before_filter :set_title
|
2013-10-09 09:17:40 -04:00
|
|
|
before_filter :authorize_change_password!
|
2013-06-13 12:53:04 -04:00
|
|
|
|
|
|
|
def new
|
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
2014-07-29 08:28:20 -04:00
|
|
|
unless @user.valid_password?(user_params[:current_password])
|
|
|
|
redirect_to new_profile_password_path, alert: 'You must provide a valid current password'
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2014-06-26 08:11:45 -04:00
|
|
|
new_password = user_params[:password]
|
|
|
|
new_password_confirmation = user_params[:password_confirmation]
|
2013-06-13 12:53:04 -04:00
|
|
|
|
|
|
|
result = @user.update_attributes(
|
|
|
|
password: new_password,
|
|
|
|
password_confirmation: new_password_confirmation
|
|
|
|
)
|
|
|
|
|
|
|
|
if result
|
2013-06-13 13:21:51 -04:00
|
|
|
@user.update_attributes(password_expires_at: nil)
|
|
|
|
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
|
|
|
|
|
2014-06-26 08:11:45 -04:00
|
|
|
unless @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
|
|
|
|
|
|
|
|
if @user.update_attributes(password_attributes)
|
|
|
|
flash[:notice] = "Password was successfully updated. Please login with it"
|
|
|
|
redirect_to new_user_session_path
|
|
|
|
else
|
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
|
|
|
|
|
|
|
|
def set_title
|
|
|
|
@title = "New password"
|
|
|
|
end
|
2013-10-09 09:17:40 -04:00
|
|
|
|
|
|
|
def determine_layout
|
|
|
|
if [:new, :create].include?(action_name.to_sym)
|
|
|
|
'navless'
|
|
|
|
else
|
|
|
|
'profile'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def authorize_change_password!
|
|
|
|
return render_404 if @user.ldap_user?
|
|
|
|
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
|