2015-08-08 23:15:12 +03:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-21 17:49:16 +03:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
2016-05-26 20:56:05 +03:00
|
|
|
* @author Björn Schießle <bjoern@schiessle.org>
|
2016-07-21 17:49:16 +03:00
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
2016-03-01 19:25:15 +03:00
|
|
|
* @author Thomas Müller <thomas.mueller@tmit.eu>
|
2015-08-08 23:15:12 +03:00
|
|
|
*
|
|
|
|
* @license AGPL-3.0
|
|
|
|
*
|
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* 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, version 3,
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
|
|
|
*
|
|
|
|
*/
|
2016-08-26 11:52:06 +03:00
|
|
|
namespace OCA\Files_Sharing\Controller;
|
2015-08-08 23:15:12 +03:00
|
|
|
|
2017-02-22 02:24:40 +03:00
|
|
|
use OCP\AppFramework\Http\DataResponse;
|
2016-08-26 11:52:06 +03:00
|
|
|
use OCP\AppFramework\OCS\OCSBadRequestException;
|
|
|
|
use OCP\AppFramework\OCSController;
|
2015-08-13 12:06:03 +03:00
|
|
|
use OCP\Contacts\IManager;
|
2017-01-27 14:52:17 +03:00
|
|
|
use OCP\Federation\ICloudIdManager;
|
2016-11-18 16:08:42 +03:00
|
|
|
use OCP\Http\Client\IClientService;
|
2015-08-11 17:22:05 +03:00
|
|
|
use OCP\IGroup;
|
2015-08-08 23:15:12 +03:00
|
|
|
use OCP\IGroupManager;
|
2015-08-13 12:06:03 +03:00
|
|
|
use OCP\ILogger;
|
2015-08-26 10:40:20 +03:00
|
|
|
use OCP\IRequest;
|
|
|
|
use OCP\IUser;
|
2015-08-08 23:15:12 +03:00
|
|
|
use OCP\IUserManager;
|
2015-08-12 15:23:48 +03:00
|
|
|
use OCP\IConfig;
|
2015-08-08 23:15:12 +03:00
|
|
|
use OCP\IUserSession;
|
|
|
|
use OCP\IURLGenerator;
|
2015-08-13 12:06:03 +03:00
|
|
|
use OCP\Share;
|
2015-08-08 23:15:12 +03:00
|
|
|
|
2016-08-26 11:52:06 +03:00
|
|
|
class ShareesAPIController extends OCSController {
|
2015-08-08 23:15:12 +03:00
|
|
|
|
|
|
|
/** @var IGroupManager */
|
2015-08-26 11:51:26 +03:00
|
|
|
protected $groupManager;
|
2015-08-08 23:15:12 +03:00
|
|
|
|
|
|
|
/** @var IUserManager */
|
2015-08-26 11:51:26 +03:00
|
|
|
protected $userManager;
|
2015-08-08 23:15:12 +03:00
|
|
|
|
2015-08-13 12:06:03 +03:00
|
|
|
/** @var IManager */
|
2015-08-26 11:51:26 +03:00
|
|
|
protected $contactsManager;
|
2015-08-08 23:15:12 +03:00
|
|
|
|
2015-08-12 15:23:48 +03:00
|
|
|
/** @var IConfig */
|
2015-08-26 11:51:26 +03:00
|
|
|
protected $config;
|
2015-08-08 23:15:12 +03:00
|
|
|
|
|
|
|
/** @var IUserSession */
|
2015-08-26 11:51:26 +03:00
|
|
|
protected $userSession;
|
2015-08-08 23:15:12 +03:00
|
|
|
|
|
|
|
/** @var IURLGenerator */
|
2015-08-26 11:51:26 +03:00
|
|
|
protected $urlGenerator;
|
2015-08-08 23:15:12 +03:00
|
|
|
|
2015-08-13 12:06:03 +03:00
|
|
|
/** @var ILogger */
|
2015-08-26 11:51:26 +03:00
|
|
|
protected $logger;
|
2015-08-13 12:06:03 +03:00
|
|
|
|
2016-03-18 18:39:03 +03:00
|
|
|
/** @var \OCP\Share\IManager */
|
|
|
|
protected $shareManager;
|
|
|
|
|
2016-11-18 16:08:42 +03:00
|
|
|
/** @var IClientService */
|
|
|
|
protected $clientService;
|
|
|
|
|
2017-01-27 14:52:17 +03:00
|
|
|
/** @var ICloudIdManager */
|
|
|
|
protected $cloudIdManager;
|
|
|
|
|
2015-08-26 10:40:20 +03:00
|
|
|
/** @var bool */
|
2015-08-26 11:51:26 +03:00
|
|
|
protected $shareWithGroupOnly = false;
|
2015-08-26 10:40:20 +03:00
|
|
|
|
2015-08-26 13:30:07 +03:00
|
|
|
/** @var bool */
|
|
|
|
protected $shareeEnumeration = true;
|
|
|
|
|
2015-08-26 10:40:20 +03:00
|
|
|
/** @var int */
|
|
|
|
protected $offset = 0;
|
|
|
|
|
|
|
|
/** @var int */
|
|
|
|
protected $limit = 10;
|
|
|
|
|
|
|
|
/** @var array */
|
|
|
|
protected $result = [
|
|
|
|
'exact' => [
|
|
|
|
'users' => [],
|
|
|
|
'groups' => [],
|
|
|
|
'remotes' => [],
|
2016-10-24 14:16:05 +03:00
|
|
|
'emails' => [],
|
2017-03-17 22:48:33 +03:00
|
|
|
'circles' => [],
|
2015-08-26 10:40:20 +03:00
|
|
|
],
|
|
|
|
'users' => [],
|
|
|
|
'groups' => [],
|
|
|
|
'remotes' => [],
|
2016-10-24 14:16:05 +03:00
|
|
|
'emails' => [],
|
2016-11-18 16:08:42 +03:00
|
|
|
'lookup' => [],
|
2017-03-17 22:48:33 +03:00
|
|
|
'circles' => [],
|
2015-08-26 10:40:20 +03:00
|
|
|
];
|
|
|
|
|
|
|
|
protected $reachedEndFor = [];
|
2015-08-13 11:13:23 +03:00
|
|
|
|
2015-08-08 23:15:12 +03:00
|
|
|
/**
|
2016-08-26 11:52:06 +03:00
|
|
|
* @param string $appName
|
|
|
|
* @param IRequest $request
|
2015-08-08 23:15:12 +03:00
|
|
|
* @param IGroupManager $groupManager
|
|
|
|
* @param IUserManager $userManager
|
2015-08-13 12:06:03 +03:00
|
|
|
* @param IManager $contactsManager
|
2015-08-12 15:23:48 +03:00
|
|
|
* @param IConfig $config
|
2015-08-08 23:15:12 +03:00
|
|
|
* @param IUserSession $userSession
|
2015-08-13 12:06:03 +03:00
|
|
|
* @param IURLGenerator $urlGenerator
|
|
|
|
* @param ILogger $logger
|
2016-03-18 18:39:03 +03:00
|
|
|
* @param \OCP\Share\IManager $shareManager
|
2016-11-18 16:08:42 +03:00
|
|
|
* @param IClientService $clientService
|
2017-01-27 14:52:17 +03:00
|
|
|
* @param ICloudIdManager $cloudIdManager
|
2015-08-08 23:15:12 +03:00
|
|
|
*/
|
2016-08-26 11:52:06 +03:00
|
|
|
public function __construct($appName,
|
|
|
|
IRequest $request,
|
|
|
|
IGroupManager $groupManager,
|
2015-08-11 15:57:51 +03:00
|
|
|
IUserManager $userManager,
|
2015-08-13 12:06:03 +03:00
|
|
|
IManager $contactsManager,
|
2015-08-12 15:23:48 +03:00
|
|
|
IConfig $config,
|
2015-08-08 23:15:12 +03:00
|
|
|
IUserSession $userSession,
|
2015-08-13 12:06:03 +03:00
|
|
|
IURLGenerator $urlGenerator,
|
2016-03-18 18:39:03 +03:00
|
|
|
ILogger $logger,
|
2016-11-18 16:08:42 +03:00
|
|
|
\OCP\Share\IManager $shareManager,
|
2017-01-27 14:52:17 +03:00
|
|
|
IClientService $clientService,
|
|
|
|
ICloudIdManager $cloudIdManager
|
|
|
|
) {
|
2016-08-26 11:52:06 +03:00
|
|
|
parent::__construct($appName, $request);
|
|
|
|
|
2015-08-08 23:15:12 +03:00
|
|
|
$this->groupManager = $groupManager;
|
|
|
|
$this->userManager = $userManager;
|
|
|
|
$this->contactsManager = $contactsManager;
|
2015-08-12 15:23:48 +03:00
|
|
|
$this->config = $config;
|
2015-08-08 23:15:12 +03:00
|
|
|
$this->userSession = $userSession;
|
|
|
|
$this->urlGenerator = $urlGenerator;
|
2015-08-13 12:06:03 +03:00
|
|
|
$this->logger = $logger;
|
2016-03-18 18:39:03 +03:00
|
|
|
$this->shareManager = $shareManager;
|
2016-11-18 16:08:42 +03:00
|
|
|
$this->clientService = $clientService;
|
2017-01-27 14:52:17 +03:00
|
|
|
$this->cloudIdManager = $cloudIdManager;
|
2015-08-08 23:15:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $search
|
|
|
|
*/
|
2015-08-26 10:40:20 +03:00
|
|
|
protected function getUsers($search) {
|
|
|
|
$this->result['users'] = $this->result['exact']['users'] = $users = [];
|
|
|
|
|
2015-12-02 18:01:01 +03:00
|
|
|
$userGroups = [];
|
2015-08-26 10:40:20 +03:00
|
|
|
if ($this->shareWithGroupOnly) {
|
2015-08-08 23:15:12 +03:00
|
|
|
// Search in all the groups this user is part of
|
2015-08-11 16:43:44 +03:00
|
|
|
$userGroups = $this->groupManager->getUserGroupIds($this->userSession->getUser());
|
2015-08-08 23:15:12 +03:00
|
|
|
foreach ($userGroups as $userGroup) {
|
2015-08-26 11:51:26 +03:00
|
|
|
$usersTmp = $this->groupManager->displayNamesInGroup($userGroup, $search, $this->limit, $this->offset);
|
|
|
|
foreach ($usersTmp as $uid => $userDisplayName) {
|
2015-08-26 10:40:20 +03:00
|
|
|
$users[$uid] = $userDisplayName;
|
|
|
|
}
|
2015-08-08 23:15:12 +03:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Search in all users
|
2015-08-26 10:40:20 +03:00
|
|
|
$usersTmp = $this->userManager->searchDisplayName($search, $this->limit, $this->offset);
|
2015-08-08 23:15:12 +03:00
|
|
|
|
2015-08-26 10:40:20 +03:00
|
|
|
foreach ($usersTmp as $user) {
|
2015-08-08 23:15:12 +03:00
|
|
|
$users[$user->getUID()] = $user->getDisplayName();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-26 13:30:07 +03:00
|
|
|
if (!$this->shareeEnumeration || sizeof($users) < $this->limit) {
|
2015-08-26 10:40:20 +03:00
|
|
|
$this->reachedEndFor[] = 'users';
|
|
|
|
}
|
2015-08-08 23:15:12 +03:00
|
|
|
|
2015-08-26 10:40:20 +03:00
|
|
|
$foundUserById = false;
|
2016-11-30 22:56:10 +03:00
|
|
|
$lowerSearch = strtolower($search);
|
2015-08-26 10:40:20 +03:00
|
|
|
foreach ($users as $uid => $userDisplayName) {
|
2016-11-30 22:56:10 +03:00
|
|
|
if (strtolower($uid) === $lowerSearch || strtolower($userDisplayName) === $lowerSearch) {
|
|
|
|
if (strtolower($uid) === $lowerSearch) {
|
2015-08-26 10:40:20 +03:00
|
|
|
$foundUserById = true;
|
|
|
|
}
|
|
|
|
$this->result['exact']['users'][] = [
|
2015-08-26 11:51:26 +03:00
|
|
|
'label' => $userDisplayName,
|
|
|
|
'value' => [
|
|
|
|
'shareType' => Share::SHARE_TYPE_USER,
|
|
|
|
'shareWith' => $uid,
|
|
|
|
],
|
2015-08-26 10:40:20 +03:00
|
|
|
];
|
|
|
|
} else {
|
|
|
|
$this->result['users'][] = [
|
|
|
|
'label' => $userDisplayName,
|
2015-08-26 11:51:26 +03:00
|
|
|
'value' => [
|
|
|
|
'shareType' => Share::SHARE_TYPE_USER,
|
|
|
|
'shareWith' => $uid,
|
|
|
|
],
|
2015-08-26 10:40:20 +03:00
|
|
|
];
|
|
|
|
}
|
2015-08-08 23:15:12 +03:00
|
|
|
}
|
|
|
|
|
2015-08-26 10:40:20 +03:00
|
|
|
if ($this->offset === 0 && !$foundUserById) {
|
|
|
|
// On page one we try if the search result has a direct hit on the
|
|
|
|
// user id and if so, we add that to the exact match list
|
|
|
|
$user = $this->userManager->get($search);
|
|
|
|
if ($user instanceof IUser) {
|
2015-12-02 18:01:01 +03:00
|
|
|
$addUser = true;
|
|
|
|
|
|
|
|
if ($this->shareWithGroupOnly) {
|
|
|
|
// Only add, if we have a common group
|
|
|
|
$commonGroups = array_intersect($userGroups, $this->groupManager->getUserGroupIds($user));
|
|
|
|
$addUser = !empty($commonGroups);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($addUser) {
|
|
|
|
array_push($this->result['exact']['users'], [
|
|
|
|
'label' => $user->getDisplayName(),
|
|
|
|
'value' => [
|
|
|
|
'shareType' => Share::SHARE_TYPE_USER,
|
|
|
|
'shareWith' => $user->getUID(),
|
|
|
|
],
|
|
|
|
]);
|
|
|
|
}
|
2015-08-26 10:40:20 +03:00
|
|
|
}
|
|
|
|
}
|
2015-08-26 13:30:07 +03:00
|
|
|
|
|
|
|
if (!$this->shareeEnumeration) {
|
|
|
|
$this->result['users'] = [];
|
|
|
|
}
|
2015-08-08 23:15:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $search
|
|
|
|
*/
|
2015-08-26 10:40:20 +03:00
|
|
|
protected function getGroups($search) {
|
|
|
|
$this->result['groups'] = $this->result['exact']['groups'] = [];
|
|
|
|
|
|
|
|
$groups = $this->groupManager->search($search, $this->limit, $this->offset);
|
2016-11-30 22:56:10 +03:00
|
|
|
$groupIds = array_map(function (IGroup $group) { return $group->getGID(); }, $groups);
|
2015-08-08 23:15:12 +03:00
|
|
|
|
2015-08-26 13:30:07 +03:00
|
|
|
if (!$this->shareeEnumeration || sizeof($groups) < $this->limit) {
|
2015-08-26 10:40:20 +03:00
|
|
|
$this->reachedEndFor[] = 'groups';
|
|
|
|
}
|
|
|
|
|
|
|
|
$userGroups = [];
|
|
|
|
if (!empty($groups) && $this->shareWithGroupOnly) {
|
2015-08-08 23:15:12 +03:00
|
|
|
// Intersect all the groups that match with the groups this user is a member of
|
|
|
|
$userGroups = $this->groupManager->getUserGroups($this->userSession->getUser());
|
2015-08-11 17:22:05 +03:00
|
|
|
$userGroups = array_map(function (IGroup $group) { return $group->getGID(); }, $userGroups);
|
2016-11-30 22:56:10 +03:00
|
|
|
$groupIds = array_intersect($groupIds, $userGroups);
|
2015-08-08 23:15:12 +03:00
|
|
|
}
|
|
|
|
|
2016-11-30 22:56:10 +03:00
|
|
|
$lowerSearch = strtolower($search);
|
|
|
|
foreach ($groups as $group) {
|
|
|
|
// FIXME: use a more efficient approach
|
|
|
|
$gid = $group->getGID();
|
|
|
|
if (!in_array($gid, $groupIds)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (strtolower($gid) === $lowerSearch || strtolower($group->getDisplayName()) === $lowerSearch) {
|
2015-08-26 10:40:20 +03:00
|
|
|
$this->result['exact']['groups'][] = [
|
2016-11-30 22:56:10 +03:00
|
|
|
'label' => $group->getDisplayName(),
|
2015-08-26 11:51:26 +03:00
|
|
|
'value' => [
|
|
|
|
'shareType' => Share::SHARE_TYPE_GROUP,
|
2016-03-10 16:55:31 +03:00
|
|
|
'shareWith' => $gid,
|
2015-08-26 11:51:26 +03:00
|
|
|
],
|
2015-08-26 10:40:20 +03:00
|
|
|
];
|
|
|
|
} else {
|
|
|
|
$this->result['groups'][] = [
|
2016-11-30 22:56:10 +03:00
|
|
|
'label' => $group->getDisplayName(),
|
2015-08-26 11:51:26 +03:00
|
|
|
'value' => [
|
|
|
|
'shareType' => Share::SHARE_TYPE_GROUP,
|
|
|
|
'shareWith' => $gid,
|
|
|
|
],
|
2015-08-26 10:40:20 +03:00
|
|
|
];
|
|
|
|
}
|
2015-08-08 23:15:12 +03:00
|
|
|
}
|
|
|
|
|
2015-08-26 10:40:20 +03:00
|
|
|
if ($this->offset === 0 && empty($this->result['exact']['groups'])) {
|
|
|
|
// On page one we try if the search result has a direct hit on the
|
|
|
|
// user id and if so, we add that to the exact match list
|
|
|
|
$group = $this->groupManager->get($search);
|
2015-08-26 12:45:11 +03:00
|
|
|
if ($group instanceof IGroup && (!$this->shareWithGroupOnly || in_array($group->getGID(), $userGroups))) {
|
|
|
|
array_push($this->result['exact']['groups'], [
|
2016-11-30 22:56:10 +03:00
|
|
|
'label' => $group->getDisplayName(),
|
2015-08-26 11:51:26 +03:00
|
|
|
'value' => [
|
|
|
|
'shareType' => Share::SHARE_TYPE_GROUP,
|
|
|
|
'shareWith' => $group->getGID(),
|
|
|
|
],
|
2015-08-26 10:40:20 +03:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|
2015-08-26 13:30:07 +03:00
|
|
|
|
|
|
|
if (!$this->shareeEnumeration) {
|
|
|
|
$this->result['groups'] = [];
|
|
|
|
}
|
2015-08-08 23:15:12 +03:00
|
|
|
}
|
|
|
|
|
2017-03-17 22:48:33 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $search
|
|
|
|
*/
|
|
|
|
protected function getCircles($search) {
|
|
|
|
$this->result['circles'] = $this->result['exact']['circles'] = [];
|
|
|
|
|
|
|
|
$result = \OCA\Circles\Api\Sharees::search($search, $this->limit, $this->offset);
|
|
|
|
if (array_key_exists('circles', $result['exact'])) {
|
|
|
|
$this->result['exact']['circles'] = $result['exact']['circles'];
|
|
|
|
}
|
|
|
|
if (array_key_exists('circles', $result)) {
|
|
|
|
$this->result['circles'] = $result['circles'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-08-08 23:15:12 +03:00
|
|
|
/**
|
|
|
|
* @param string $search
|
2016-10-25 18:13:10 +03:00
|
|
|
* @return array
|
2015-08-08 23:15:12 +03:00
|
|
|
*/
|
2015-08-12 15:19:34 +03:00
|
|
|
protected function getRemote($search) {
|
2016-10-25 18:13:10 +03:00
|
|
|
$result = ['results' => [], 'exact' => []];
|
2015-08-08 23:15:12 +03:00
|
|
|
|
|
|
|
// Search in contacts
|
2015-08-26 10:40:20 +03:00
|
|
|
//@todo Pagination missing
|
2015-08-08 23:15:12 +03:00
|
|
|
$addressBookContacts = $this->contactsManager->search($search, ['CLOUD', 'FN']);
|
2016-10-25 18:13:10 +03:00
|
|
|
$result['exactIdMatch'] = false;
|
2015-08-08 23:15:12 +03:00
|
|
|
foreach ($addressBookContacts as $contact) {
|
2016-02-02 13:24:26 +03:00
|
|
|
if (isset($contact['isLocalSystemBook'])) {
|
|
|
|
continue;
|
|
|
|
}
|
2015-08-08 23:15:12 +03:00
|
|
|
if (isset($contact['CLOUD'])) {
|
2016-01-28 18:01:34 +03:00
|
|
|
$cloudIds = $contact['CLOUD'];
|
|
|
|
if (!is_array($cloudIds)) {
|
|
|
|
$cloudIds = [$cloudIds];
|
|
|
|
}
|
2016-11-30 22:56:10 +03:00
|
|
|
$lowerSearch = strtolower($search);
|
2016-01-28 18:01:34 +03:00
|
|
|
foreach ($cloudIds as $cloudId) {
|
2016-02-12 17:27:39 +03:00
|
|
|
list(, $serverUrl) = $this->splitUserRemote($cloudId);
|
2016-11-30 22:56:10 +03:00
|
|
|
if (strtolower($contact['FN']) === $lowerSearch || strtolower($cloudId) === $lowerSearch) {
|
|
|
|
if (strtolower($cloudId) === $lowerSearch) {
|
2016-10-25 18:13:10 +03:00
|
|
|
$result['exactIdMatch'] = true;
|
2015-08-26 12:45:11 +03:00
|
|
|
}
|
2016-10-25 18:13:10 +03:00
|
|
|
$result['exact'][] = [
|
2016-10-25 17:24:49 +03:00
|
|
|
'label' => $contact['FN'] . " ($cloudId)",
|
2015-08-26 11:51:26 +03:00
|
|
|
'value' => [
|
|
|
|
'shareType' => Share::SHARE_TYPE_REMOTE,
|
|
|
|
'shareWith' => $cloudId,
|
2016-02-12 17:27:39 +03:00
|
|
|
'server' => $serverUrl,
|
2015-08-26 11:51:26 +03:00
|
|
|
],
|
2015-08-26 10:40:20 +03:00
|
|
|
];
|
|
|
|
} else {
|
2016-10-25 18:13:10 +03:00
|
|
|
$result['results'][] = [
|
2016-10-25 17:24:49 +03:00
|
|
|
'label' => $contact['FN'] . " ($cloudId)",
|
2015-08-26 11:51:26 +03:00
|
|
|
'value' => [
|
|
|
|
'shareType' => Share::SHARE_TYPE_REMOTE,
|
|
|
|
'shareWith' => $cloudId,
|
2016-02-12 17:27:39 +03:00
|
|
|
'server' => $serverUrl,
|
2015-08-26 11:51:26 +03:00
|
|
|
],
|
2015-08-26 10:40:20 +03:00
|
|
|
];
|
|
|
|
}
|
2015-08-08 23:15:12 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-26 13:30:07 +03:00
|
|
|
if (!$this->shareeEnumeration) {
|
2016-10-25 18:13:10 +03:00
|
|
|
$result['results'] = [];
|
2015-08-26 13:30:07 +03:00
|
|
|
}
|
|
|
|
|
2017-01-27 14:52:17 +03:00
|
|
|
if (!$result['exactIdMatch'] && $this->cloudIdManager->isValidCloudId($search) && $this->offset === 0) {
|
2016-10-25 18:13:10 +03:00
|
|
|
$result['exact'][] = [
|
2015-08-26 12:45:11 +03:00
|
|
|
'label' => $search,
|
|
|
|
'value' => [
|
|
|
|
'shareType' => Share::SHARE_TYPE_REMOTE,
|
|
|
|
'shareWith' => $search,
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2015-08-26 10:40:20 +03:00
|
|
|
$this->reachedEndFor[] = 'remotes';
|
2016-10-25 15:42:59 +03:00
|
|
|
|
2016-10-25 18:13:10 +03:00
|
|
|
return $result;
|
2015-08-08 23:15:12 +03:00
|
|
|
}
|
|
|
|
|
2016-02-12 17:27:39 +03:00
|
|
|
/**
|
|
|
|
* split user and remote from federated cloud id
|
|
|
|
*
|
|
|
|
* @param string $address federated share address
|
|
|
|
* @return array [user, remoteURL]
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
|
|
|
public function splitUserRemote($address) {
|
2017-01-27 14:52:17 +03:00
|
|
|
try {
|
|
|
|
$cloudId = $this->cloudIdManager->resolveCloudId($address);
|
|
|
|
return [$cloudId->getUser(), $cloudId->getRemote()];
|
|
|
|
} catch (\InvalidArgumentException $e) {
|
|
|
|
throw new \Exception('Invalid Federated Cloud ID', 0, $e);
|
2016-02-12 17:27:39 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Strips away a potential file names and trailing slashes:
|
|
|
|
* - http://localhost
|
|
|
|
* - http://localhost/
|
|
|
|
* - http://localhost/index.php
|
|
|
|
* - http://localhost/index.php/s/{shareToken}
|
|
|
|
*
|
|
|
|
* all return: http://localhost
|
|
|
|
*
|
|
|
|
* @param string $remote
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function fixRemoteURL($remote) {
|
|
|
|
$remote = str_replace('\\', '/', $remote);
|
|
|
|
if ($fileNamePosition = strpos($remote, '/index.php')) {
|
|
|
|
$remote = substr($remote, 0, $fileNamePosition);
|
|
|
|
}
|
|
|
|
$remote = rtrim($remote, '/');
|
|
|
|
|
|
|
|
return $remote;
|
|
|
|
}
|
|
|
|
|
2015-08-12 15:19:34 +03:00
|
|
|
/**
|
2016-08-26 11:52:06 +03:00
|
|
|
* @NoAdminRequired
|
|
|
|
*
|
|
|
|
* @param string $search
|
|
|
|
* @param string $itemType
|
|
|
|
* @param int $page
|
|
|
|
* @param int $perPage
|
|
|
|
* @param int|int[] $shareType
|
2016-11-18 16:08:42 +03:00
|
|
|
* @param bool $lookup
|
2017-02-22 02:24:40 +03:00
|
|
|
* @return DataResponse
|
2016-08-26 11:52:06 +03:00
|
|
|
* @throws OCSBadRequestException
|
2015-08-12 15:19:34 +03:00
|
|
|
*/
|
2016-11-18 16:08:42 +03:00
|
|
|
public function search($search = '', $itemType = null, $page = 1, $perPage = 200, $shareType = null, $lookup = true) {
|
2017-02-21 20:10:38 +03:00
|
|
|
|
|
|
|
// only search for string larger than a given threshold
|
2017-02-22 17:14:36 +03:00
|
|
|
$threshold = (int)$this->config->getSystemValue('sharing.minSearchStringLength', 0);
|
2017-02-21 20:10:38 +03:00
|
|
|
if (strlen($search) < $threshold) {
|
2017-02-22 02:24:40 +03:00
|
|
|
return new DataResponse($this->result);
|
2017-02-21 20:10:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// never return more than the max. number of results configured in the config.php
|
2017-02-22 17:14:36 +03:00
|
|
|
$maxResults = (int)$this->config->getSystemValue('sharing.maxAutocompleteResults', 0);
|
2017-02-21 20:10:38 +03:00
|
|
|
if ($maxResults > 0) {
|
|
|
|
$perPage = min($perPage, $maxResults);
|
|
|
|
}
|
2015-09-15 13:37:09 +03:00
|
|
|
if ($perPage <= 0) {
|
2016-08-26 11:52:06 +03:00
|
|
|
throw new OCSBadRequestException('Invalid perPage argument');
|
2015-09-15 13:37:09 +03:00
|
|
|
}
|
|
|
|
if ($page <= 0) {
|
2016-08-26 11:52:06 +03:00
|
|
|
throw new OCSBadRequestException('Invalid page');
|
2015-09-15 13:37:09 +03:00
|
|
|
}
|
2015-08-12 18:05:20 +03:00
|
|
|
|
|
|
|
$shareTypes = [
|
2015-08-13 12:06:03 +03:00
|
|
|
Share::SHARE_TYPE_USER,
|
2015-08-12 18:05:20 +03:00
|
|
|
];
|
2016-03-18 18:39:03 +03:00
|
|
|
|
2016-11-10 16:16:35 +03:00
|
|
|
if ($itemType === 'file' || $itemType === 'folder') {
|
|
|
|
if ($this->shareManager->allowGroupSharing()) {
|
|
|
|
$shareTypes[] = Share::SHARE_TYPE_GROUP;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->isRemoteSharingAllowed($itemType)) {
|
|
|
|
$shareTypes[] = Share::SHARE_TYPE_REMOTE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->shareManager->shareProviderExists(Share::SHARE_TYPE_EMAIL)) {
|
|
|
|
$shareTypes[] = Share::SHARE_TYPE_EMAIL;
|
|
|
|
}
|
|
|
|
} else {
|
2016-03-18 18:39:03 +03:00
|
|
|
$shareTypes[] = Share::SHARE_TYPE_GROUP;
|
2016-11-10 16:16:35 +03:00
|
|
|
$shareTypes[] = Share::SHARE_TYPE_EMAIL;
|
2016-03-18 18:39:03 +03:00
|
|
|
}
|
|
|
|
|
2017-03-17 22:48:33 +03:00
|
|
|
if (\OCP\App::isEnabled('circles')) {
|
|
|
|
$shareTypes[] = Share::SHARE_TYPE_CIRCLE;
|
|
|
|
}
|
|
|
|
|
2016-07-29 16:37:08 +03:00
|
|
|
if (isset($_GET['shareType']) && is_array($_GET['shareType'])) {
|
|
|
|
$shareTypes = array_intersect($shareTypes, $_GET['shareType']);
|
2015-08-12 18:05:20 +03:00
|
|
|
sort($shareTypes);
|
2016-08-26 11:52:06 +03:00
|
|
|
} else if (is_numeric($shareType)) {
|
|
|
|
$shareTypes = array_intersect($shareTypes, [(int) $shareType]);
|
2015-08-12 18:05:20 +03:00
|
|
|
sort($shareTypes);
|
|
|
|
}
|
|
|
|
|
2015-08-26 10:40:20 +03:00
|
|
|
$this->shareWithGroupOnly = $this->config->getAppValue('core', 'shareapi_only_share_with_group_members', 'no') === 'yes';
|
2015-08-26 13:30:07 +03:00
|
|
|
$this->shareeEnumeration = $this->config->getAppValue('core', 'shareapi_allow_share_dialog_user_enumeration', 'yes') === 'yes';
|
2015-08-26 10:40:20 +03:00
|
|
|
$this->limit = (int) $perPage;
|
|
|
|
$this->offset = $perPage * ($page - 1);
|
2015-08-12 15:19:34 +03:00
|
|
|
|
2016-11-18 16:08:42 +03:00
|
|
|
return $this->searchSharees($search, $itemType, $shareTypes, $page, $perPage, $lookup);
|
2015-08-12 18:05:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Method to get out the static call for better testing
|
|
|
|
*
|
|
|
|
* @param string $itemType
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
protected function isRemoteSharingAllowed($itemType) {
|
|
|
|
try {
|
2015-08-13 12:06:03 +03:00
|
|
|
$backend = Share::getBackend($itemType);
|
|
|
|
return $backend->isShareTypeAllowed(Share::SHARE_TYPE_REMOTE);
|
2015-08-12 18:05:20 +03:00
|
|
|
} catch (\Exception $e) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-08-12 15:19:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Testable search function that does not need globals
|
|
|
|
*
|
|
|
|
* @param string $search
|
|
|
|
* @param string $itemType
|
2015-08-12 18:05:20 +03:00
|
|
|
* @param array $shareTypes
|
2015-08-12 15:19:34 +03:00
|
|
|
* @param int $page
|
|
|
|
* @param int $perPage
|
2016-11-18 16:08:42 +03:00
|
|
|
* @param bool $lookup
|
2017-02-22 02:24:40 +03:00
|
|
|
* @return DataResponse
|
2016-08-26 11:52:06 +03:00
|
|
|
* @throws OCSBadRequestException
|
2015-08-12 15:19:34 +03:00
|
|
|
*/
|
2016-11-18 16:08:42 +03:00
|
|
|
protected function searchSharees($search, $itemType, array $shareTypes, $page, $perPage, $lookup) {
|
2015-08-08 23:15:12 +03:00
|
|
|
// Verify arguments
|
2015-08-11 19:03:07 +03:00
|
|
|
if ($itemType === null) {
|
2016-08-26 11:52:06 +03:00
|
|
|
throw new OCSBadRequestException('Missing itemType');
|
2015-08-08 23:15:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get users
|
2015-08-13 12:06:03 +03:00
|
|
|
if (in_array(Share::SHARE_TYPE_USER, $shareTypes)) {
|
2015-08-26 10:40:20 +03:00
|
|
|
$this->getUsers($search);
|
2015-08-08 23:15:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get groups
|
2015-08-13 12:06:03 +03:00
|
|
|
if (in_array(Share::SHARE_TYPE_GROUP, $shareTypes)) {
|
2015-08-26 10:40:20 +03:00
|
|
|
$this->getGroups($search);
|
2015-08-08 23:15:12 +03:00
|
|
|
}
|
|
|
|
|
2017-03-17 22:48:33 +03:00
|
|
|
// Get circles
|
|
|
|
if (in_array(Share::SHARE_TYPE_CIRCLE, $shareTypes)) {
|
|
|
|
$this->getCircles($search);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-08-08 23:15:12 +03:00
|
|
|
// Get remote
|
2016-10-27 13:27:09 +03:00
|
|
|
$remoteResults = ['results' => [], 'exact' => [], 'exactIdMatch' => false];
|
2015-08-13 12:06:03 +03:00
|
|
|
if (in_array(Share::SHARE_TYPE_REMOTE, $shareTypes)) {
|
2016-10-25 18:13:10 +03:00
|
|
|
$remoteResults = $this->getRemote($search);
|
2015-08-08 23:15:12 +03:00
|
|
|
}
|
|
|
|
|
2016-11-18 16:08:42 +03:00
|
|
|
// Get emails
|
2016-10-27 13:27:09 +03:00
|
|
|
$mailResults = ['results' => [], 'exact' => [], 'exactIdMatch' => false];
|
2016-10-25 18:13:10 +03:00
|
|
|
if (in_array(Share::SHARE_TYPE_EMAIL, $shareTypes)) {
|
|
|
|
$mailResults = $this->getEmail($search);
|
|
|
|
}
|
|
|
|
|
2016-11-18 16:08:42 +03:00
|
|
|
// Get from lookup server
|
|
|
|
if ($lookup) {
|
|
|
|
$this->getLookup($search);
|
|
|
|
}
|
|
|
|
|
2016-10-25 18:13:10 +03:00
|
|
|
// if we have a exact match, either for the federated cloud id or for the
|
|
|
|
// email address we only return the exact match. It is highly unlikely
|
|
|
|
// that the exact same email address and federated cloud id exists
|
|
|
|
if ($mailResults['exactIdMatch'] && !$remoteResults['exactIdMatch']) {
|
|
|
|
$this->result['emails'] = $mailResults['results'];
|
|
|
|
$this->result['exact']['emails'] = $mailResults['exact'];
|
|
|
|
} else if (!$mailResults['exactIdMatch'] && $remoteResults['exactIdMatch']) {
|
|
|
|
$this->result['remotes'] = $remoteResults['results'];
|
|
|
|
$this->result['exact']['remotes'] = $remoteResults['exact'];
|
|
|
|
} else {
|
|
|
|
$this->result['remotes'] = $remoteResults['results'];
|
|
|
|
$this->result['exact']['remotes'] = $remoteResults['exact'];
|
|
|
|
$this->result['emails'] = $mailResults['results'];
|
|
|
|
$this->result['exact']['emails'] = $mailResults['exact'];
|
2016-10-24 14:16:05 +03:00
|
|
|
}
|
|
|
|
|
2017-02-22 02:24:40 +03:00
|
|
|
$response = new DataResponse($this->result);
|
2015-08-11 19:03:07 +03:00
|
|
|
|
2015-08-26 10:40:20 +03:00
|
|
|
if (sizeof($this->reachedEndFor) < 3) {
|
|
|
|
$response->addHeader('Link', $this->getPaginationLink($page, [
|
|
|
|
'search' => $search,
|
|
|
|
'itemType' => $itemType,
|
|
|
|
'shareType' => $shareTypes,
|
2015-09-15 13:14:14 +03:00
|
|
|
'perPage' => $perPage,
|
2015-08-26 10:40:20 +03:00
|
|
|
]));
|
2015-08-08 23:15:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return $response;
|
|
|
|
}
|
2015-08-11 19:03:07 +03:00
|
|
|
|
2016-07-29 16:37:08 +03:00
|
|
|
/**
|
|
|
|
* @param string $search
|
2016-10-25 18:13:10 +03:00
|
|
|
* @return array
|
2016-07-29 16:37:08 +03:00
|
|
|
*/
|
|
|
|
protected function getEmail($search) {
|
2017-06-13 19:07:47 +03:00
|
|
|
$result = ['results' => [], 'exact' => [], 'exactIdMatch' => false];
|
2016-07-29 16:37:08 +03:00
|
|
|
|
2016-10-25 15:42:59 +03:00
|
|
|
// Search in contacts
|
|
|
|
//@todo Pagination missing
|
|
|
|
$addressBookContacts = $this->contactsManager->search($search, ['EMAIL', 'FN']);
|
2017-06-13 19:07:47 +03:00
|
|
|
$lowerSearch = strtolower($search);
|
2016-10-25 15:42:59 +03:00
|
|
|
foreach ($addressBookContacts as $contact) {
|
|
|
|
if (isset($contact['EMAIL'])) {
|
|
|
|
$emailAddresses = $contact['EMAIL'];
|
|
|
|
if (!is_array($emailAddresses)) {
|
|
|
|
$emailAddresses = [$emailAddresses];
|
|
|
|
}
|
|
|
|
foreach ($emailAddresses as $emailAddress) {
|
2017-06-13 19:07:47 +03:00
|
|
|
$exactEmailMatch = strtolower($emailAddress) === $lowerSearch;
|
|
|
|
if ($exactEmailMatch || strtolower($contact['FN']) === $lowerSearch) {
|
|
|
|
if (isset($contact['isLocalSystemBook'])) {
|
|
|
|
if ($exactEmailMatch) {
|
|
|
|
$cloud = $this->cloudIdManager->resolveCloudId($contact['CLOUD'][0]);
|
|
|
|
$this->result['exact']['users'][] = [
|
|
|
|
'label' => $contact['FN'] . " ($emailAddress)",
|
|
|
|
'value' => [
|
|
|
|
'shareType' => Share::SHARE_TYPE_USER,
|
|
|
|
'shareWith' => $cloud->getUser(),
|
|
|
|
],
|
|
|
|
];
|
|
|
|
return ['results' => [], 'exact' => [], 'exactIdMatch' => true];
|
|
|
|
}
|
|
|
|
if ($this->shareeEnumeration) {
|
|
|
|
$cloud = $this->cloudIdManager->resolveCloudId($contact['CLOUD'][0]);
|
|
|
|
$this->result['users'][] = [
|
|
|
|
'label' => $contact['FN'] . " ($emailAddress)",
|
|
|
|
'value' => [
|
|
|
|
'shareType' => Share::SHARE_TYPE_USER,
|
|
|
|
'shareWith' => $cloud->getUser(),
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($exactEmailMatch) {
|
2016-10-25 18:13:10 +03:00
|
|
|
$result['exactIdMatch'] = true;
|
2016-10-25 15:42:59 +03:00
|
|
|
}
|
2016-10-25 18:13:10 +03:00
|
|
|
$result['exact'][] = [
|
2016-10-25 15:42:59 +03:00
|
|
|
'label' => $contact['FN'] . " ($emailAddress)",
|
|
|
|
'value' => [
|
|
|
|
'shareType' => Share::SHARE_TYPE_EMAIL,
|
|
|
|
'shareWith' => $emailAddress,
|
|
|
|
],
|
|
|
|
];
|
|
|
|
} else {
|
2016-10-25 18:13:10 +03:00
|
|
|
$result['results'][] = [
|
2016-10-25 15:42:59 +03:00
|
|
|
'label' => $contact['FN'] . " ($emailAddress)",
|
|
|
|
'value' => [
|
|
|
|
'shareType' => Share::SHARE_TYPE_EMAIL,
|
|
|
|
'shareWith' => $emailAddress,
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$this->shareeEnumeration) {
|
2016-10-25 18:13:10 +03:00
|
|
|
$result['results'] = [];
|
2016-10-25 15:42:59 +03:00
|
|
|
}
|
|
|
|
|
2016-10-25 18:13:10 +03:00
|
|
|
if (!$result['exactIdMatch'] && filter_var($search, FILTER_VALIDATE_EMAIL)) {
|
|
|
|
$result['exact'][] = [
|
2016-07-29 16:37:08 +03:00
|
|
|
'label' => $search,
|
|
|
|
'value' => [
|
|
|
|
'shareType' => Share::SHARE_TYPE_EMAIL,
|
|
|
|
'shareWith' => $search,
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
2016-10-25 15:42:59 +03:00
|
|
|
|
|
|
|
$this->reachedEndFor[] = 'emails';
|
|
|
|
|
2016-10-25 18:13:10 +03:00
|
|
|
return $result;
|
2016-07-29 16:37:08 +03:00
|
|
|
}
|
|
|
|
|
2016-11-18 16:08:42 +03:00
|
|
|
protected function getLookup($search) {
|
2016-11-18 19:39:08 +03:00
|
|
|
$isEnabled = $this->config->getAppValue('files_sharing', 'lookupServerEnabled', 'no');
|
2017-04-21 13:23:44 +03:00
|
|
|
$lookupServerUrl = $this->config->getSystemValue('lookup_server', 'https://lookup.nextcloud.com');
|
2017-04-26 14:19:15 +03:00
|
|
|
$lookupServerUrl = rtrim($lookupServerUrl, '/');
|
2016-11-18 16:08:42 +03:00
|
|
|
$result = [];
|
2016-11-18 19:39:08 +03:00
|
|
|
|
|
|
|
if($isEnabled === 'yes') {
|
|
|
|
try {
|
|
|
|
$client = $this->clientService->newClient();
|
|
|
|
$response = $client->get(
|
2017-04-21 13:23:44 +03:00
|
|
|
$lookupServerUrl . '/users?search=' . urlencode($search),
|
2016-11-18 19:39:08 +03:00
|
|
|
[
|
|
|
|
'timeout' => 10,
|
|
|
|
'connect_timeout' => 3,
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
$body = json_decode($response->getBody(), true);
|
|
|
|
|
|
|
|
$result = [];
|
|
|
|
foreach ($body as $lookup) {
|
|
|
|
$result[] = [
|
|
|
|
'label' => $lookup['federationId'],
|
|
|
|
'value' => [
|
|
|
|
'shareType' => Share::SHARE_TYPE_REMOTE,
|
|
|
|
'shareWith' => $lookup['federationId'],
|
|
|
|
],
|
|
|
|
'extra' => $lookup,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
} catch (\Exception $e) {}
|
2016-11-18 16:08:42 +03:00
|
|
|
}
|
2016-11-18 19:39:08 +03:00
|
|
|
|
2016-11-18 16:08:42 +03:00
|
|
|
$this->result['lookup'] = $result;
|
|
|
|
}
|
|
|
|
|
2015-08-11 19:03:07 +03:00
|
|
|
/**
|
2015-08-26 10:40:20 +03:00
|
|
|
* Generates a bunch of pagination links for the current page
|
2015-08-13 11:13:23 +03:00
|
|
|
*
|
2015-08-26 10:40:20 +03:00
|
|
|
* @param int $page Current page
|
|
|
|
* @param array $params Parameters for the URL
|
|
|
|
* @return string
|
2015-08-13 11:13:23 +03:00
|
|
|
*/
|
2015-08-26 10:40:20 +03:00
|
|
|
protected function getPaginationLink($page, array $params) {
|
|
|
|
if ($this->isV2()) {
|
|
|
|
$url = $this->urlGenerator->getAbsoluteURL('/ocs/v2.php/apps/files_sharing/api/v1/sharees') . '?';
|
|
|
|
} else {
|
|
|
|
$url = $this->urlGenerator->getAbsoluteURL('/ocs/v1.php/apps/files_sharing/api/v1/sharees') . '?';
|
2015-08-13 11:13:23 +03:00
|
|
|
}
|
2015-08-26 10:40:20 +03:00
|
|
|
$params['page'] = $page + 1;
|
|
|
|
$link = '<' . $url . http_build_query($params) . '>; rel="next"';
|
2015-08-13 11:13:23 +03:00
|
|
|
|
2015-08-26 10:40:20 +03:00
|
|
|
return $link;
|
2015-08-13 11:13:23 +03:00
|
|
|
}
|
|
|
|
|
2015-08-13 11:57:08 +03:00
|
|
|
/**
|
2015-08-26 10:40:20 +03:00
|
|
|
* @return bool
|
2015-08-13 11:57:08 +03:00
|
|
|
*/
|
2015-08-26 10:40:20 +03:00
|
|
|
protected function isV2() {
|
|
|
|
return $this->request->getScriptName() === '/ocs/v2.php';
|
2015-08-11 19:03:07 +03:00
|
|
|
}
|
2015-08-08 23:15:12 +03:00
|
|
|
}
|