Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add a service to find out if a user knows another user
Signed-off-by: Joas Schilling <coding@schilljs.com>
  • Loading branch information
nickvergessen committed Mar 10, 2021
commit c7be18c0d6cf6a5af2251abdfa18cdccd11da33b
37 changes: 14 additions & 23 deletions apps/provisioning_api/lib/Controller/UsersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@
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;
Expand Down Expand Up @@ -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;

Expand All @@ -112,7 +111,7 @@ public function __construct(string $appName,
FederatedShareProviderFactory $federatedShareProviderFactory,
ISecureRandom $secureRandom,
RemoteWipe $remoteWipe,
KnownUserMapper $knownUserMapper,
KnownUserService $knownUserService,
IEventDispatcher $eventDispatcher) {
parent::__construct($appName,
$request,
Expand All @@ -131,7 +130,7 @@ public function __construct(string $appName,
$this->federatedShareProviderFactory = $federatedShareProviderFactory;
$this->secureRandom = $secureRandom;
$this->remoteWipe = $remoteWipe;
$this->knownUserMapper = $knownUserMapper;
$this->knownUserService = $knownUserService;
$this->eventDispatcher = $eventDispatcher;
}

Expand Down Expand Up @@ -237,6 +236,13 @@ public function searchByPhoneNumbers(string $location, array $search): DataRespo
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) {
Expand Down Expand Up @@ -271,25 +277,10 @@ public function searchByPhoneNumbers(string $location, array $search): DataRespo
}

$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);
Expand Down Expand Up @@ -701,7 +692,7 @@ public function editUser(string $userId, string $key, string $value): DataRespon
$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);
Expand Down
14 changes: 7 additions & 7 deletions apps/provisioning_api/lib/Listener/UserDeletedListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,18 @@

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 {
Expand All @@ -46,9 +46,9 @@ public function handle(Event $event): void {
$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());
}
}
23 changes: 23 additions & 0 deletions apps/provisioning_api/tests/Controller/UsersControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
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;
Expand Down Expand Up @@ -102,6 +103,8 @@ class UsersControllerTest extends TestCase {
private $secureRandom;
/** @var RemoteWipe|MockObject */
private $remoteWipe;
/** @var KnownUserService|MockObject */
private $knownUserService;
/** @var IEventDispatcher */
private $eventDispatcher;

Expand All @@ -122,6 +125,7 @@ protected function setUp(): void {
$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)
Expand All @@ -141,6 +145,7 @@ protected function setUp(): void {
$this->federatedShareProviderFactory,
$this->secureRandom,
$this->remoteWipe,
$this->knownUserService,
$this->eventDispatcher,
])
->setMethods(['fillStorageInfo'])
Expand Down Expand Up @@ -405,6 +410,7 @@ public function testAddUserSuccessfulWithDisplayName() {
$this->federatedShareProviderFactory,
$this->secureRandom,
$this->remoteWipe,
$this->knownUserService,
$this->eventDispatcher,
])
->setMethods(['editUser'])
Expand Down Expand Up @@ -1400,6 +1406,13 @@ public function dataSearchByPhoneNumbers(): array {
* @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');
Expand All @@ -1408,6 +1421,14 @@ public function testSearchByPhoneNumbers(string $location, array $search, int $s
->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')
Expand Down Expand Up @@ -3229,6 +3250,7 @@ public function testGetCurrentUserLoggedIn() {
$this->federatedShareProviderFactory,
$this->secureRandom,
$this->remoteWipe,
$this->knownUserService,
$this->eventDispatcher,
])
->setMethods(['getUserData'])
Expand Down Expand Up @@ -3295,6 +3317,7 @@ public function testGetUser() {
$this->federatedShareProviderFactory,
$this->secureRandom,
$this->remoteWipe,
$this->knownUserService,
$this->eventDispatcher,
])
->setMethods(['getUserData'])
Expand Down
1 change: 1 addition & 0 deletions lib/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -1168,6 +1168,7 @@
'OC\\IntegrityCheck\\Iterator\\ExcludeFoldersByPathFilterIterator' => $baseDir . '/lib/private/IntegrityCheck/Iterator/ExcludeFoldersByPathFilterIterator.php',
'OC\\KnownUser\\KnownUser' => $baseDir . '/lib/private/KnownUser/KnownUser.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\\L10N' => $baseDir . '/lib/private/L10N/L10N.php',
'OC\\L10N\\L10NString' => $baseDir . '/lib/private/L10N/L10NString.php',
Expand Down
1 change: 1 addition & 0 deletions lib/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -1197,6 +1197,7 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c
'OC\\IntegrityCheck\\Iterator\\ExcludeFoldersByPathFilterIterator' => __DIR__ . '/../../..' . '/lib/private/IntegrityCheck/Iterator/ExcludeFoldersByPathFilterIterator.php',
'OC\\KnownUser\\KnownUser' => __DIR__ . '/../../..' . '/lib/private/KnownUser/KnownUser.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\\L10N' => __DIR__ . '/../../..' . '/lib/private/L10N/L10N.php',
'OC\\L10N\\L10NString' => __DIR__ . '/../../..' . '/lib/private/L10N/L10NString.php',
Expand Down
13 changes: 13 additions & 0 deletions lib/private/KnownUser/KnownUserMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,19 @@ public function deleteKnownUser(string $knownUser): int {
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 {
return $this->mapRowToEntity([
'id' => $row['s_id'],
Expand Down
62 changes: 62 additions & 0 deletions lib/private/KnownUser/KnownUserService.php
Original file line number Diff line number Diff line change
@@ -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]);
}
}