diff options
author | Joas Schilling <coding@schilljs.com> | 2021-03-09 21:22:59 +0100 |
---|---|---|
committer | Joas Schilling <coding@schilljs.com> | 2021-03-11 08:39:46 +0100 |
commit | 88855d88273cd413eaf882558ee49ec8ef03ade7 (patch) | |
tree | 2310dfc1a1ac339f44c19612f60a3dca1ca7ee1b /apps | |
parent | 5af22f84b38b5b1951d04e92fb65b6dde7d11fee (diff) | |
download | nextcloud-server-88855d88273cd413eaf882558ee49ec8ef03ade7.tar.gz nextcloud-server-88855d88273cd413eaf882558ee49ec8ef03ade7.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')
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 70450756be7..198d6657468 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; @@ -91,8 +90,8 @@ class UsersController extends AUserData { private $secureRandom; /** @var RemoteWipe */ private $remoteWipe; - /** @var KnownUserMapper */ - private $knownUserMapper; + /** @var KnownUserService */ + private $knownUserService; /** @var IEventDispatcher */ private $eventDispatcher; @@ -111,7 +110,7 @@ class UsersController extends AUserData { FederatedShareProviderFactory $federatedShareProviderFactory, ISecureRandom $secureRandom, RemoteWipe $remoteWipe, - KnownUserMapper $knownUserMapper, + KnownUserService $knownUserService, IEventDispatcher $eventDispatcher) { parent::__construct($appName, $request, @@ -130,7 +129,7 @@ class UsersController extends AUserData { $this->federatedShareProviderFactory = $federatedShareProviderFactory; $this->secureRandom = $secureRandom; $this->remoteWipe = $remoteWipe; - $this->knownUserMapper = $knownUserMapper; + $this->knownUserService = $knownUserService; $this->eventDispatcher = $eventDispatcher; } @@ -236,6 +235,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) { @@ -270,25 +276,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); @@ -688,7 +679,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 10f5a4841d4..39743579b7a 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']) @@ -1399,6 +1405,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'); @@ -1407,6 +1420,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') @@ -3228,6 +3249,7 @@ class UsersControllerTest extends TestCase { $this->federatedShareProviderFactory, $this->secureRandom, $this->remoteWipe, + $this->knownUserService, $this->eventDispatcher, ]) ->setMethods(['getUserData']) @@ -3294,6 +3316,7 @@ class UsersControllerTest extends TestCase { $this->federatedShareProviderFactory, $this->secureRandom, $this->remoteWipe, + $this->knownUserService, $this->eventDispatcher, ]) ->setMethods(['getUserData']) |