diff --git a/app/controllers/api/account.php b/app/controllers/api/account.php index f03cadd12..06459b067 100644 --- a/app/controllers/api/account.php +++ b/app/controllers/api/account.php @@ -1066,17 +1066,15 @@ App::patch('/v1/account/email') } $email = \strtolower($email); - $profile = $dbForInternal->findOne('users', [new Query('email', Query::TYPE_EQUAL, [\strtolower($email)])]); // Get user by email address - - if ($profile) { - throw new Exception('User already registered', 400); - } - - $user = $dbForInternal->updateDocument('users', $user->getId(), $user + try { + $user = $dbForInternal->updateDocument('users', $user->getId(), $user ->setAttribute('password', $isAnonymousUser ? Auth::passwordHash($password) : $user->getAttribute('password', '')) ->setAttribute('email', $email) ->setAttribute('emailVerification', false) // After this user needs to confirm mail again - ); + ); + } catch(Duplicate $th) { + throw new Exception('Email already exists', 409); + } $audits ->setParam('userId', $user->getId()) diff --git a/app/controllers/api/users.php b/app/controllers/api/users.php index 057689a8e..e66c8204e 100644 --- a/app/controllers/api/users.php +++ b/app/controllers/api/users.php @@ -484,15 +484,13 @@ App::patch('/v1/users/:userId/email') throw new Exception('User not found', 404); } - $email = \strtolower($email); - $profile = $dbForInternal->findOne('users', [new Query('email', Query::TYPE_EQUAL, [\strtolower($email)])]); // Get user by email address - - if ($profile) { + $email = \strtolower($email); + try { + $user = $dbForInternal->updateDocument('users', $user->getId(), $user->setAttribute('email', $email)); + } catch(Duplicate $th) { throw new Exception('Email already exists', 409); } - $user = $dbForInternal->updateDocument('users', $user->getId(), $user->setAttribute('email', $email)); - $audits ->setParam('userId', $user->getId()) ->setParam('event', 'account.update.email')