Skip to content

Commit

Permalink
Only encrypts NewPassword if it is not empty, when updating the user …
Browse files Browse the repository at this point in the history
…details. Fixes #1222
  • Loading branch information
deluan committed Jul 1, 2021
1 parent ed286c7 commit fb183e5
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
4 changes: 3 additions & 1 deletion persistence/user_repository.go
Expand Up @@ -63,7 +63,9 @@ func (r *userRepository) Put(u *model.User) error {
u.ID = uuid.NewString()
}
u.UpdatedAt = time.Now()
_ = r.encryptPassword(u)
if u.NewPassword != "" {
_ = r.encryptPassword(u)
}
values, _ := toSqlArgs(*u)
delete(values, "current_password")
update := Update(r.tableName).Where(Eq{"id": u.ID}).SetMap(values)
Expand Down
18 changes: 18 additions & 0 deletions persistence/user_repository_test.go
Expand Up @@ -48,6 +48,24 @@ var _ = Describe("UserRepository", func() {
Expect(actual.Name).To(Equal("Admin"))
Expect(actual.Password).To(Equal("wordpass"))
})
It("updates the name and keep the same password", func() {
usr.Name = "Jane Doe"
usr.NewPassword = ""
Expect(repo.Put(&usr)).To(BeNil())

actual, err := repo.FindByUsernameWithPassword("admin")
Expect(err).ToNot(HaveOccurred())
Expect(actual.Name).To(Equal("Jane Doe"))
Expect(actual.Password).To(Equal("wordpass"))
})
It("updates password if specified", func() {
usr.NewPassword = "newpass"
Expect(repo.Put(&usr)).To(BeNil())

actual, err := repo.FindByUsernameWithPassword("admin")
Expect(err).ToNot(HaveOccurred())
Expect(actual.Password).To(Equal("newpass"))
})
})

Describe("validatePasswordChange", func() {
Expand Down

0 comments on commit fb183e5

Please sign in to comment.