Add a service to find out if a user knows another user

Signed-off-by: Joas Schilling <coding@schilljs.com>
This commit is contained in:
Joas Schilling 2021-03-09 21:22:59 +01:00
parent 7baefcfc74
commit c7be18c0d6
No known key found for this signature in database
GPG Key ID: 7076EA9751AACDDA
7 changed files with 121 additions and 30 deletions

View File

@ -49,8 +49,7 @@ use libphonenumber\PhoneNumberUtil;
use OC\Accounts\AccountManager; use OC\Accounts\AccountManager;
use OC\Authentication\Token\RemoteWipe; use OC\Authentication\Token\RemoteWipe;
use OC\HintException; use OC\HintException;
use OC\KnownUser\KnownUser; use OC\KnownUser\KnownUserService;
use OC\KnownUser\KnownUserMapper;
use OCA\Provisioning_API\FederatedShareProviderFactory; use OCA\Provisioning_API\FederatedShareProviderFactory;
use OCA\Settings\Mailer\NewUserMailHelper; use OCA\Settings\Mailer\NewUserMailHelper;
use OCP\Accounts\IAccountManager; use OCP\Accounts\IAccountManager;
@ -92,8 +91,8 @@ class UsersController extends AUserData {
private $secureRandom; private $secureRandom;
/** @var RemoteWipe */ /** @var RemoteWipe */
private $remoteWipe; private $remoteWipe;
/** @var KnownUserMapper */ /** @var KnownUserService */
private $knownUserMapper; private $knownUserService;
/** @var IEventDispatcher */ /** @var IEventDispatcher */
private $eventDispatcher; private $eventDispatcher;
@ -112,7 +111,7 @@ class UsersController extends AUserData {
FederatedShareProviderFactory $federatedShareProviderFactory, FederatedShareProviderFactory $federatedShareProviderFactory,
ISecureRandom $secureRandom, ISecureRandom $secureRandom,
RemoteWipe $remoteWipe, RemoteWipe $remoteWipe,
KnownUserMapper $knownUserMapper, KnownUserService $knownUserService,
IEventDispatcher $eventDispatcher) { IEventDispatcher $eventDispatcher) {
parent::__construct($appName, parent::__construct($appName,
$request, $request,
@ -131,7 +130,7 @@ class UsersController extends AUserData {
$this->federatedShareProviderFactory = $federatedShareProviderFactory; $this->federatedShareProviderFactory = $federatedShareProviderFactory;
$this->secureRandom = $secureRandom; $this->secureRandom = $secureRandom;
$this->remoteWipe = $remoteWipe; $this->remoteWipe = $remoteWipe;
$this->knownUserMapper = $knownUserMapper; $this->knownUserService = $knownUserService;
$this->eventDispatcher = $eventDispatcher; $this->eventDispatcher = $eventDispatcher;
} }
@ -237,6 +236,13 @@ class UsersController extends AUserData {
return new DataResponse([], Http::STATUS_BAD_REQUEST); 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 = []; $normalizedNumberToKey = [];
foreach ($search as $key => $phoneNumbers) { foreach ($search as $key => $phoneNumbers) {
foreach ($phoneNumbers as $phone) { foreach ($phoneNumbers as $phone) {
@ -271,25 +277,10 @@ class UsersController extends AUserData {
} }
$matches = []; $matches = [];
$knownUsers = [];
foreach ($userMatches as $phone => $userId) { 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 // 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; $matches[$normalizedNumberToKey[$phone]] = $userId . '@' . $cloudUrl;
$knownUsers[] = $userId; $this->knownUserService->storeIsKnownToUser($knownTo, $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);
} }
return new DataResponse($matches); return new DataResponse($matches);
@ -701,7 +692,7 @@ class UsersController extends AUserData {
$this->accountManager->updateUser($targetUser, $userAccount, true); $this->accountManager->updateUser($targetUser, $userAccount, true);
if ($key === IAccountManager::PROPERTY_PHONE) { if ($key === IAccountManager::PROPERTY_PHONE) {
$this->knownUserMapper->deleteKnownUser($targetUser->getUID()); $this->knownUserService->deleteKnownUser($targetUser->getUID());
} }
} catch (\InvalidArgumentException $e) { } catch (\InvalidArgumentException $e) {
throw new OCSException('Invalid ' . $e->getMessage(), 102); throw new OCSException('Invalid ' . $e->getMessage(), 102);

View File

@ -23,18 +23,18 @@ declare(strict_types=1);
namespace OCA\Provisioning_API\Listener; namespace OCA\Provisioning_API\Listener;
use OC\KnownUser\KnownUserMapper; use OC\KnownUser\KnownUserService;
use OCP\EventDispatcher\Event; use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener; use OCP\EventDispatcher\IEventListener;
use OCP\User\Events\UserDeletedEvent; use OCP\User\Events\UserDeletedEvent;
class UserDeletedListener implements IEventListener { class UserDeletedListener implements IEventListener {
/** @var KnownUserMapper */ /** @var KnownUserService */
private $knownUserMapper; private $service;
public function __construct(KnownUserMapper $knownUserMapper) { public function __construct(KnownUserService $service) {
$this->knownUserMapper = $knownUserMapper; $this->service = $service;
} }
public function handle(Event $event): void { public function handle(Event $event): void {
@ -46,9 +46,9 @@ class UserDeletedListener implements IEventListener {
$user = $event->getUser(); $user = $event->getUser();
// Delete all entries of this user // 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 // Delete all entries that other users know this user
$this->knownUserMapper->deleteKnownUser($user->getUID()); $this->service->deleteKnownUser($user->getUID());
} }
} }

View File

@ -44,6 +44,7 @@ use Exception;
use OC\Accounts\AccountManager; use OC\Accounts\AccountManager;
use OC\Authentication\Token\RemoteWipe; use OC\Authentication\Token\RemoteWipe;
use OC\Group\Manager; use OC\Group\Manager;
use OC\KnownUser\KnownUserService;
use OC\SubAdmin; use OC\SubAdmin;
use OCA\FederatedFileSharing\FederatedShareProvider; use OCA\FederatedFileSharing\FederatedShareProvider;
use OCA\Provisioning_API\Controller\UsersController; use OCA\Provisioning_API\Controller\UsersController;
@ -102,6 +103,8 @@ class UsersControllerTest extends TestCase {
private $secureRandom; private $secureRandom;
/** @var RemoteWipe|MockObject */ /** @var RemoteWipe|MockObject */
private $remoteWipe; private $remoteWipe;
/** @var KnownUserService|MockObject */
private $knownUserService;
/** @var IEventDispatcher */ /** @var IEventDispatcher */
private $eventDispatcher; private $eventDispatcher;
@ -122,6 +125,7 @@ class UsersControllerTest extends TestCase {
$this->federatedShareProviderFactory = $this->createMock(FederatedShareProviderFactory::class); $this->federatedShareProviderFactory = $this->createMock(FederatedShareProviderFactory::class);
$this->secureRandom = $this->createMock(ISecureRandom::class); $this->secureRandom = $this->createMock(ISecureRandom::class);
$this->remoteWipe = $this->createMock(RemoteWipe::class); $this->remoteWipe = $this->createMock(RemoteWipe::class);
$this->knownUserService = $this->createMock(KnownUserService::class);
$this->eventDispatcher = $this->createMock(IEventDispatcher::class); $this->eventDispatcher = $this->createMock(IEventDispatcher::class);
$this->api = $this->getMockBuilder(UsersController::class) $this->api = $this->getMockBuilder(UsersController::class)
@ -141,6 +145,7 @@ class UsersControllerTest extends TestCase {
$this->federatedShareProviderFactory, $this->federatedShareProviderFactory,
$this->secureRandom, $this->secureRandom,
$this->remoteWipe, $this->remoteWipe,
$this->knownUserService,
$this->eventDispatcher, $this->eventDispatcher,
]) ])
->setMethods(['fillStorageInfo']) ->setMethods(['fillStorageInfo'])
@ -405,6 +410,7 @@ class UsersControllerTest extends TestCase {
$this->federatedShareProviderFactory, $this->federatedShareProviderFactory,
$this->secureRandom, $this->secureRandom,
$this->remoteWipe, $this->remoteWipe,
$this->knownUserService,
$this->eventDispatcher, $this->eventDispatcher,
]) ])
->setMethods(['editUser']) ->setMethods(['editUser'])
@ -1400,6 +1406,13 @@ class UsersControllerTest extends TestCase {
* @param array $expected * @param array $expected
*/ */
public function testSearchByPhoneNumbers(string $location, array $search, int $status, ?array $searchUsers, ?array $userMatches, 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) { if ($searchUsers === null) {
$this->accountManager->expects($this->never()) $this->accountManager->expects($this->never())
->method('searchUsers'); ->method('searchUsers');
@ -1408,6 +1421,14 @@ class UsersControllerTest extends TestCase {
->method('searchUsers') ->method('searchUsers')
->with(IAccountManager::PROPERTY_PHONE, $searchUsers) ->with(IAccountManager::PROPERTY_PHONE, $searchUsers)
->willReturn($userMatches); ->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') $this->urlGenerator->method('getAbsoluteURL')
@ -3229,6 +3250,7 @@ class UsersControllerTest extends TestCase {
$this->federatedShareProviderFactory, $this->federatedShareProviderFactory,
$this->secureRandom, $this->secureRandom,
$this->remoteWipe, $this->remoteWipe,
$this->knownUserService,
$this->eventDispatcher, $this->eventDispatcher,
]) ])
->setMethods(['getUserData']) ->setMethods(['getUserData'])
@ -3295,6 +3317,7 @@ class UsersControllerTest extends TestCase {
$this->federatedShareProviderFactory, $this->federatedShareProviderFactory,
$this->secureRandom, $this->secureRandom,
$this->remoteWipe, $this->remoteWipe,
$this->knownUserService,
$this->eventDispatcher, $this->eventDispatcher,
]) ])
->setMethods(['getUserData']) ->setMethods(['getUserData'])

View File

@ -1168,6 +1168,7 @@ return array(
'OC\\IntegrityCheck\\Iterator\\ExcludeFoldersByPathFilterIterator' => $baseDir . '/lib/private/IntegrityCheck/Iterator/ExcludeFoldersByPathFilterIterator.php', 'OC\\IntegrityCheck\\Iterator\\ExcludeFoldersByPathFilterIterator' => $baseDir . '/lib/private/IntegrityCheck/Iterator/ExcludeFoldersByPathFilterIterator.php',
'OC\\KnownUser\\KnownUser' => $baseDir . '/lib/private/KnownUser/KnownUser.php', 'OC\\KnownUser\\KnownUser' => $baseDir . '/lib/private/KnownUser/KnownUser.php',
'OC\\KnownUser\\KnownUserMapper' => $baseDir . '/lib/private/KnownUser/KnownUserMapper.php', 'OC\\KnownUser\\KnownUserMapper' => $baseDir . '/lib/private/KnownUser/KnownUserMapper.php',
'OC\\KnownUser\\KnownUserService' => $baseDir . '/lib/private/KnownUser/KnownUserService.php',
'OC\\L10N\\Factory' => $baseDir . '/lib/private/L10N/Factory.php', 'OC\\L10N\\Factory' => $baseDir . '/lib/private/L10N/Factory.php',
'OC\\L10N\\L10N' => $baseDir . '/lib/private/L10N/L10N.php', 'OC\\L10N\\L10N' => $baseDir . '/lib/private/L10N/L10N.php',
'OC\\L10N\\L10NString' => $baseDir . '/lib/private/L10N/L10NString.php', 'OC\\L10N\\L10NString' => $baseDir . '/lib/private/L10N/L10NString.php',

View File

@ -1197,6 +1197,7 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c
'OC\\IntegrityCheck\\Iterator\\ExcludeFoldersByPathFilterIterator' => __DIR__ . '/../../..' . '/lib/private/IntegrityCheck/Iterator/ExcludeFoldersByPathFilterIterator.php', 'OC\\IntegrityCheck\\Iterator\\ExcludeFoldersByPathFilterIterator' => __DIR__ . '/../../..' . '/lib/private/IntegrityCheck/Iterator/ExcludeFoldersByPathFilterIterator.php',
'OC\\KnownUser\\KnownUser' => __DIR__ . '/../../..' . '/lib/private/KnownUser/KnownUser.php', 'OC\\KnownUser\\KnownUser' => __DIR__ . '/../../..' . '/lib/private/KnownUser/KnownUser.php',
'OC\\KnownUser\\KnownUserMapper' => __DIR__ . '/../../..' . '/lib/private/KnownUser/KnownUserMapper.php', 'OC\\KnownUser\\KnownUserMapper' => __DIR__ . '/../../..' . '/lib/private/KnownUser/KnownUserMapper.php',
'OC\\KnownUser\\KnownUserService' => __DIR__ . '/../../..' . '/lib/private/KnownUser/KnownUserService.php',
'OC\\L10N\\Factory' => __DIR__ . '/../../..' . '/lib/private/L10N/Factory.php', 'OC\\L10N\\Factory' => __DIR__ . '/../../..' . '/lib/private/L10N/Factory.php',
'OC\\L10N\\L10N' => __DIR__ . '/../../..' . '/lib/private/L10N/L10N.php', 'OC\\L10N\\L10N' => __DIR__ . '/../../..' . '/lib/private/L10N/L10N.php',
'OC\\L10N\\L10NString' => __DIR__ . '/../../..' . '/lib/private/L10N/L10NString.php', 'OC\\L10N\\L10NString' => __DIR__ . '/../../..' . '/lib/private/L10N/L10NString.php',

View File

@ -62,6 +62,19 @@ class KnownUserMapper extends QBMapper {
return (int) $query->execute(); return (int) $query->execute();
} }
/**
* @param string $knownTo
* @return KnownUser[]
*/
public function getKnownTo(string $knownTo): array {
$query = $this->db->getQueryBuilder();
$query->select('*')
->from($this->getTableName())
->where($query->expr()->eq('known_to', $query->createNamedParameter($knownTo)));
return $this->findEntities($query);
}
public function createKnownUserFromRow(array $row): KnownUser { public function createKnownUserFromRow(array $row): KnownUser {
return $this->mapRowToEntity([ return $this->mapRowToEntity([
'id' => $row['s_id'], 'id' => $row['s_id'],

View File

@ -0,0 +1,62 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2021 Joas Schilling <coding@schilljs.com>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OC\KnownUser;
class KnownUserService {
/** @var KnownUserMapper */
protected $mapper;
/** @var array */
protected $knownUsers = [];
public function __construct(KnownUserMapper $mapper) {
$this->mapper = $mapper;
}
public function deleteKnownTo(string $knownTo): int {
return $this->mapper->deleteKnownTo($knownTo);
}
public function deleteKnownUser(string $knownUser): int {
return $this->mapper->deleteKnownUser($knownUser);
}
public function storeIsKnownToUser(string $knownTo, string $knownUser): void {
$entity = new KnownUser();
$entity->setKnownTo($knownTo);
$entity->setKnownUser($knownUser);
$this->mapper->insert($entity);
}
public function isKnownToUser(string $knownTo, string $user): bool {
if (!isset($this->knownUsers[$knownTo])) {
$entities = $this->mapper->getKnownTo($knownTo);
$this->knownUsers[$knownTo] = [];
foreach ($entities as $entity) {
$this->knownUsers[$knownTo][$entity->getKnownUser()] = true;
}
}
return isset($this->knownUsers[$knownTo][$user]);
}
}