Commit c41e66db520c43fdcfc0e1e49208a23bb02835a7

Authored by Dmitriy Zaporozhets
1 parent 7af1bc3b

Separate page for password change

app/controllers/profiles/passwords_controller.rb
1 1 class Profiles::PasswordsController < ApplicationController
2   - layout 'navless'
  2 + layout :determine_layout
3 3  
4 4 skip_before_filter :check_password_expiration
5 5  
6 6 before_filter :set_user
7 7 before_filter :set_title
  8 + before_filter :authorize_change_password!
8 9  
9 10 def new
10 11 end
... ... @@ -26,6 +27,32 @@ class Profiles::PasswordsController &lt; ApplicationController
26 27 end
27 28 end
28 29  
  30 + def edit
  31 + end
  32 +
  33 + def update
  34 + password_attributes = params[:user].select do |key, value|
  35 + %w(password password_confirmation).include?(key.to_s)
  36 + end
  37 +
  38 + unless @user.valid_password?(params[:user][:current_password])
  39 + redirect_to edit_profile_password_path, alert: 'You must provide a valid current password'
  40 + return
  41 + end
  42 +
  43 + if @user.update_attributes(password_attributes)
  44 + flash[:notice] = "Password was successfully updated. Please login with it"
  45 + redirect_to new_user_session_path
  46 + else
  47 + render 'account'
  48 + end
  49 + end
  50 +
  51 + def reset
  52 + current_user.send_reset_password_instructions
  53 + redirect_to edit_profile_password_path, notice: 'We sent you an email with reset password instructions'
  54 + end
  55 +
29 56 private
30 57  
31 58 def set_user
... ... @@ -35,4 +62,16 @@ class Profiles::PasswordsController &lt; ApplicationController
35 62 def set_title
36 63 @title = "New password"
37 64 end
  65 +
  66 + def determine_layout
  67 + if [:new, :create].include?(action_name.to_sym)
  68 + 'navless'
  69 + else
  70 + 'profile'
  71 + end
  72 + end
  73 +
  74 + def authorize_change_password!
  75 + return render_404 if @user.ldap_user?
  76 + end
38 77 end
... ...
app/views/profiles/passwords/edit.html.haml 0 → 100644
... ... @@ -0,0 +1,32 @@
  1 +%h3.page-title Password
  2 +%p.light
  3 + Change your password or recover your current one.
  4 +%hr
  5 +.update-password
  6 + = form_for @user, url: profile_password_path, method: :put do |f|
  7 + %div
  8 + %p.slead
  9 + You must provide current password in order to change it.
  10 + %br
  11 + After a successful password update you will be redirected to login page where you should login with your new password
  12 + -if @user.errors.any?
  13 + .alert.alert-error
  14 + %ul
  15 + - @user.errors.full_messages.each do |msg|
  16 + %li= msg
  17 + .control-group
  18 + = f.label :current_password
  19 + .controls
  20 + = f.password_field :current_password, required: true
  21 + %div
  22 + = link_to "Forgot your password?", reset_profile_password_path, method: :put
  23 +
  24 + .control-group
  25 + = f.label :password, 'New password'
  26 + .controls= f.password_field :password, required: true
  27 + .control-group
  28 + = f.label :password_confirmation
  29 + .controls
  30 + = f.password_field :password_confirmation, required: true
  31 + .form-actions
  32 + = f.submit 'Save password', class: "btn btn-save"
... ...