Skip to content

Commit

Permalink
[api] fix update password functionality (write correct hashed entry i…
Browse files Browse the repository at this point in the history
…nto database)
  • Loading branch information
adrianschroeter committed Nov 23, 2012
1 parent 0c08cbe commit 71564be
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 16 deletions.
1 change: 1 addition & 0 deletions src/api/app/controllers/person_controller.rb
Expand Up @@ -262,6 +262,7 @@ def update_watchlist( user, xml )
private :update_watchlist

def change_my_password
#FIXME3.0: remove this function
valid_http_methods :post, :put

xml = REXML::Document.new( request.raw_post )
Expand Down
23 changes: 12 additions & 11 deletions src/api/app/models/user.rb
Expand Up @@ -100,8 +100,9 @@ def new_password?
# user.save
#
def update_password(pass)
self.password_confirmation = pass
self.password = pass
self.password_crypted = hash_string(pass).crypt("os")
self.password_confirmation = hash_string(pass)
self.password = hash_string(pass)
end

# After saving the object into the database, the password is not new any more.
Expand Down Expand Up @@ -710,10 +711,10 @@ def state_transition_allowed?(from, to)
# Model Validation

validates_presence_of :login, :email, :password, :password_hash_type, :state,
:message => 'must be given'
:message => 'must be given'

validates_uniqueness_of :login,
:message => 'is the name of an already existing user.'
:message => 'is the name of an already existing user.'

# Overriding this method to do some more validation: Password equals
# password_confirmation, state an password hash type being in the range
Expand Down Expand Up @@ -760,20 +761,20 @@ def validate
# include this condition in your :if parameter to validates_format_of when
# overriding the password format validation.
validates_format_of :password,
:with => %r{\A[\w\.\- !?(){}|~*]+\z},
:message => 'must not contain invalid characters.',
:if => Proc.new { |user| user.new_password? and not user.password.nil? }
:with => %r{\A[\w\.\- !?(){}|~*]+\z},
:message => 'must not contain invalid characters.',
:if => Proc.new { |user| user.new_password? and not user.password.nil? }

# We want the password to have between 6 and 64 characters.
# The length must only be checked if the password has been set and the record
# has not been stored yet and it has actually been set at all. Make sure you
# include this condition in your :if parameter to validates_length_of when
# overriding the length format validation.
validates_length_of :password,
:within => 6..64,
:too_long => 'must have between 6 and 64 characters.',
:too_short => 'must have between 6 and 64 characters.',
:if => Proc.new { |user| user.new_password? and not user.password.nil? }
:within => 6..64,
:too_long => 'must have between 6 and 64 characters.',
:too_short => 'must have between 6 and 64 characters.',
:if => Proc.new { |user| user.new_password? and not user.password.nil? }

class << self
def current
Expand Down
9 changes: 4 additions & 5 deletions src/api/test/functional/person_controller_test.rb
Expand Up @@ -172,11 +172,10 @@ def test_register_and_change_password_new_way

post "/person/adrianSuSE?cmd=change_password", data
assert_response :success

u = User.find_by_login "adrianSuSE"
assert_not_nil u
assert_equal u.login, "adrianSuSE"
assert_equal u.password, data
# test login with new password
prepare_request_with_user "adrianSuSE", data
get "/person/adrianSuSE"
assert_response :success

#cleanup
u.destroy
Expand Down

0 comments on commit 71564be

Please sign in to comment.