diff options
author | Joas Schilling <coding@schilljs.com> | 2021-03-09 21:22:59 +0100 |
---|---|---|
committer | Joas Schilling <coding@schilljs.com> | 2021-03-10 17:19:55 +0100 |
commit | c7be18c0d6cf6a5af2251abdfa18cdccd11da33b (patch) | |
tree | 13dcd637d5cc1437ec3febf8ba5f809ce11f7c63 /apps/provisioning_api | |
parent | 7baefcfc74d12da8a3850b6747da7c055c56e522 (diff) | |
download | nextcloud-server-c7be18c0d6cf6a5af2251abdfa18cdccd11da33b.tar.gz nextcloud-server-c7be18c0d6cf6a5af2251abdfa18cdccd11da33b.zip |
Add a service to find out if a user knows another user
Signed-off-by: Joas Schilling <coding@schilljs.com>
Diffstat (limited to 'apps/provisioning_api')
3 files changed, 44 insertions, 30 deletions
diff --git a/apps/provisioning_api/lib/Controller/UsersController.php b/apps/provisioning_api/lib/Controller/UsersController.php index 648faf8ad20..d1dba3ea6ee 100644 --- a/apps/provisioning_api/lib/Controller/UsersController.php +++ b/apps/provisioning_api/lib/Controller/UsersController.php @@ -49,8 +49,7 @@ use libphonenumber\PhoneNumberUtil; use OC\Accounts\AccountManager; use OC\Authentication\Token\RemoteWipe; use OC\HintException; -use OC\KnownUser\KnownUser; -use OC\KnownUser\KnownUserMapper; +use OC\KnownUser\KnownUserService; use OCA\Provisioning_API\FederatedShareProviderFactory; use OCA\Settings\Mailer\NewUserMailHelper; use OCP\Accounts\IAccountManager; @@ -92,8 +91,8 @@ class UsersController extends AUserData { private $secureRandom; /** @var RemoteWipe */ private $remoteWipe; - /** @var KnownUserMapper */ - private $knownUserMapper; + /** @var KnownUserService */ + private $knownUserService; /** @var IEventDispatcher */ private $eventDispatcher; @@ -112,7 +111,7 @@ class UsersController extends AUserData { FederatedShareProviderFactory $federatedShareProviderFactory, ISecureRandom $secureRandom, RemoteWipe $remoteWipe, - KnownUserMapper $knownUserMapper, + KnownUserService $knownUserService, IEventDispatcher $eventDispatcher) { parent::__construct($appName, $request, @@ -131,7 +130,7 @@ class UsersController extends AUserData { $this->federatedShareProviderFactory = $federatedShareProviderFactory; $this->secureRandom = $secureRandom; $this->remoteWipe = $remoteWipe; - $this->knownUserMapper = $knownUserMapper; + $this->knownUserService = $knownUserService; $this->eventDispatcher = $eventDispatcher; } @@ -237,6 +236,13 @@ class UsersController extends AUserData { return new DataResponse([], Http::STATUS_BAD_REQUEST); } + /** @var IUser $user */ + $user = $this->userSession->getUser(); + $knownTo = $user->getUID(); + + // Cleanup all previous entries and only allow new matches + $this->knownUserService->deleteKnownTo($knownTo); + $normalizedNumberToKey = []; foreach ($search as $key => $phoneNumbers) { foreach ($phoneNumbers as $phone) { @@ -271,25 +277,10 @@ class UsersController extends AUserData { } $matches = []; - $knownUsers = []; foreach ($userMatches as $phone => $userId) { // Not using the ICloudIdManager as that would run a search for each contact to find the display name in the address book $matches[$normalizedNumberToKey[$phone]] = $userId . '@' . $cloudUrl; - $knownUsers[] = $userId; - } - - /** @var IUser $user */ - $user = $this->userSession->getUser(); - $knownTo = $user->getUID(); - - // Cleanup all previous entries and only allow new matches - $this->knownUserMapper->deleteKnownTo($knownTo); - - foreach ($knownUsers as $knownUser) { - $entity = new KnownUser(); - $entity->setKnownTo($knownTo); - $entity->setKnownUser($knownUser); - $this->knownUserMapper->insert($entity); + $this->knownUserService->storeIsKnownToUser($knownTo, $userId); } return new DataResponse($matches); @@ -701,7 +692,7 @@ class UsersController extends AUserData { $this->accountManager->updateUser($targetUser, $userAccount, true); if ($key === IAccountManager::PROPERTY_PHONE) { - $this->knownUserMapper->deleteKnownUser($targetUser->getUID()); + $this->knownUserService->deleteKnownUser($targetUser->getUID()); } } catch (\InvalidArgumentException $e) { throw new OCSException('Invalid ' . $e->getMessage(), 102); diff --git a/apps/provisioning_api/lib/Listener/UserDeletedListener.php b/apps/provisioning_api/lib/Listener/UserDeletedListener.php index bcbf8cc85b6..f4fdb973080 100644 --- a/apps/provisioning_api/lib/Listener/UserDeletedListener.php +++ b/apps/provisioning_api/lib/Listener/UserDeletedListener.php @@ -23,18 +23,18 @@ declare(strict_types=1); namespace OCA\Provisioning_API\Listener; -use OC\KnownUser\KnownUserMapper; +use OC\KnownUser\KnownUserService; use OCP\EventDispatcher\Event; use OCP\EventDispatcher\IEventListener; use OCP\User\Events\UserDeletedEvent; class UserDeletedListener implements IEventListener { - /** @var KnownUserMapper */ - private $knownUserMapper; + /** @var KnownUserService */ + private $service; - public function __construct(KnownUserMapper $knownUserMapper) { - $this->knownUserMapper = $knownUserMapper; + public function __construct(KnownUserService $service) { + $this->service = $service; } public function handle(Event $event): void { @@ -46,9 +46,9 @@ class UserDeletedListener implements IEventListener { $user = $event->getUser(); // Delete all entries of this user - $this->knownUserMapper->deleteKnownTo($user->getUID()); + $this->service->deleteKnownTo($user->getUID()); // Delete all entries that other users know this user - $this->knownUserMapper->deleteKnownUser($user->getUID()); + $this->service->deleteKnownUser($user->getUID()); } } diff --git a/apps/provisioning_api/tests/Controller/UsersControllerTest.php b/apps/provisioning_api/tests/Controller/UsersControllerTest.php index 3130fce3e9c..d65e3d07913 100644 --- a/apps/provisioning_api/tests/Controller/UsersControllerTest.php +++ b/apps/provisioning_api/tests/Controller/UsersControllerTest.php @@ -44,6 +44,7 @@ use Exception; use OC\Accounts\AccountManager; use OC\Authentication\Token\RemoteWipe; use OC\Group\Manager; +use OC\KnownUser\KnownUserService; use OC\SubAdmin; use OCA\FederatedFileSharing\FederatedShareProvider; use OCA\Provisioning_API\Controller\UsersController; @@ -102,6 +103,8 @@ class UsersControllerTest extends TestCase { private $secureRandom; /** @var RemoteWipe|MockObject */ private $remoteWipe; + /** @var KnownUserService|MockObject */ + private $knownUserService; /** @var IEventDispatcher */ private $eventDispatcher; @@ -122,6 +125,7 @@ class UsersControllerTest extends TestCase { $this->federatedShareProviderFactory = $this->createMock(FederatedShareProviderFactory::class); $this->secureRandom = $this->createMock(ISecureRandom::class); $this->remoteWipe = $this->createMock(RemoteWipe::class); + $this->knownUserService = $this->createMock(KnownUserService::class); $this->eventDispatcher = $this->createMock(IEventDispatcher::class); $this->api = $this->getMockBuilder(UsersController::class) @@ -141,6 +145,7 @@ class UsersControllerTest extends TestCase { $this->federatedShareProviderFactory, $this->secureRandom, $this->remoteWipe, + $this->knownUserService, $this->eventDispatcher, ]) ->setMethods(['fillStorageInfo']) @@ -405,6 +410,7 @@ class UsersControllerTest extends TestCase { $this->federatedShareProviderFactory, $this->secureRandom, $this->remoteWipe, + $this->knownUserService, $this->eventDispatcher, ]) ->setMethods(['editUser']) @@ -1400,6 +1406,13 @@ class UsersControllerTest extends TestCase { * @param array $expected */ public function testSearchByPhoneNumbers(string $location, array $search, int $status, ?array $searchUsers, ?array $userMatches, array $expected) { + $knownTo = 'knownTo'; + $user = $this->createMock(IUser::class); + $user->method('getUID') + ->willReturn($knownTo); + $this->userSession->method('getUser') + ->willReturn($user); + if ($searchUsers === null) { $this->accountManager->expects($this->never()) ->method('searchUsers'); @@ -1408,6 +1421,14 @@ class UsersControllerTest extends TestCase { ->method('searchUsers') ->with(IAccountManager::PROPERTY_PHONE, $searchUsers) ->willReturn($userMatches); + + $this->knownUserService->expects($this->once()) + ->method('deleteKnownTo') + ->with($knownTo); + + $this->knownUserService->expects($this->exactly(count($expected))) + ->method('storeIsKnownToUser') + ->with($knownTo, $this->anything()); } $this->urlGenerator->method('getAbsoluteURL') @@ -3229,6 +3250,7 @@ class UsersControllerTest extends TestCase { $this->federatedShareProviderFactory, $this->secureRandom, $this->remoteWipe, + $this->knownUserService, $this->eventDispatcher, ]) ->setMethods(['getUserData']) @@ -3295,6 +3317,7 @@ class UsersControllerTest extends TestCase { $this->federatedShareProviderFactory, $this->secureRandom, $this->remoteWipe, + $this->knownUserService, $this->eventDispatcher, ]) ->setMethods(['getUserData']) |