Fail if changing the username would result in duplicate users
This commit is contained in:
parent
e2da1f8eaf
commit
458a6645ee
|
@ -168,7 +168,7 @@ var userUpdateCmd = &cobra.Command{
|
||||||
|
|
||||||
_, err = user.UpdateUser(u)
|
_, err = user.UpdateUser(u)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Criticalf("Error updating the user: %s", err)
|
log.Fatalf("Error updating the user: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println("User updated successfully.")
|
fmt.Println("User updated successfully.")
|
||||||
|
|
|
@ -324,6 +324,15 @@ func UpdateUser(user *User) (updatedUser *User, err error) {
|
||||||
if user.Username == "" {
|
if user.Username == "" {
|
||||||
//return User{}, ErrNoUsername{user.ID}
|
//return User{}, ErrNoUsername{user.ID}
|
||||||
user.Username = theUser.Username // Dont change the username if we dont have one
|
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
|
user.Password = theUser.Password // set the password to the one in the database to not accedently resetting it
|
||||||
|
|
Loading…
Reference in New Issue