Skip to content

Commit

Permalink
refactored change password error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
activefx committed Sep 15, 2008
1 parent 387821a commit 3b6aec9
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 19 deletions.
21 changes: 6 additions & 15 deletions app/controllers/user/password_settings_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,12 @@ def index

# Change password action
def create
begin
if current_user.change_password!(params[:old_password], params[:password], params[:password_confirmation])
flash[:notice] = "Password successfully updated."
redirect_to user_profile_path(current_user)
else
@old_password = nil
flash.now[:error] = "Your password was not changed, you old password may be incorrect."
render :action => 'index'
end
rescue Authentication::UserAbstraction::OpenidAccount
flash[:error] = "OpenID users cannot change their password."
redirect_to user_profile_path(current_user)
rescue Authentication::UserAbstraction::PasswordMismatch
@old_password = nil
flash.now[:error] = "New password does not match the password confirmation."
if current_user.change_password!(params[:old_password], params[:password], params[:password_confirmation])
flash[:notice] = "Password successfully updated."
redirect_to user_profile_path(current_user)
else
@old_password = nil
flash.now[:error] = current_user.errors.on_base || "There was a problem updating your password."
render :action => 'index'
end
end
Expand Down
1 change: 0 additions & 1 deletion app/views/user/password_settings/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
<%= password_field_tag 'password_confirmation', {}, :size => 45 %></p>

<%= submit_tag 'Change Password' %>
<% end %>


11 changes: 8 additions & 3 deletions lib/authentication/user_abstraction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,14 @@ def has_role?(role_in_question)
end

def change_password!(old_password, new_password, new_confirmation)
raise OpenidAccount if (!self.identity_url.blank? && self.crypted_password.blank?)
raise PasswordMismatch if (new_password != new_confirmation)
return nil unless (!new_password.blank? && User.authenticate(self.login, old_password))
errors.add_to_base("OpenID users cannot change their password.") and
return false if (!self.identity_url.blank? && self.crypted_password.blank?)
errors.add_to_base("New password does not match the password confirmation.") and
return false if (new_password != new_confirmation)
errors.add_to_base("New password cannot be blank.") and
return false if new_password.blank?
errors.add_to_base("You password was not changed, your old password is incorrect.") and
return false unless User.authenticate(self.login, old_password)
self.password, self.password_confirmation = new_password, new_confirmation
save
end
Expand Down

0 comments on commit 3b6aec9

Please sign in to comment.