diff options
author | Joas Schilling <coding@schilljs.com> | 2020-12-02 14:11:47 +0100 |
---|---|---|
committer | Joas Schilling <coding@schilljs.com> | 2020-12-07 14:19:37 +0100 |
commit | fe9c46e595cba2d8e52db13cf15848a1f9d6f141 (patch) | |
tree | 3d52e51948a25b962e74c27dd6dc77ab36b54103 /apps/provisioning_api | |
parent | eaba155a09ea7d226b538c5c8d046d2ea839b452 (diff) | |
download | nextcloud-server-fe9c46e595cba2d8e52db13cf15848a1f9d6f141.tar.gz nextcloud-server-fe9c46e595cba2d8e52db13cf15848a1f9d6f141.zip |
Add an endpoint to search for accounts based on phone number
Signed-off-by: Joas Schilling <coding@schilljs.com>
Diffstat (limited to 'apps/provisioning_api')
-rw-r--r-- | apps/provisioning_api/appinfo/routes.php | 1 | ||||
-rw-r--r-- | apps/provisioning_api/lib/Controller/UsersController.php | 62 | ||||
-rw-r--r-- | apps/provisioning_api/tests/Controller/UsersControllerTest.php | 22 |
3 files changed, 82 insertions, 3 deletions
diff --git a/apps/provisioning_api/appinfo/routes.php b/apps/provisioning_api/appinfo/routes.php index fd1579ca843..912dd82e853 100644 --- a/apps/provisioning_api/appinfo/routes.php +++ b/apps/provisioning_api/appinfo/routes.php @@ -48,6 +48,7 @@ return [ // Users ['root' => '/cloud', 'name' => 'Users#getUsers', 'url' => '/users', 'verb' => 'GET'], ['root' => '/cloud', 'name' => 'Users#getUsersDetails', 'url' => '/users/details', 'verb' => 'GET'], + ['root' => '/cloud', 'name' => 'Users#searchByPhoneNumbers', 'url' => '/users/search/by-phone', 'verb' => 'POST'], ['root' => '/cloud', 'name' => 'Users#addUser', 'url' => '/users', 'verb' => 'POST'], ['root' => '/cloud', 'name' => 'Users#getUser', 'url' => '/users/{userId}', 'verb' => 'GET'], ['root' => '/cloud', 'name' => 'Users#getCurrentUser', 'url' => '/user', 'verb' => 'GET'], diff --git a/apps/provisioning_api/lib/Controller/UsersController.php b/apps/provisioning_api/lib/Controller/UsersController.php index ddde254ed41..08c541ffa6c 100644 --- a/apps/provisioning_api/lib/Controller/UsersController.php +++ b/apps/provisioning_api/lib/Controller/UsersController.php @@ -41,6 +41,10 @@ declare(strict_types=1); namespace OCA\Provisioning_API\Controller; +use libphonenumber\NumberParseException; +use libphonenumber\PhoneNumber; +use libphonenumber\PhoneNumberFormat; +use libphonenumber\PhoneNumberUtil; use OC\Accounts\AccountManager; use OC\Authentication\Token\RemoteWipe; use OC\HintException; @@ -51,11 +55,13 @@ use OCP\App\IAppManager; use OCP\AppFramework\Http\DataResponse; use OCP\AppFramework\OCS\OCSException; use OCP\AppFramework\OCS\OCSForbiddenException; +use OCP\Federation\ICloudIdManager; use OCP\IConfig; use OCP\IGroup; use OCP\IGroupManager; use OCP\ILogger; use OCP\IRequest; +use OCP\IURLGenerator; use OCP\IUser; use OCP\IUserManager; use OCP\IUserSession; @@ -68,6 +74,10 @@ class UsersController extends AUserData { /** @var IAppManager */ private $appManager; + /** @var ICloudIdManager */ + protected $cloudIdManager; + /** @var IURLGenerator */ + protected $urlGenerator; /** @var ILogger */ private $logger; /** @var IFactory */ @@ -91,6 +101,8 @@ class UsersController extends AUserData { IGroupManager $groupManager, IUserSession $userSession, AccountManager $accountManager, + ICloudIdManager $cloudIdManager, + IURLGenerator $urlGenerator, ILogger $logger, IFactory $l10nFactory, NewUserMailHelper $newUserMailHelper, @@ -108,6 +120,8 @@ class UsersController extends AUserData { $l10nFactory); $this->appManager = $appManager; + $this->cloudIdManager = $cloudIdManager; + $this->urlGenerator = $urlGenerator; $this->logger = $logger; $this->l10nFactory = $l10nFactory; $this->newUserMailHelper = $newUserMailHelper; @@ -202,6 +216,54 @@ class UsersController extends AUserData { ]); } + + /** + * @NoAdminRequired + * @NoSubAdminRequired + * + * @param string $location + * @param array $search + * @return DataResponse + */ + public function searchByPhoneNumbers(string $location, array $search): DataResponse { + $phoneUtil = PhoneNumberUtil::getInstance(); + + $normalizedNumberToKey = []; + foreach ($search as $key => $phoneNumbers) { + foreach ($phoneNumbers as $phone) { + try { + $phoneNumber = $phoneUtil->parse($phone, $location); + if ($phoneNumber instanceof PhoneNumber && $phoneUtil->isValidNumber($phoneNumber)) { + $normalizedNumber = $phoneUtil->format($phoneNumber, PhoneNumberFormat::E164); + $normalizedNumberToKey[$normalizedNumber] = (string) $key; + } + } catch (NumberParseException $e) { + } + } + } + + $phoneNumbers = array_keys($normalizedNumberToKey); + + if (empty($phoneNumbers)) { + return new DataResponse(); + } + + $userMatches = $this->accountManager->searchUsers(IAccountManager::PROPERTY_PHONE, $phoneNumbers); + + if (empty($userMatches)) { + return new DataResponse(); + } + + $cloudUrl = $this->urlGenerator->getAbsoluteURL('/'); + + $matches = []; + foreach ($userMatches as $phone => $userId) { + $matches[$normalizedNumberToKey[$phone]] = $this->cloudIdManager->getCloudId($userId, $cloudUrl)->getId(); + } + + return new DataResponse($matches); + } + /** * @throws OCSException */ diff --git a/apps/provisioning_api/tests/Controller/UsersControllerTest.php b/apps/provisioning_api/tests/Controller/UsersControllerTest.php index 8bc60551005..db00963626c 100644 --- a/apps/provisioning_api/tests/Controller/UsersControllerTest.php +++ b/apps/provisioning_api/tests/Controller/UsersControllerTest.php @@ -52,11 +52,13 @@ use OCP\Accounts\IAccountManager; use OCP\App\IAppManager; use OCP\AppFramework\Http\DataResponse; use OCP\EventDispatcher\IEventDispatcher; +use OCP\Federation\ICloudIdManager; use OCP\IConfig; use OCP\IGroup; use OCP\IL10N; use OCP\ILogger; use OCP\IRequest; +use OCP\IURLGenerator; use OCP\IUser; use OCP\IUserManager; use OCP\IUserSession; @@ -86,6 +88,10 @@ class UsersControllerTest extends TestCase { protected $api; /** @var AccountManager|MockObject */ protected $accountManager; + /** @var ICloudIdManager|MockObject */ + protected $cloudIdManager; + /** @var IURLGenerator|MockObject */ + protected $urlGenerator; /** @var IRequest|MockObject */ protected $request; /** @var IFactory|MockObject */ @@ -112,6 +118,8 @@ class UsersControllerTest extends TestCase { $this->logger = $this->createMock(ILogger::class); $this->request = $this->createMock(IRequest::class); $this->accountManager = $this->createMock(AccountManager::class); + $this->cloudIdManager = $this->createMock(ICloudIdManager::class); + $this->urlGenerator = $this->createMock(IURLGenerator::class); $this->l10nFactory = $this->createMock(IFactory::class); $this->newUserMailHelper = $this->createMock(NewUserMailHelper::class); $this->federatedShareProviderFactory = $this->createMock(FederatedShareProviderFactory::class); @@ -129,6 +137,8 @@ class UsersControllerTest extends TestCase { $this->groupManager, $this->userSession, $this->accountManager, + $this->cloudIdManager, + $this->urlGenerator, $this->logger, $this->l10nFactory, $this->newUserMailHelper, @@ -382,7 +392,7 @@ class UsersControllerTest extends TestCase { } public function testAddUserSuccessfulWithDisplayName() { - $api = $this->getMockBuilder('OCA\Provisioning_API\Controller\UsersController') + $api = $this->getMockBuilder(UsersController::class) ->setConstructorArgs([ 'provisioning_api', $this->request, @@ -392,6 +402,8 @@ class UsersControllerTest extends TestCase { $this->groupManager, $this->userSession, $this->accountManager, + $this->cloudIdManager, + $this->urlGenerator, $this->logger, $this->l10nFactory, $this->newUserMailHelper, @@ -3163,7 +3175,7 @@ class UsersControllerTest extends TestCase { ->willReturn($user); /** @var UsersController | MockObject $api */ - $api = $this->getMockBuilder('OCA\Provisioning_API\Controller\UsersController') + $api = $this->getMockBuilder(UsersController::class) ->setConstructorArgs([ 'provisioning_api', $this->request, @@ -3173,6 +3185,8 @@ class UsersControllerTest extends TestCase { $this->groupManager, $this->userSession, $this->accountManager, + $this->cloudIdManager, + $this->urlGenerator, $this->logger, $this->l10nFactory, $this->newUserMailHelper, @@ -3228,7 +3242,7 @@ class UsersControllerTest extends TestCase { public function testGetUser() { /** @var UsersController | MockObject $api */ - $api = $this->getMockBuilder('OCA\Provisioning_API\Controller\UsersController') + $api = $this->getMockBuilder(UsersController::class) ->setConstructorArgs([ 'provisioning_api', $this->request, @@ -3238,6 +3252,8 @@ class UsersControllerTest extends TestCase { $this->groupManager, $this->userSession, $this->accountManager, + $this->cloudIdManager, + $this->urlGenerator, $this->logger, $this->l10nFactory, $this->newUserMailHelper, |