Fail if changing the username would result in duplicate users

This commit is contained in:
kolaente 2020-08-13 16:15:04 +02:00
parent e2da1f8eaf
commit 458a6645ee
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
2 changed files with 10 additions and 1 deletions

View File

@ -168,7 +168,7 @@ var userUpdateCmd = &cobra.Command{
_, err = user.UpdateUser(u)
if err != nil {
log.Criticalf("Error updating the user: %s", err)
log.Fatalf("Error updating the user: %s", err)
}
fmt.Println("User updated successfully.")

View File

@ -324,6 +324,15 @@ func UpdateUser(user *User) (updatedUser *User, err error) {
if user.Username == "" {
//return User{}, ErrNoUsername{user.ID}
user.Username = theUser.Username // Dont change the username if we dont have one
} else {
// Check if the new username already exists
uu, err := GetUserByUsername(user.Username)
if err != nil && !IsErrUserDoesNotExist(err) {
return nil, err
}
if uu.ID != user.ID {
return nil, &ErrUsernameExists{Username: user.Username, UserID: uu.ID}
}
}
user.Password = theUser.Password // set the password to the one in the database to not accedently resetting it