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-08-24 15:54:25 +03:00
|
|
|
* @author Georg Ehrke <oc.list@georgehrke.com>
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
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>
|
2020-04-29 12:57:22 +03:00
|
|
|
* @author Robin Appelman <robin@icewind.nl>
|
2020-08-24 15:54:25 +03:00
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
2020-03-31 11:49:10 +03:00
|
|
|
* @author Thomas Citharel <nextcloud@tcit.fr>
|
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\IGroupManager;
|
|
|
|
use OCP\IUser;
|
|
|
|
use OCP\IUserManager;
|
|
|
|
use OCP\IUserSession;
|
2020-02-18 21:44:18 +03:00
|
|
|
use OCP\Share\IShare;
|
2020-08-05 11:37:20 +03:00
|
|
|
use OCP\UserStatus\IManager as IUserStatusManager;
|
2017-08-31 23:47:02 +03:00
|
|
|
|
|
|
|
class UserPlugin implements ISearchPlugin {
|
|
|
|
/* @var bool */
|
|
|
|
protected $shareWithGroupOnly;
|
|
|
|
protected $shareeEnumeration;
|
2020-02-18 21:44:18 +03:00
|
|
|
protected $shareeEnumerationInGroupOnly;
|
2017-08-31 23:47:02 +03:00
|
|
|
|
|
|
|
/** @var IConfig */
|
|
|
|
private $config;
|
|
|
|
/** @var IGroupManager */
|
|
|
|
private $groupManager;
|
|
|
|
/** @var IUserSession */
|
|
|
|
private $userSession;
|
|
|
|
/** @var IUserManager */
|
|
|
|
private $userManager;
|
2020-08-05 11:37:20 +03:00
|
|
|
/** @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,
|
|
|
|
IUserStatusManager $userStatusManager) {
|
2017-08-31 23:47:02 +03:00
|
|
|
$this->config = $config;
|
2017-09-12 18:42:57 +03:00
|
|
|
|
2017-08-31 23:47:02 +03:00
|
|
|
$this->groupManager = $groupManager;
|
|
|
|
$this->userSession = $userSession;
|
|
|
|
$this->userManager = $userManager;
|
2020-08-05 11:37:20 +03:00
|
|
|
$this->userStatusManager = $userStatusManager;
|
2017-08-31 23:47:02 +03:00
|
|
|
|
2017-09-12 18:42:57 +03:00
|
|
|
$this->shareWithGroupOnly = $this->config->getAppValue('core', 'shareapi_only_share_with_group_members', 'no') === 'yes';
|
2017-08-31 23:47:02 +03:00
|
|
|
$this->shareeEnumeration = $this->config->getAppValue('core', 'shareapi_allow_share_dialog_user_enumeration', 'yes') === '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) {
|
|
|
|
$result = ['wide' => [], 'exact' => []];
|
|
|
|
$users = [];
|
|
|
|
$hasMoreResults = false;
|
|
|
|
|
2020-06-17 12:59:56 +03:00
|
|
|
$currentUserGroups = $this->groupManager->getUserGroupIds($this->userSession->getUser());
|
2017-08-31 23:47:02 +03:00
|
|
|
if ($this->shareWithGroupOnly) {
|
|
|
|
// Search in all the groups this user is part of
|
2020-06-17 12:59:56 +03:00
|
|
|
foreach ($currentUserGroups as $userGroupId) {
|
|
|
|
$usersInGroup = $this->groupManager->displayNamesInGroup($userGroupId, $search, $limit, $offset);
|
|
|
|
foreach ($usersInGroup as $userId => $displayName) {
|
|
|
|
$userId = (string) $userId;
|
2020-05-01 17:27:44 +03:00
|
|
|
$user = $this->userManager->get($userId);
|
|
|
|
if (!$user->isEnabled()) {
|
|
|
|
// Ignore disabled users
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$users[$userId] = $user;
|
|
|
|
}
|
|
|
|
if (count($usersInGroup) >= $limit) {
|
|
|
|
$hasMoreResults = true;
|
2017-08-31 23:47:02 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Search in all users
|
|
|
|
$usersTmp = $this->userManager->searchDisplayName($search, $limit, $offset);
|
|
|
|
foreach ($usersTmp as $user) {
|
2018-08-06 10:55:24 +03:00
|
|
|
if ($user->isEnabled()) { // Don't keep deactivated users
|
2019-06-21 16:38:19 +03:00
|
|
|
$users[$user->getUID()] = $user;
|
2018-08-06 10:55:24 +03:00
|
|
|
}
|
2017-08-31 23:47:02 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-17 15:49:18 +03:00
|
|
|
$this->takeOutCurrentUser($users);
|
|
|
|
|
2018-02-13 23:48:24 +03:00
|
|
|
if (!$this->shareeEnumeration || count($users) < $limit) {
|
2017-08-31 23:47:02 +03:00
|
|
|
$hasMoreResults = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$foundUserById = false;
|
|
|
|
$lowerSearch = strtolower($search);
|
2020-08-05 11:37:20 +03:00
|
|
|
$userStatuses = $this->userStatusManager->getUserStatuses(array_keys($users));
|
2019-06-21 16:38:19 +03:00
|
|
|
foreach ($users as $uid => $user) {
|
|
|
|
$userDisplayName = $user->getDisplayName();
|
|
|
|
$userEmail = $user->getEMailAddress();
|
2019-11-19 18:43:46 +03:00
|
|
|
$uid = (string) $uid;
|
2020-08-05 11:37:20 +03:00
|
|
|
|
|
|
|
$status = [];
|
|
|
|
if (array_key_exists($uid, $userStatuses)) {
|
|
|
|
$userStatus = $userStatuses[$uid];
|
|
|
|
$status = [
|
|
|
|
'status' => $userStatus->getStatus(),
|
|
|
|
'message' => $userStatus->getMessage(),
|
|
|
|
'icon' => $userStatus->getIcon(),
|
|
|
|
'clearAt' => $userStatus->getClearAt()
|
|
|
|
? (int)$userStatus->getClearAt()->format('U')
|
|
|
|
: null,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-06-21 16:38:19 +03:00
|
|
|
if (
|
2020-09-28 16:35:11 +03:00
|
|
|
$lowerSearch !== '' && (strtolower($uid) === $lowerSearch ||
|
2019-06-21 16:38:19 +03:00
|
|
|
strtolower($userDisplayName) === $lowerSearch ||
|
2020-09-28 16:35:11 +03:00
|
|
|
strtolower($userEmail) === $lowerSearch)
|
2019-06-21 16:38:19 +03:00
|
|
|
) {
|
2017-08-31 23:47:02 +03:00
|
|
|
if (strtolower($uid) === $lowerSearch) {
|
|
|
|
$foundUserById = true;
|
|
|
|
}
|
|
|
|
$result['exact'][] = [
|
|
|
|
'label' => $userDisplayName,
|
2020-10-20 15:24:01 +03:00
|
|
|
'subline' => $status['message'] ?? '',
|
2020-10-04 16:33:17 +03:00
|
|
|
'icon' => 'icon-user',
|
2017-08-31 23:47:02 +03:00
|
|
|
'value' => [
|
2020-06-24 17:49:16 +03:00
|
|
|
'shareType' => IShare::TYPE_USER,
|
2017-08-31 23:47:02 +03:00
|
|
|
'shareWith' => $uid,
|
|
|
|
],
|
2020-08-05 11:37:20 +03:00
|
|
|
'status' => $status,
|
2017-08-31 23:47:02 +03:00
|
|
|
];
|
|
|
|
} else {
|
2020-06-15 10:29:30 +03:00
|
|
|
$addToWideResults = false;
|
|
|
|
if ($this->shareeEnumeration && !$this->shareeEnumerationInGroupOnly) {
|
|
|
|
$addToWideResults = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->shareeEnumerationInGroupOnly) {
|
|
|
|
$commonGroups = array_intersect($currentUserGroups, $this->groupManager->getUserGroupIds($user));
|
|
|
|
if (!empty($commonGroups)) {
|
|
|
|
$addToWideResults = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($addToWideResults) {
|
|
|
|
$result['wide'][] = [
|
|
|
|
'label' => $userDisplayName,
|
2020-10-20 15:24:01 +03:00
|
|
|
'subline' => $status['message'] ?? '',
|
2020-10-04 16:33:17 +03:00
|
|
|
'icon' => 'icon-user',
|
2020-06-15 10:29:30 +03:00
|
|
|
'value' => [
|
|
|
|
'shareType' => IShare::TYPE_USER,
|
|
|
|
'shareWith' => $uid,
|
|
|
|
],
|
2020-08-05 11:37:20 +03:00
|
|
|
'status' => $status,
|
2020-06-15 10:29:30 +03:00
|
|
|
];
|
|
|
|
}
|
2017-08-31 23:47:02 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($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) {
|
|
|
|
$addUser = true;
|
|
|
|
|
|
|
|
if ($this->shareWithGroupOnly) {
|
|
|
|
// Only add, if we have a common group
|
2020-06-18 12:33:43 +03:00
|
|
|
$commonGroups = array_intersect($currentUserGroups, $this->groupManager->getUserGroupIds($user));
|
2017-08-31 23:47:02 +03:00
|
|
|
$addUser = !empty($commonGroups);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($addUser) {
|
2020-08-05 11:37:20 +03:00
|
|
|
$status = [];
|
|
|
|
if (array_key_exists($user->getUID(), $userStatuses)) {
|
|
|
|
$userStatus = $userStatuses[$user->getUID()];
|
|
|
|
$status = [
|
|
|
|
'status' => $userStatus->getStatus(),
|
|
|
|
'message' => $userStatus->getMessage(),
|
|
|
|
'icon' => $userStatus->getIcon(),
|
|
|
|
'clearAt' => $userStatus->getClearAt()
|
|
|
|
? (int)$userStatus->getClearAt()->format('U')
|
|
|
|
: null,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2018-01-26 00:36:03 +03:00
|
|
|
$result['exact'][] = [
|
2017-08-31 23:47:02 +03:00
|
|
|
'label' => $user->getDisplayName(),
|
2020-10-04 16:33:17 +03:00
|
|
|
'icon' => 'icon-user',
|
2020-10-20 15:24:01 +03:00
|
|
|
'subline' => $status['message'] ?? '',
|
2017-08-31 23:47:02 +03:00
|
|
|
'value' => [
|
2020-06-24 17:49:16 +03:00
|
|
|
'shareType' => IShare::TYPE_USER,
|
2017-08-31 23:47:02 +03:00
|
|
|
'shareWith' => $user->getUID(),
|
|
|
|
],
|
2020-08-05 11:37:20 +03:00
|
|
|
'status' => $status,
|
2018-01-26 00:36:03 +03:00
|
|
|
];
|
2017-08-31 23:47:02 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-15 10:29:30 +03:00
|
|
|
|
2017-08-31 23:47:02 +03:00
|
|
|
|
2017-09-06 17:09:29 +03:00
|
|
|
$type = new SearchResultType('users');
|
|
|
|
$searchResult->addResultSet($type, $result['wide'], $result['exact']);
|
2019-06-21 16:38:19 +03:00
|
|
|
if (count($result['exact'])) {
|
|
|
|
$searchResult->markExactIdMatch($type);
|
|
|
|
}
|
2017-08-31 23:47:02 +03:00
|
|
|
|
|
|
|
return $hasMoreResults;
|
|
|
|
}
|
2018-01-17 15:49:18 +03:00
|
|
|
|
|
|
|
public function takeOutCurrentUser(array &$users) {
|
|
|
|
$currentUser = $this->userSession->getUser();
|
2020-04-10 15:19:56 +03:00
|
|
|
if (!is_null($currentUser)) {
|
2018-01-17 15:49:18 +03:00
|
|
|
if (isset($users[$currentUser->getUID()])) {
|
|
|
|
unset($users[$currentUser->getUID()]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-08-31 23:47:02 +03:00
|
|
|
}
|