2017-01-24 06:47:14 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @copyright 2017 Christoph Wurst <christoph@winzerhof-wurst.at>
|
|
|
|
*
|
|
|
|
* @author 2017 Christoph Wurst <christoph@winzerhof-wurst.at>
|
|
|
|
*
|
|
|
|
* @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\Contacts\ContactsMenu;
|
|
|
|
|
2017-07-02 09:02:18 +00:00
|
|
|
use OC\Share\Share;
|
2017-01-24 06:47:14 +00:00
|
|
|
use OCP\Contacts\ContactsMenu\IEntry;
|
|
|
|
use OCP\Contacts\IManager;
|
2017-07-02 09:02:18 +00:00
|
|
|
use OCP\IConfig;
|
|
|
|
use OCP\IGroupManager;
|
2017-04-10 14:49:26 +00:00
|
|
|
use OCP\IUser;
|
2017-07-02 09:02:18 +00:00
|
|
|
use OCP\IUserManager;
|
|
|
|
use OCP\IUserSession;
|
2017-01-24 06:47:14 +00:00
|
|
|
|
|
|
|
class ContactsStore {
|
|
|
|
|
|
|
|
/** @var IManager */
|
|
|
|
private $contactsManager;
|
|
|
|
|
2017-07-02 09:02:18 +00:00
|
|
|
/** @var IConfig */
|
|
|
|
private $config;
|
|
|
|
|
|
|
|
/** @var IUserManager */
|
|
|
|
private $userManager;
|
|
|
|
|
|
|
|
/** @var IGroupManager */
|
|
|
|
private $groupManager;
|
|
|
|
|
2017-01-24 06:47:14 +00:00
|
|
|
/**
|
|
|
|
* @param IManager $contactsManager
|
2017-07-02 09:02:18 +00:00
|
|
|
* @param IConfig $config
|
|
|
|
* @param IUserManager $userManager
|
|
|
|
* @param IGroupManager $groupManager
|
2017-01-24 06:47:14 +00:00
|
|
|
*/
|
2017-07-02 09:02:18 +00:00
|
|
|
public function __construct(IManager $contactsManager, IConfig $config, IUserManager $userManager, IGroupManager $groupManager) {
|
2017-01-24 06:47:14 +00:00
|
|
|
$this->contactsManager = $contactsManager;
|
2017-07-02 09:02:18 +00:00
|
|
|
$this->config = $config;
|
|
|
|
$this->userManager = $userManager;
|
|
|
|
$this->groupManager = $groupManager;
|
2017-01-24 06:47:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-04-10 14:49:26 +00:00
|
|
|
* @param IUser $user
|
2017-01-24 06:47:14 +00:00
|
|
|
* @param string|null $filter
|
|
|
|
* @return IEntry[]
|
|
|
|
*/
|
2017-04-10 14:49:26 +00:00
|
|
|
public function getContacts(IUser $user, $filter) {
|
2017-01-24 06:47:14 +00:00
|
|
|
$allContacts = $this->contactsManager->search($filter ?: '', [
|
|
|
|
'FN',
|
|
|
|
]);
|
|
|
|
|
2017-04-10 14:49:26 +00:00
|
|
|
$entries = array_map(function(array $contact) {
|
2017-01-24 06:47:14 +00:00
|
|
|
return $this->contactArrayToEntry($contact);
|
|
|
|
}, $allContacts);
|
2017-07-02 09:02:18 +00:00
|
|
|
return $this->filterContacts($user, $entries);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief filters the contacts. Applies 3 filters:
|
|
|
|
* 1. filter the current user
|
|
|
|
* 2. if the `shareapi_exclude_groups` config option is enabled and the
|
|
|
|
* current user is in an excluded group it will filter all local users.
|
|
|
|
* 3. if the ``shareapi_only_share_with_group_members config option is
|
|
|
|
* enabled it will filter all users which doens't have a common group
|
|
|
|
* with the current user.
|
|
|
|
* @param IUser $self
|
|
|
|
* @param $entries Entry[]
|
|
|
|
* @return array the filtered contacts
|
|
|
|
*/
|
|
|
|
private function filterContacts(IUser $self, Array $entries) {
|
|
|
|
$excludedGroups = $this->config->getAppValue('core', 'shareapi_exclude_groups', 'no') === 'yes' ? true : false;
|
|
|
|
|
|
|
|
$skipLocal = false; // whether to filter out local users
|
|
|
|
$ownGroupsOnly = Share::shareWithGroupMembersOnly(); // whether to filter out all users which doesn't have the same group as the current user
|
|
|
|
|
|
|
|
$selfGroups = $this->groupManager->getUserGroupIds($self);
|
|
|
|
|
|
|
|
if ($excludedGroups) {
|
|
|
|
$excludedGroups = $this->config->getAppValue('core', 'shareapi_exclude_groups_list', '');
|
|
|
|
$excludeGroupsList = !is_null(json_decode($excludedGroups)) ? json_decode($excludedGroups, true) : [];
|
|
|
|
|
|
|
|
if (count(array_intersect($excludeGroupsList, $selfGroups)) !== 0) {
|
|
|
|
// a group of the current user is excluded -> filter all local users
|
|
|
|
$skipLocal = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-02 09:33:59 +00:00
|
|
|
$selfUID = $self->getUID();
|
|
|
|
|
|
|
|
return array_filter($entries, function(IEntry $entry) use ($self, $skipLocal, $ownGroupsOnly, $selfGroups, $selfUID) {
|
2017-07-02 09:02:18 +00:00
|
|
|
|
|
|
|
if ($skipLocal && $entry->getProperty('isLocalSystemBook') === true) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($ownGroupsOnly && $entry->getProperty('isLocalSystemBook') === true) {
|
|
|
|
$contactGroups = $this->groupManager->getUserGroupIds($this->userManager->get($entry->getProperty('UID')));
|
|
|
|
if (count(array_intersect($contactGroups, $selfGroups)) === 0) {
|
|
|
|
// no groups in common, so shouldn't see the contact
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-02 09:33:59 +00:00
|
|
|
return $entry->getProperty('UID') !== $selfUID;
|
2017-04-10 14:49:26 +00:00
|
|
|
});
|
2017-07-02 09:02:18 +00:00
|
|
|
|
|
|
|
|
2017-01-24 06:47:14 +00:00
|
|
|
}
|
|
|
|
|
2017-04-24 09:39:03 +00:00
|
|
|
/**
|
|
|
|
* @param IUser $user
|
|
|
|
* @param integer $shareType
|
|
|
|
* @param string $shareWith
|
|
|
|
* @return IEntry|null
|
|
|
|
*/
|
|
|
|
public function findOne(IUser $user, $shareType, $shareWith) {
|
|
|
|
switch($shareType) {
|
|
|
|
case 0:
|
|
|
|
case 6:
|
|
|
|
$filter = ['UID'];
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
$filter = ['EMAIL'];
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$userId = $user->getUID();
|
|
|
|
$allContacts = $this->contactsManager->search($shareWith, $filter);
|
|
|
|
$contacts = array_filter($allContacts, function($contact) use ($userId) {
|
|
|
|
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) {
|
|
|
|
if ($contact['UID'] === $shareWith && $contact['isLocalSystemBook'] === true) {
|
|
|
|
$match = $contact;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $match ? $this->contactArrayToEntry($match) : null;
|
|
|
|
}
|
|
|
|
|
2017-01-24 06:47:14 +00: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;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|