2017-08-31 23:47:02 +03:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @copyright Copyright (c) 2017 Arthur Schiwon <blizzz@arthur-schiwon.de>
|
|
|
|
*
|
|
|
|
* @author Arthur Schiwon <blizzz@arthur-schiwon.de>
|
2020-04-29 12:57:22 +03:00
|
|
|
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
|
2020-03-31 11:49:10 +03:00
|
|
|
* @author Julius Härtl <jus@bitgrid.net>
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
|
|
|
* @author Robin Appelman <robin@icewind.nl>
|
2017-08-31 23:47:02 +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-08-31 23:47:02 +03:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OC\Collaboration\Collaborators;
|
|
|
|
|
|
|
|
use OCP\Collaboration\Collaborators\ISearchPlugin;
|
|
|
|
use OCP\Collaboration\Collaborators\ISearchResult;
|
2017-09-06 17:09:29 +03:00
|
|
|
use OCP\Collaboration\Collaborators\SearchResultType;
|
2017-08-31 23:47:02 +03:00
|
|
|
use OCP\IConfig;
|
|
|
|
use OCP\IGroup;
|
|
|
|
use OCP\IGroupManager;
|
|
|
|
use OCP\IUserSession;
|
2020-06-24 17:49:16 +03:00
|
|
|
use OCP\Share\IShare;
|
2017-08-31 23:47:02 +03:00
|
|
|
|
|
|
|
class GroupPlugin implements ISearchPlugin {
|
|
|
|
protected $shareeEnumeration;
|
|
|
|
protected $shareWithGroupOnly;
|
|
|
|
|
|
|
|
/** @var IGroupManager */
|
|
|
|
private $groupManager;
|
|
|
|
/** @var IConfig */
|
|
|
|
private $config;
|
|
|
|
/** @var IUserSession */
|
|
|
|
private $userSession;
|
|
|
|
|
|
|
|
public function __construct(IConfig $config, IGroupManager $groupManager, IUserSession $userSession) {
|
|
|
|
$this->groupManager = $groupManager;
|
|
|
|
$this->config = $config;
|
|
|
|
$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';
|
2020-02-18 21:44:18 +03:00
|
|
|
$this->shareeEnumerationInGroupOnly = $this->shareeEnumeration && $this->config->getAppValue('core', 'shareapi_restrict_user_enumeration_to_group', 'no') === 'yes';
|
2017-08-31 23:47:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function search($search, $limit, $offset, ISearchResult $searchResult) {
|
|
|
|
$hasMoreResults = false;
|
|
|
|
$result = ['wide' => [], 'exact' => []];
|
|
|
|
|
|
|
|
$groups = $this->groupManager->search($search, $limit, $offset);
|
2020-04-10 15:19:56 +03:00
|
|
|
$groupIds = array_map(function (IGroup $group) {
|
|
|
|
return $group->getGID();
|
|
|
|
}, $groups);
|
2017-08-31 23:47:02 +03:00
|
|
|
|
2018-02-13 23:48:24 +03:00
|
|
|
if (!$this->shareeEnumeration || count($groups) < $limit) {
|
2017-08-31 23:47:02 +03:00
|
|
|
$hasMoreResults = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$userGroups = [];
|
2020-02-18 21:44:18 +03:00
|
|
|
if (!empty($groups) && ($this->shareWithGroupOnly || $this->shareeEnumerationInGroupOnly)) {
|
2017-08-31 23:47:02 +03:00
|
|
|
// Intersect all the groups that match with the groups this user is a member of
|
|
|
|
$userGroups = $this->groupManager->getUserGroups($this->userSession->getUser());
|
2020-04-10 15:19:56 +03:00
|
|
|
$userGroups = array_map(function (IGroup $group) {
|
|
|
|
return $group->getGID();
|
|
|
|
}, $userGroups);
|
2017-08-31 23:47:02 +03:00
|
|
|
$groupIds = array_intersect($groupIds, $userGroups);
|
|
|
|
}
|
|
|
|
|
|
|
|
$lowerSearch = strtolower($search);
|
|
|
|
foreach ($groups as $group) {
|
2019-02-22 18:36:25 +03:00
|
|
|
if ($group->hideFromCollaboration()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-08-31 23:47:02 +03:00
|
|
|
// FIXME: use a more efficient approach
|
|
|
|
$gid = $group->getGID();
|
|
|
|
if (!in_array($gid, $groupIds)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (strtolower($gid) === $lowerSearch || strtolower($group->getDisplayName()) === $lowerSearch) {
|
|
|
|
$result['exact'][] = [
|
|
|
|
'label' => $group->getDisplayName(),
|
|
|
|
'value' => [
|
2020-06-24 17:49:16 +03:00
|
|
|
'shareType' => IShare::TYPE_GROUP,
|
2017-08-31 23:47:02 +03:00
|
|
|
'shareWith' => $gid,
|
|
|
|
],
|
|
|
|
];
|
|
|
|
} else {
|
2020-02-18 21:44:18 +03:00
|
|
|
if ($this->shareeEnumerationInGroupOnly && !in_array($group->getGID(), $userGroups, true)) {
|
|
|
|
continue;
|
|
|
|
}
|
2017-08-31 23:47:02 +03:00
|
|
|
$result['wide'][] = [
|
|
|
|
'label' => $group->getDisplayName(),
|
|
|
|
'value' => [
|
2020-06-24 17:49:16 +03:00
|
|
|
'shareType' => IShare::TYPE_GROUP,
|
2017-08-31 23:47:02 +03:00
|
|
|
'shareWith' => $gid,
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($offset === 0 && empty($result['exact'])) {
|
|
|
|
// 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);
|
|
|
|
if ($group instanceof IGroup && (!$this->shareWithGroupOnly || in_array($group->getGID(), $userGroups))) {
|
2018-01-26 00:36:03 +03:00
|
|
|
$result['exact'][] = [
|
2017-08-31 23:47:02 +03:00
|
|
|
'label' => $group->getDisplayName(),
|
|
|
|
'value' => [
|
2020-06-24 17:49:16 +03:00
|
|
|
'shareType' => IShare::TYPE_GROUP,
|
2017-08-31 23:47:02 +03:00
|
|
|
'shareWith' => $group->getGID(),
|
|
|
|
],
|
2018-01-26 00:36:03 +03:00
|
|
|
];
|
2017-08-31 23:47:02 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$this->shareeEnumeration) {
|
|
|
|
$result['wide'] = [];
|
|
|
|
}
|
|
|
|
|
2017-09-06 17:09:29 +03:00
|
|
|
$type = new SearchResultType('groups');
|
|
|
|
$searchResult->addResultSet($type, $result['wide'], $result['exact']);
|
2017-08-31 23:47:02 +03:00
|
|
|
|
2017-09-06 17:09:29 +03:00
|
|
|
return $hasMoreResults;
|
2017-08-31 23:47:02 +03:00
|
|
|
}
|
|
|
|
}
|