BigW Consortium Gitlab

passwords_controller.rb 1.74 KB
Newer Older
1
class Profiles::PasswordsController < ApplicationController
2
  layout :determine_layout
3

4
  skip_before_filter :check_password_expiration, only: [:new, :create]
5

6 7
  before_filter :set_user
  before_filter :set_title
8
  before_filter :authorize_change_password!
9 10 11 12 13 14 15 16 17 18 19 20 21 22

  def new
  end

  def create
    new_password = params[:user][:password]
    new_password_confirmation = params[:user][:password_confirmation]

    result = @user.update_attributes(
      password: new_password,
      password_confirmation: new_password_confirmation
    )

    if result
23 24
      @user.update_attributes(password_expires_at: nil)
      redirect_to root_path, notice: 'Password successfully changed'
25 26 27 28 29
    else
      render :new
    end
  end

30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
  def edit
  end

  def update
    password_attributes = params[:user].select do |key, value|
      %w(password password_confirmation).include?(key.to_s)
    end

    unless @user.valid_password?(params[:user][:current_password])
      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
47
      render 'edit'
48 49 50 51 52 53 54 55
    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

56 57 58 59 60 61 62 63 64
  private

  def set_user
    @user = current_user
  end

  def set_title
    @title = "New password"
  end
65 66 67 68 69 70 71 72 73 74 75 76

  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
77
end