2017-01-24 09:47:14 +03:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @copyright 2017 Christoph Wurst <christoph@winzerhof-wurst.at>
|
2017-09-15 16:58:04 +03:00
|
|
|
* @copyright 2017 Lukas Reschke <lukas@statuscode.ch>
|
2017-01-24 09:47:14 +03:00
|
|
|
*
|
2020-08-24 15:54:25 +03:00
|
|
|
* @author Arthur Schiwon <blizzz@arthur-schiwon.de>
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
|
2020-04-29 12:57:22 +03:00
|
|
|
* @author Daniel Calviño Sánchez <danxuliu@gmail.com>
|
2017-11-06 22:15:27 +03:00
|
|
|
* @author Georg Ehrke <oc.list@georgehrke.com>
|
2020-03-31 11:49:10 +03:00
|
|
|
* @author Julius Härtl <jus@bitgrid.net>
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Lukas Reschke <lukas@statuscode.ch>
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Tobia De Koninck <tobia@ledfan.be>
|
2017-01-24 09:47:14 +03:00
|
|
|
*
|
|
|
|
* @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
|
2019-12-03 21:57:53 +03:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2017-01-24 09:47:14 +03:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OC\Contacts\ContactsMenu;
|
|
|
|
|
2019-11-22 22:52:10 +03:00
|
|
|
use OCP\Contacts\ContactsMenu\IContactsStore;
|
2017-01-24 09:47:14 +03:00
|
|
|
use OCP\Contacts\ContactsMenu\IEntry;
|
|
|
|
use OCP\Contacts\IManager;
|
2017-07-02 12:02:18 +03:00
|
|
|
use OCP\IConfig;
|
|
|
|
use OCP\IGroupManager;
|
2017-04-10 17:49:26 +03:00
|
|
|
use OCP\IUser;
|
2017-07-02 12:02:18 +03:00
|
|
|
use OCP\IUserManager;
|
2017-01-24 09:47:14 +03:00
|
|
|
|
2017-09-16 14:42:46 +03:00
|
|
|
class ContactsStore implements IContactsStore {
|
2017-01-24 09:47:14 +03:00
|
|
|
|
|
|
|
/** @var IManager */
|
|
|
|
private $contactsManager;
|
|
|
|
|
2017-07-02 12:02:18 +03:00
|
|
|
/** @var IConfig */
|
|
|
|
private $config;
|
|
|
|
|
|
|
|
/** @var IUserManager */
|
|
|
|
private $userManager;
|
|
|
|
|
|
|
|
/** @var IGroupManager */
|
|
|
|
private $groupManager;
|
|
|
|
|
2017-01-24 09:47:14 +03:00
|
|
|
/**
|
|
|
|
* @param IManager $contactsManager
|
2017-07-02 12:02:18 +03:00
|
|
|
* @param IConfig $config
|
|
|
|
* @param IUserManager $userManager
|
|
|
|
* @param IGroupManager $groupManager
|
2017-01-24 09:47:14 +03:00
|
|
|
*/
|
2017-09-15 16:58:04 +03:00
|
|
|
public function __construct(IManager $contactsManager,
|
|
|
|
IConfig $config,
|
|
|
|
IUserManager $userManager,
|
|
|
|
IGroupManager $groupManager) {
|
2017-01-24 09:47:14 +03:00
|
|
|
$this->contactsManager = $contactsManager;
|
2017-07-02 12:02:18 +03:00
|
|
|
$this->config = $config;
|
|
|
|
$this->userManager = $userManager;
|
|
|
|
$this->groupManager = $groupManager;
|
2017-01-24 09:47:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-04-10 17:49:26 +03:00
|
|
|
* @param IUser $user
|
2017-01-24 09:47:14 +03:00
|
|
|
* @param string|null $filter
|
|
|
|
* @return IEntry[]
|
|
|
|
*/
|
2020-07-30 13:50:04 +03:00
|
|
|
public function getContacts(IUser $user, $filter, ?int $limit = null, ?int $offset = null) {
|
|
|
|
$options = [];
|
|
|
|
if ($limit !== null) {
|
|
|
|
$options['limit'] = $limit;
|
|
|
|
}
|
|
|
|
if ($offset !== null) {
|
|
|
|
$options['offset'] = $offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
$allContacts = $this->contactsManager->search(
|
|
|
|
$filter ?: '',
|
|
|
|
[
|
|
|
|
'FN',
|
|
|
|
'EMAIL'
|
|
|
|
],
|
|
|
|
$options
|
|
|
|
);
|
2017-01-24 09:47:14 +03:00
|
|
|
|
2020-04-09 14:53:40 +03:00
|
|
|
$entries = array_map(function (array $contact) {
|
2017-01-24 09:47:14 +03:00
|
|
|
return $this->contactArrayToEntry($contact);
|
|
|
|
}, $allContacts);
|
2017-09-15 16:58:04 +03:00
|
|
|
return $this->filterContacts(
|
|
|
|
$user,
|
|
|
|
$entries,
|
|
|
|
$filter
|
|
|
|
);
|
2017-07-02 12:02:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-09-15 16:58:04 +03:00
|
|
|
* Filters the contacts. Applies 3 filters:
|
2017-07-02 12:02:18 +03:00
|
|
|
* 1. filter the current user
|
2017-09-15 16:58:04 +03:00
|
|
|
* 2. if the `shareapi_allow_share_dialog_user_enumeration` config option is
|
|
|
|
* enabled it will filter all local users
|
|
|
|
* 3. if the `shareapi_exclude_groups` config option is enabled and the
|
2017-07-02 12:02:18 +03:00
|
|
|
* current user is in an excluded group it will filter all local users.
|
2017-09-15 16:58:04 +03:00
|
|
|
* 4. if the `shareapi_only_share_with_group_members` config option is
|
2017-07-02 12:02:18 +03:00
|
|
|
* enabled it will filter all users which doens't have a common group
|
|
|
|
* with the current user.
|
2017-09-15 16:58:04 +03:00
|
|
|
*
|
2017-07-02 12:02:18 +03:00
|
|
|
* @param IUser $self
|
2017-08-05 16:21:14 +03:00
|
|
|
* @param Entry[] $entries
|
2017-09-15 16:58:04 +03:00
|
|
|
* @param string $filter
|
2017-08-05 16:21:14 +03:00
|
|
|
* @return Entry[] the filtered contacts
|
2017-07-02 12:02:18 +03:00
|
|
|
*/
|
2017-09-15 16:58:04 +03:00
|
|
|
private function filterContacts(IUser $self,
|
|
|
|
array $entries,
|
|
|
|
$filter) {
|
|
|
|
$disallowEnumeration = $this->config->getAppValue('core', 'shareapi_allow_share_dialog_user_enumeration', 'yes') !== 'yes';
|
2020-03-24 16:30:59 +03:00
|
|
|
$restrictEnumeration = $this->config->getAppValue('core', 'shareapi_restrict_user_enumeration_to_group', 'no') === 'yes';
|
2017-07-08 00:12:28 +03:00
|
|
|
$excludedGroups = $this->config->getAppValue('core', 'shareapi_exclude_groups', 'no') === 'yes';
|
2017-07-02 12:02:18 +03:00
|
|
|
|
2017-08-05 16:21:14 +03:00
|
|
|
// whether to filter out local users
|
|
|
|
$skipLocal = false;
|
|
|
|
// whether to filter out all users which doesn't have the same group as the current user
|
2020-02-18 21:44:49 +03:00
|
|
|
$ownGroupsOnly = $this->config->getAppValue('core', 'shareapi_only_share_with_group_members', 'no') === 'yes' || $restrictEnumeration;
|
2017-07-02 12:02:18 +03:00
|
|
|
|
|
|
|
$selfGroups = $this->groupManager->getUserGroupIds($self);
|
|
|
|
|
|
|
|
if ($excludedGroups) {
|
|
|
|
$excludedGroups = $this->config->getAppValue('core', 'shareapi_exclude_groups_list', '');
|
2017-07-08 00:12:28 +03:00
|
|
|
$decodedExcludeGroups = json_decode($excludedGroups, true);
|
2020-07-30 13:50:04 +03:00
|
|
|
$excludeGroupsList = ($decodedExcludeGroups !== null) ? $decodedExcludeGroups : [];
|
2017-07-02 12:02:18 +03:00
|
|
|
|
|
|
|
if (count(array_intersect($excludeGroupsList, $selfGroups)) !== 0) {
|
|
|
|
// a group of the current user is excluded -> filter all local users
|
|
|
|
$skipLocal = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-02 12:33:59 +03:00
|
|
|
$selfUID = $self->getUID();
|
|
|
|
|
2020-04-09 14:53:40 +03:00
|
|
|
return array_values(array_filter($entries, function (IEntry $entry) use ($self, $skipLocal, $ownGroupsOnly, $selfGroups, $selfUID, $disallowEnumeration, $filter) {
|
2017-07-02 12:02:18 +03:00
|
|
|
if ($skipLocal && $entry->getProperty('isLocalSystemBook') === true) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-09-15 16:58:04 +03:00
|
|
|
// Prevent enumerating local users
|
2020-04-10 15:19:56 +03:00
|
|
|
if ($disallowEnumeration && $entry->getProperty('isLocalSystemBook')) {
|
2017-09-15 16:58:04 +03:00
|
|
|
$filterUser = true;
|
|
|
|
|
|
|
|
$mailAddresses = $entry->getEMailAddresses();
|
2020-04-10 15:19:56 +03:00
|
|
|
foreach ($mailAddresses as $mailAddress) {
|
|
|
|
if ($mailAddress === $filter) {
|
2017-09-15 16:58:04 +03:00
|
|
|
$filterUser = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-10 15:19:56 +03:00
|
|
|
if ($entry->getProperty('UID') && $entry->getProperty('UID') === $filter) {
|
2017-09-15 16:58:04 +03:00
|
|
|
$filterUser = false;
|
|
|
|
}
|
|
|
|
|
2020-04-10 15:19:56 +03:00
|
|
|
if ($filterUser) {
|
2017-09-15 16:58:04 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-02 12:02:18 +03:00
|
|
|
if ($ownGroupsOnly && $entry->getProperty('isLocalSystemBook') === true) {
|
2018-07-19 12:39:28 +03:00
|
|
|
$uid = $this->userManager->get($entry->getProperty('UID'));
|
|
|
|
|
2020-03-25 23:53:04 +03:00
|
|
|
if ($uid === null) {
|
2018-07-19 12:39:28 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$contactGroups = $this->groupManager->getUserGroupIds($uid);
|
2017-07-02 12:02:18 +03:00
|
|
|
if (count(array_intersect($contactGroups, $selfGroups)) === 0) {
|
|
|
|
// no groups in common, so shouldn't see the contact
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-02 12:33:59 +03:00
|
|
|
return $entry->getProperty('UID') !== $selfUID;
|
2017-09-15 16:58:04 +03:00
|
|
|
}));
|
2017-01-24 09:47:14 +03:00
|
|
|
}
|
|
|
|
|
2017-04-24 12:39:03 +03:00
|
|
|
/**
|
|
|
|
* @param IUser $user
|
|
|
|
* @param integer $shareType
|
|
|
|
* @param string $shareWith
|
|
|
|
* @return IEntry|null
|
|
|
|
*/
|
|
|
|
public function findOne(IUser $user, $shareType, $shareWith) {
|
2020-04-10 15:19:56 +03:00
|
|
|
switch ($shareType) {
|
2017-04-24 12:39:03 +03:00
|
|
|
case 0:
|
|
|
|
case 6:
|
|
|
|
$filter = ['UID'];
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
$filter = ['EMAIL'];
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$userId = $user->getUID();
|
|
|
|
$allContacts = $this->contactsManager->search($shareWith, $filter);
|
2020-04-09 14:53:40 +03:00
|
|
|
$contacts = array_filter($allContacts, function ($contact) use ($userId) {
|
2017-04-24 12:39:03 +03:00
|
|
|
return $contact['UID'] !== $userId;
|
|
|
|
});
|
|
|
|
$match = null;
|
|
|
|
|
|
|
|
foreach ($contacts as $contact) {
|
|
|
|
if ($shareType === 4 && isset($contact['EMAIL'])) {
|
|
|
|
if (in_array($shareWith, $contact['EMAIL'])) {
|
|
|
|
$match = $contact;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($shareType === 0 || $shareType === 6) {
|
2018-06-04 09:45:04 +03:00
|
|
|
$isLocal = $contact['isLocalSystemBook'] ?? false;
|
|
|
|
if ($contact['UID'] === $shareWith && $isLocal === true) {
|
2017-04-24 12:39:03 +03:00
|
|
|
$match = $contact;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-14 12:45:54 +03:00
|
|
|
if ($match) {
|
2017-09-15 16:58:04 +03:00
|
|
|
$match = $this->filterContacts($user, [$this->contactArrayToEntry($match)], $shareWith);
|
2017-08-14 12:45:54 +03:00
|
|
|
if (count($match) === 1) {
|
|
|
|
$match = $match[0];
|
|
|
|
} else {
|
|
|
|
$match = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $match;
|
2017-04-24 12:39:03 +03:00
|
|
|
}
|
|
|
|
|
2017-01-24 09:47:14 +03:00
|
|
|
/**
|
|
|
|
* @param array $contact
|
|
|
|
* @return Entry
|
|
|
|
*/
|
|
|
|
private function contactArrayToEntry(array $contact) {
|
|
|
|
$entry = new Entry();
|
|
|
|
|
|
|
|
if (isset($contact['id'])) {
|
|
|
|
$entry->setId($contact['id']);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($contact['FN'])) {
|
|
|
|
$entry->setFullName($contact['FN']);
|
|
|
|
}
|
|
|
|
|
|
|
|
$avatarPrefix = "VALUE=uri:";
|
|
|
|
if (isset($contact['PHOTO']) && strpos($contact['PHOTO'], $avatarPrefix) === 0) {
|
|
|
|
$entry->setAvatar(substr($contact['PHOTO'], strlen($avatarPrefix)));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($contact['EMAIL'])) {
|
|
|
|
foreach ($contact['EMAIL'] as $email) {
|
|
|
|
$entry->addEMailAddress($email);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attach all other properties to the entry too because some
|
|
|
|
// providers might make use of it.
|
|
|
|
$entry->setProperties($contact);
|
|
|
|
|
|
|
|
return $entry;
|
|
|
|
}
|
|
|
|
}
|