From 6d22932092d0d927c92078a1388ef569678eb188 Mon Sep 17 00:00:00 2001 From: Hugo Sales Date: Wed, 28 Jul 2021 21:30:09 +0000 Subject: [PATCH] [ENTITY][LocalUser] Add method 'findByNicknameOrEmail' --- src/Entity/LocalUser.php | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/Entity/LocalUser.php b/src/Entity/LocalUser.php index 84a1d7e320..eca644e1ac 100644 --- a/src/Entity/LocalUser.php +++ b/src/Entity/LocalUser.php @@ -25,6 +25,7 @@ use App\Core\DB\DB; use App\Core\Entity; use App\Core\UserRoles; use App\Util\Common; +use App\Util\Exception\DuplicateFoundException; use DateTimeInterface; use Exception; use libphonenumber\PhoneNumber; @@ -302,6 +303,24 @@ class LocalUser extends Entity implements UserInterface { } + /** + * Is the nickname or email already in use locally? + * + * @return self Returns self if nickname or email found + */ + public static function findByNicknameOrEmail(string $nickname, string $email): ?self + { + $users = DB::findBy('local_user', ['or' => ['nickname' => $nickname, 'outgoing_email' => $email, 'incoming_email' => $email]]); + switch (count($users)) { + case 0: + return null; + case 1: + return $users[0]; + default: + throw new DuplicateFoundException('Multiple values in table local_user match the requested criteria'); + } + } + public function checkPassword(string $new_password): bool { // Timing safe password verification