From 594baa921b841de407126cfcefef9ccd2b5c1ca9 Mon Sep 17 00:00:00 2001 From: kolaente Date: Thu, 13 Aug 2020 16:41:29 +0200 Subject: [PATCH] Make sure only one user exists with a particular email when updating --- pkg/user/user.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/pkg/user/user.go b/pkg/user/user.go index 3683cce42..6d152b701 100644 --- a/pkg/user/user.go +++ b/pkg/user/user.go @@ -335,6 +335,19 @@ func UpdateUser(user *User) (updatedUser *User, err error) { } } + // Check if the email is already used + if user.Email == "" { + user.Email = theUser.Email + } else { + uu, err := getUser(&User{Email: user.Email}, true) + if err != nil && !IsErrUserDoesNotExist(err) { + return nil, err + } + if uu.ID != user.ID { + return nil, &ErrUserEmailExists{Email: user.Email, UserID: uu.ID} + } + } + user.Password = theUser.Password // set the password to the one in the database to not accedently resetting it // Validate the avatar type