Skip to content

Commit

Permalink
TEMP!!!!
Browse files Browse the repository at this point in the history
Restrict autocompletion also based on the phonebook known users

Signed-off-by: Joas Schilling <coding@schilljs.com>
  • Loading branch information
nickvergessen committed Mar 9, 2021
1 parent af9baf7 commit cfbcd82
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 23 deletions.
5 changes: 3 additions & 2 deletions apps/dav/lib/CardDAV/SystemAddressbook.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@ public function __construct(BackendInterface $carddavBackend, array $addressBook

public function getChildren() {
$shareEnumeration = $this->config->getAppValue('core', 'shareapi_allow_share_dialog_user_enumeration', 'yes') === 'yes';
$restrictShareEnumeration = $this->config->getAppValue('core', 'shareapi_restrict_user_enumeration_to_group', 'no') === 'yes';
if (!$shareEnumeration || ($shareEnumeration && $restrictShareEnumeration)) {
$shareEnumerationGroup = $this->config->getAppValue('core', 'shareapi_restrict_user_enumeration_to_group', 'no') === 'yes';
$shareEnumerationPhone = $this->config->getAppValue('core', 'shareapi_restrict_user_enumeration_to_phone', 'no') === 'yes';
if (!$shareEnumeration || ($shareEnumeration && ($shareEnumerationGroup || $shareEnumerationPhone))) {
return [];
}

Expand Down
33 changes: 27 additions & 6 deletions lib/private/Collaboration/Collaborators/MailPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

namespace OC\Collaboration\Collaborators;

use OCA\Settings\Service\KnownUserService;
use OCP\Collaboration\Collaborators\ISearchPlugin;
use OCP\Collaboration\Collaborators\ISearchResult;
use OCP\Collaboration\Collaborators\SearchResultType;
Expand All @@ -40,8 +41,14 @@
use OCP\Share\IShare;

class MailPlugin implements ISearchPlugin {
protected $shareeEnumeration;
/* @var bool */
protected $shareWithGroupOnly;
/* @var bool */
protected $shareeEnumeration;
/* @var bool */
protected $shareeEnumerationInGroupOnly;
/* @var bool */
protected $shareeEnumerationPhone;

/** @var IManager */
private $contactsManager;
Expand All @@ -52,20 +59,28 @@ class MailPlugin implements ISearchPlugin {

/** @var IGroupManager */
private $groupManager;

/** @var KnownUserService */
private $knownUserService;
/** @var IUserSession */
private $userSession;

public function __construct(IManager $contactsManager, ICloudIdManager $cloudIdManager, IConfig $config, IGroupManager $groupManager, IUserSession $userSession) {
public function __construct(IManager $contactsManager,
ICloudIdManager $cloudIdManager,
IConfig $config,
IGroupManager $groupManager,
KnownUserService $knownUserService,
IUserSession $userSession) {
$this->contactsManager = $contactsManager;
$this->cloudIdManager = $cloudIdManager;
$this->config = $config;
$this->groupManager = $groupManager;
$this->knownUserService = $knownUserService;
$this->userSession = $userSession;

$this->shareeEnumeration = $this->config->getAppValue('core', 'shareapi_allow_share_dialog_user_enumeration', 'yes') === 'yes';
$this->shareWithGroupOnly = $this->config->getAppValue('core', 'shareapi_only_share_with_group_members', 'no') === 'yes';
$this->shareeEnumerationInGroupOnly = $this->shareeEnumeration && $this->config->getAppValue('core', 'shareapi_restrict_user_enumeration_to_group', 'no') === 'yes';
$this->shareeEnumerationPhone = $this->shareeEnumeration && $this->config->getAppValue('core', 'shareapi_restrict_user_enumeration_to_phone', 'no') === 'yes';
}

/**
Expand All @@ -77,6 +92,8 @@ public function __construct(IManager $contactsManager, ICloudIdManager $cloudIdM
* @since 13.0.0
*/
public function search($search, $limit, $offset, ISearchResult $searchResult) {
$currentUserId = $this->userSession->getUser()->getUID();

$result = $userResults = ['wide' => [], 'exact' => []];
$userType = new SearchResultType('users');
$emailType = new SearchResultType('emails');
Expand Down Expand Up @@ -152,8 +169,12 @@ public function search($search, $limit, $offset, ISearchResult $searchResult) {
continue;
}

$addToWide = !$this->shareeEnumerationInGroupOnly;
if ($this->shareeEnumerationInGroupOnly) {
$addToWide = !($this->shareeEnumerationInGroupOnly || $this->shareeEnumerationPhone);
if (!$addToWide && $this->shareeEnumerationPhone && $this->knownUserService->isKnownToUser($currentUserId, $contact['UID'])) {
$addToWide = true;
}

if (!$addToWide && $this->shareeEnumerationInGroupOnly) {
$addToWide = false;
$userGroups = $this->groupManager->getUserGroupIds($this->userSession->getUser());
foreach ($userGroups as $userGroup) {
Expand Down Expand Up @@ -181,7 +202,7 @@ public function search($search, $limit, $offset, ISearchResult $searchResult) {
}

if ($exactEmailMatch
|| isset($contact['FN']) && strtolower($contact['FN']) === $lowerSearch) {
|| (isset($contact['FN']) && strtolower($contact['FN']) === $lowerSearch)) {
if ($exactEmailMatch) {
$searchResult->markExactIdMatch($emailType);
}
Expand Down
29 changes: 18 additions & 11 deletions lib/private/Collaboration/Collaborators/UserPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

namespace OC\Collaboration\Collaborators;

use OCA\Settings\Service\KnownUserService;
use OCP\Collaboration\Collaborators\ISearchPlugin;
use OCP\Collaboration\Collaborators\ISearchResult;
use OCP\Collaboration\Collaborators\SearchResultType;
Expand All @@ -46,8 +47,12 @@
class UserPlugin implements ISearchPlugin {
/* @var bool */
protected $shareWithGroupOnly;
/* @var bool */
protected $shareeEnumeration;
/* @var bool */
protected $shareeEnumerationInGroupOnly;
/* @var bool */
protected $shareeEnumerationPhone;

/** @var IConfig */
private $config;
Expand All @@ -57,40 +62,37 @@ class UserPlugin implements ISearchPlugin {
private $userSession;
/** @var IUserManager */
private $userManager;
/** @var KnownUserService */
private $knownUserService;
/** @var IUserStatusManager */
private $userStatusManager;

/**
* UserPlugin constructor.
*
* @param IConfig $config
* @param IUserManager $userManager
* @param IGroupManager $groupManager
* @param IUserSession $userSession
* @param IUserStatusManager $userStatusManager
*/
public function __construct(IConfig $config,
IUserManager $userManager,
IGroupManager $groupManager,
IUserSession $userSession,
KnownUserService $knownUserService,
IUserStatusManager $userStatusManager) {
$this->config = $config;

$this->groupManager = $groupManager;
$this->userSession = $userSession;
$this->userManager = $userManager;
$this->knownUserService = $knownUserService;
$this->userStatusManager = $userStatusManager;

$this->shareWithGroupOnly = $this->config->getAppValue('core', 'shareapi_only_share_with_group_members', 'no') === 'yes';
$this->shareeEnumeration = $this->config->getAppValue('core', 'shareapi_allow_share_dialog_user_enumeration', 'yes') === 'yes';
$this->shareeEnumerationInGroupOnly = $this->shareeEnumeration && $this->config->getAppValue('core', 'shareapi_restrict_user_enumeration_to_group', 'no') === 'yes';
$this->shareeEnumerationPhone = $this->shareeEnumeration && $this->config->getAppValue('core', 'shareapi_restrict_user_enumeration_to_phone', 'no') === 'yes';
}

public function search($search, $limit, $offset, ISearchResult $searchResult) {
$result = ['wide' => [], 'exact' => []];
$users = [];
$hasMoreResults = false;

$currentUserId = $this->userSession->getUser()->getUID();
$currentUserGroups = $this->groupManager->getUserGroupIds($this->userSession->getUser());
if ($this->shareWithGroupOnly) {
// Search in all the groups this user is part of
Expand Down Expand Up @@ -168,11 +170,16 @@ public function search($search, $limit, $offset, ISearchResult $searchResult) {
];
} else {
$addToWideResults = false;
if ($this->shareeEnumeration && !$this->shareeEnumerationInGroupOnly) {
if ($this->shareeEnumeration &&
!($this->shareeEnumerationInGroupOnly || $this->shareeEnumerationPhone)) {
$addToWideResults = true;
}

if ($this->shareeEnumerationPhone && $this->knownUserService->isKnownToUser($currentUserId, $user->getUID())) {
$addToWideResults = true;
}

if ($this->shareeEnumerationInGroupOnly) {
if (!$addToWideResults && $this->shareeEnumerationInGroupOnly) {
$commonGroups = array_intersect($currentUserGroups, $this->groupManager->getUserGroupIds($user));
if (!empty($commonGroups)) {
$addToWideResults = true;
Expand Down
8 changes: 4 additions & 4 deletions lib/private/Contacts/ContactsMenu/ContactsStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public function getContacts(IUser $user, $filter, ?int $limit = null, ?int $offs
}

/**
* Filters the contacts. Applies 3 filters:
* Filters the contacts. Applied filters:
* 1. filter the current user
* 2. if the `shareapi_allow_share_dialog_user_enumeration` config option is
* enabled it will filter all local users
Expand Down Expand Up @@ -172,13 +172,13 @@ private function filterContacts(IUser $self,
}

if ($ownGroupsOnly && $entry->getProperty('isLocalSystemBook') === true) {
$uid = $this->userManager->get($entry->getProperty('UID'));
$user = $this->userManager->get($entry->getProperty('UID'));

if ($uid === null) {
if ($user === null) {
return false;
}

$contactGroups = $this->groupManager->getUserGroupIds($uid);
$contactGroups = $this->groupManager->getUserGroupIds($user);
if (count(array_intersect($contactGroups, $selfGroups)) === 0) {
// no groups in common, so shouldn't see the contact
return false;
Expand Down

0 comments on commit cfbcd82

Please sign in to comment.