From bb666810d9e6131d8844c93d7e4dce0e6f038c2a Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Sun, 29 Aug 2021 17:45:25 +0545 Subject: [PATCH] update to use duplicate exception to check email already exists --- app/controllers/api/account.php | 14 ++++++-------- app/controllers/api/users.php | 10 ++++------ 2 files changed, 10 insertions(+), 14 deletions(-) diff --git a/app/controllers/api/account.php b/app/controllers/api/account.php index f03cadd126..06459b067c 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 057689a8ee..e66c8204eb 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')