diff --git a/app/controllers/api/account.php b/app/controllers/api/account.php index 85d59bd02..02d70f24e 100644 --- a/app/controllers/api/account.php +++ b/app/controllers/api/account.php @@ -1529,11 +1529,11 @@ App::patch('/v1/account/name') ->inject('user') ->inject('dbForProject') ->inject('events') - ->action(function (string $name, ?\DateTime $requestTimestamp, Response $response, Document $user, Database $dbForProject, Event $events) { + ->inject('prepareUserSearch') + ->action(function (string $name, ?\DateTime $requestTimestamp, Response $response, Document $user, Database $dbForProject, Event $events, callable $prepareUserSearch) { - $user - ->setAttribute('name', $name) - ->setAttribute('search', implode(' ', [$user->getId(), $name, $user->getAttribute('email', ''), $user->getAttribute('phone', '')])); + $user->setAttribute('name', $name); + $user->setAttribute('search', $prepareUserSearch($user)); $user = $dbForProject->withRequestTimestamp($requestTimestamp, fn () => $dbForProject->updateDocument('users', $user->getId(), $user)); @@ -1628,7 +1628,8 @@ App::patch('/v1/account/email') ->inject('user') ->inject('dbForProject') ->inject('events') - ->action(function (string $email, string $password, ?\DateTime $requestTimestamp, Response $response, Document $user, Database $dbForProject, Event $events) { + ->inject('prepareUserSearch') + ->action(function (string $email, string $password, ?\DateTime $requestTimestamp, Response $response, Document $user, Database $dbForProject, Event $events, callable $prepareUserSearch) { // passwordUpdate will be empty if the user has never set a password $passwordUpdate = $user->getAttribute('passwordUpdate'); @@ -1644,7 +1645,8 @@ App::patch('/v1/account/email') $user ->setAttribute('email', $email) ->setAttribute('emailVerification', false) // After this user needs to confirm mail again - ->setAttribute('search', implode(' ', [$user->getId(), $user->getAttribute('name', ''), $email, $user->getAttribute('phone', '')])); + ; + $user->setAttribute('search', $prepareUserSearch($user)); if (empty($passwordUpdate)) { $user @@ -1690,7 +1692,8 @@ App::patch('/v1/account/phone') ->inject('user') ->inject('dbForProject') ->inject('events') - ->action(function (string $phone, string $password, ?\DateTime $requestTimestamp, Response $response, Document $user, Database $dbForProject, Event $events) { + ->inject('prepareUserSearch') + ->action(function (string $phone, string $password, ?\DateTime $requestTimestamp, Response $response, Document $user, Database $dbForProject, Event $events, callable $prepareUserSearch) { // passwordUpdate will be empty if the user has never set a password $passwordUpdate = $user->getAttribute('passwordUpdate'); @@ -1704,7 +1707,8 @@ App::patch('/v1/account/phone') $user ->setAttribute('phone', $phone) ->setAttribute('phoneVerification', false) // After this user needs to confirm phone number again - ->setAttribute('search', implode(' ', [$user->getId(), $user->getAttribute('name', ''), $user->getAttribute('email', ''), $phone])); + ; + $user->setAttribute('search', $prepareUserSearch($user)); if (empty($passwordUpdate)) { $user diff --git a/app/controllers/api/users.php b/app/controllers/api/users.php index 4441161e8..dd1c64956 100644 --- a/app/controllers/api/users.php +++ b/app/controllers/api/users.php @@ -686,7 +686,8 @@ App::put('/v1/users/:userId/labels') ->inject('response') ->inject('dbForProject') ->inject('events') - ->action(function (string $userId, array $labels, Response $response, Database $dbForProject, Event $events) { + ->inject('prepareUserSearch') + ->action(function (string $userId, array $labels, Response $response, Database $dbForProject, Event $events, callable $prepareUserSearch) { $user = $dbForProject->getDocument('users', $userId); @@ -695,6 +696,7 @@ App::put('/v1/users/:userId/labels') } $user->setAttribute('labels', (array) \array_values(\array_unique($labels))); + $user->setAttribute('search', $prepareUserSearch($user)); $user = $dbForProject->updateDocument('users', $user->getId(), $user); @@ -797,7 +799,8 @@ App::patch('/v1/users/:userId/name') ->inject('response') ->inject('dbForProject') ->inject('events') - ->action(function (string $userId, string $name, Response $response, Database $dbForProject, Event $events) { + ->inject('prepareUserSearch') + ->action(function (string $userId, string $name, Response $response, Database $dbForProject, Event $events, callable $prepareUserSearch) { $user = $dbForProject->getDocument('users', $userId); @@ -805,10 +808,8 @@ App::patch('/v1/users/:userId/name') throw new Exception(Exception::USER_NOT_FOUND); } - $user - ->setAttribute('name', $name) - ->setAttribute('search', \implode(' ', [$user->getId(), $user->getAttribute('email', ''), $name, $user->getAttribute('phone', '')])); - ; + $user->setAttribute('name', $name); + $user->setAttribute('search', $prepareUserSearch($user)); $user = $dbForProject->updateDocument('users', $user->getId(), $user); @@ -897,7 +898,8 @@ App::patch('/v1/users/:userId/email') ->inject('response') ->inject('dbForProject') ->inject('events') - ->action(function (string $userId, string $email, Response $response, Database $dbForProject, Event $events) { + ->inject('prepareUserSearch') + ->action(function (string $userId, string $email, Response $response, Database $dbForProject, Event $events, callable $prepareUserSearch) { $user = $dbForProject->getDocument('users', $userId); @@ -910,7 +912,9 @@ App::patch('/v1/users/:userId/email') $user ->setAttribute('email', $email) ->setAttribute('emailVerification', false) - ->setAttribute('search', \implode(' ', [$user->getId(), $email, $user->getAttribute('name', ''), $user->getAttribute('phone', '')])); + ; + + $user->setAttribute('search', $prepareUserSearch($user)); try { $user = $dbForProject->updateDocument('users', $user->getId(), $user); @@ -943,7 +947,8 @@ App::patch('/v1/users/:userId/phone') ->inject('response') ->inject('dbForProject') ->inject('events') - ->action(function (string $userId, string $number, Response $response, Database $dbForProject, Event $events) { + ->inject('prepareUserSearch') + ->action(function (string $userId, string $number, Response $response, Database $dbForProject, Event $events, callable $prepareUserSearch) { $user = $dbForProject->getDocument('users', $userId); @@ -954,8 +959,8 @@ App::patch('/v1/users/:userId/phone') $user ->setAttribute('phone', $number) ->setAttribute('phoneVerification', false) - ->setAttribute('search', implode(' ', [$user->getId(), $user->getAttribute('name', ''), $user->getAttribute('email', ''), $number])); ; + $user->setAttribute('search', $prepareUserSearch($user)); try { $user = $dbForProject->updateDocument('users', $user->getId(), $user); diff --git a/app/controllers/shared/api.php b/app/controllers/shared/api.php index 93cf81e1f..fcc12a3a2 100644 --- a/app/controllers/shared/api.php +++ b/app/controllers/shared/api.php @@ -87,6 +87,25 @@ $databaseListener = function (string $event, Document $document, Stats $usage) { } }; +App::setResource('prepareUserSearch', function () { + return function (Document $user): string { + $searchValues = [ + $user->getId(), + $user->getAttribute('email', ''), + $user->getAttribute('name', ''), + $user->getAttribute('phone', '') + ]; + + foreach ($user->getAttribute('labels', []) as $label) { + $searchValues[] = 'label:' . $label; + } + + $search = implode(' ', \array_filter($searchValues)); + + return $search; + }; +}); + App::init() ->groups(['api']) ->inject('utopia')