BigW Consortium Gitlab

passwords_controller.rb 2.13 KB
Newer Older
1
class Profiles::PasswordsController < Profiles::ApplicationController
2
  skip_before_action :check_password_expiration, only: [:new, :create]
3

4 5
  before_action :set_user
  before_action :authorize_change_password!
6

7 8
  layout :determine_layout

9 10 11 12
  def new
  end

  def create
13
    unless @user.password_automatically_set || @user.valid_password?(user_params[:current_password])
14 15 16 17
      redirect_to new_profile_password_path, alert: 'You must provide a valid current password'
      return
    end

18 19
    new_password = user_params[:password]
    new_password_confirmation = user_params[:password_confirmation]
20 21 22

    result = @user.update_attributes(
      password: new_password,
23 24
      password_confirmation: new_password_confirmation,
      password_automatically_set: false
25 26 27
    )

    if result
28 29
      @user.update_attributes(password_expires_at: nil)
      redirect_to root_path, notice: 'Password successfully changed'
30 31 32 33 34
    else
      render :new
    end
  end

35 36 37 38
  def edit
  end

  def update
39
    password_attributes = user_params.select do |key, value|
40 41
      %w(password password_confirmation).include?(key.to_s)
    end
42
    password_attributes[:password_automatically_set] = false
43

44
    unless @user.password_automatically_set || @user.valid_password?(user_params[:current_password])
45 46 47 48 49 50 51 52
      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
53
      render 'edit'
54 55 56 57 58 59 60 61
    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

62 63 64 65 66 67
  private

  def set_user
    @user = current_user
  end

68
  def determine_layout
69
    if [:new, :create].include?(action_name.to_sym)
70
      'application'
71
    else
72
      'profile'
73 74 75 76 77 78
    end
  end

  def authorize_change_password!
    return render_404 if @user.ldap_user?
  end
79 80

  def user_params
81
    params.require(:user).permit(:current_password, :password, :password_confirmation)
82
  end
83
end