UserPlugin.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 Arthur Schiwon <blizzz@arthur-schiwon.de>
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Georg Ehrke <oc.list@georgehrke.com>
  8. * @author Joas Schilling <coding@schilljs.com>
  9. * @author John Molakvoæ <skjnldsv@protonmail.com>
  10. * @author Julius Härtl <jus@bitgrid.net>
  11. * @author Morris Jobke <hey@morrisjobke.de>
  12. * @author Robin Appelman <robin@icewind.nl>
  13. * @author Roeland Jago Douma <roeland@famdouma.nl>
  14. * @author Thomas Citharel <nextcloud@tcit.fr>
  15. *
  16. * @license GNU AGPL version 3 or any later version
  17. *
  18. * This program is free software: you can redistribute it and/or modify
  19. * it under the terms of the GNU Affero General Public License as
  20. * published by the Free Software Foundation, either version 3 of the
  21. * License, or (at your option) any later version.
  22. *
  23. * This program is distributed in the hope that it will be useful,
  24. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26. * GNU Affero General Public License for more details.
  27. *
  28. * You should have received a copy of the GNU Affero General Public License
  29. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  30. *
  31. */
  32. namespace OC\Collaboration\Collaborators;
  33. use OC\KnownUser\KnownUserService;
  34. use OCP\Collaboration\Collaborators\ISearchPlugin;
  35. use OCP\Collaboration\Collaborators\ISearchResult;
  36. use OCP\Collaboration\Collaborators\SearchResultType;
  37. use OCP\IConfig;
  38. use OCP\IGroupManager;
  39. use OCP\IUser;
  40. use OCP\IUserManager;
  41. use OCP\IUserSession;
  42. use OCP\Share\IShare;
  43. use OCP\UserStatus\IManager as IUserStatusManager;
  44. class UserPlugin implements ISearchPlugin {
  45. /* @var bool */
  46. protected $shareWithGroupOnly;
  47. /* @var bool */
  48. protected $shareeEnumeration;
  49. /* @var bool */
  50. protected $shareeEnumerationInGroupOnly;
  51. /* @var bool */
  52. protected $shareeEnumerationPhone;
  53. /* @var bool */
  54. protected $shareeEnumerationFullMatch;
  55. /* @var bool */
  56. protected $shareeEnumerationFullMatchUserId;
  57. /* @var bool */
  58. protected $shareeEnumerationFullMatchEmail;
  59. /* @var bool */
  60. protected $shareeEnumerationFullMatchIgnoreSecondDisplayName;
  61. /** @var IConfig */
  62. private $config;
  63. /** @var IGroupManager */
  64. private $groupManager;
  65. /** @var IUserSession */
  66. private $userSession;
  67. /** @var IUserManager */
  68. private $userManager;
  69. /** @var KnownUserService */
  70. private $knownUserService;
  71. /** @var IUserStatusManager */
  72. private $userStatusManager;
  73. public function __construct(IConfig $config,
  74. IUserManager $userManager,
  75. IGroupManager $groupManager,
  76. IUserSession $userSession,
  77. KnownUserService $knownUserService,
  78. IUserStatusManager $userStatusManager) {
  79. $this->config = $config;
  80. $this->groupManager = $groupManager;
  81. $this->userSession = $userSession;
  82. $this->userManager = $userManager;
  83. $this->knownUserService = $knownUserService;
  84. $this->userStatusManager = $userStatusManager;
  85. $this->shareWithGroupOnly = $this->config->getAppValue('core', 'shareapi_only_share_with_group_members', 'no') === 'yes';
  86. $this->shareeEnumeration = $this->config->getAppValue('core', 'shareapi_allow_share_dialog_user_enumeration', 'yes') === 'yes';
  87. $this->shareeEnumerationInGroupOnly = $this->shareeEnumeration && $this->config->getAppValue('core', 'shareapi_restrict_user_enumeration_to_group', 'no') === 'yes';
  88. $this->shareeEnumerationPhone = $this->shareeEnumeration && $this->config->getAppValue('core', 'shareapi_restrict_user_enumeration_to_phone', 'no') === 'yes';
  89. $this->shareeEnumerationFullMatch = $this->config->getAppValue('core', 'shareapi_restrict_user_enumeration_full_match', 'yes') === 'yes';
  90. $this->shareeEnumerationFullMatchUserId = $this->config->getAppValue('core', 'shareapi_restrict_user_enumeration_full_match_userid', 'yes') === 'yes';
  91. $this->shareeEnumerationFullMatchEmail = $this->config->getAppValue('core', 'shareapi_restrict_user_enumeration_full_match_email', 'yes') === 'yes';
  92. $this->shareeEnumerationFullMatchIgnoreSecondDisplayName = $this->config->getAppValue('core', 'shareapi_restrict_user_enumeration_full_match_ignore_second_dn', 'no') === 'yes';
  93. }
  94. public function search($search, $limit, $offset, ISearchResult $searchResult) {
  95. $result = ['wide' => [], 'exact' => []];
  96. $users = [];
  97. $hasMoreResults = false;
  98. $currentUserId = $this->userSession->getUser()->getUID();
  99. $currentUserGroups = $this->groupManager->getUserGroupIds($this->userSession->getUser());
  100. if ($this->shareWithGroupOnly || $this->shareeEnumerationInGroupOnly) {
  101. // Search in all the groups this user is part of
  102. foreach ($currentUserGroups as $userGroupId) {
  103. $usersInGroup = $this->groupManager->displayNamesInGroup($userGroupId, $search, $limit, $offset);
  104. foreach ($usersInGroup as $userId => $displayName) {
  105. $userId = (string) $userId;
  106. $user = $this->userManager->get($userId);
  107. if (!$user->isEnabled()) {
  108. // Ignore disabled users
  109. continue;
  110. }
  111. $users[$userId] = $user;
  112. }
  113. if (count($usersInGroup) >= $limit) {
  114. $hasMoreResults = true;
  115. }
  116. }
  117. if (!$this->shareWithGroupOnly && $this->shareeEnumerationPhone) {
  118. $usersTmp = $this->userManager->searchKnownUsersByDisplayName($currentUserId, $search, $limit, $offset);
  119. if (!empty($usersTmp)) {
  120. foreach ($usersTmp as $user) {
  121. if ($user->isEnabled()) { // Don't keep deactivated users
  122. $users[$user->getUID()] = $user;
  123. }
  124. }
  125. uasort($users, function ($a, $b) {
  126. /**
  127. * @var \OC\User\User $a
  128. * @var \OC\User\User $b
  129. */
  130. return strcasecmp($a->getDisplayName(), $b->getDisplayName());
  131. });
  132. }
  133. }
  134. } else {
  135. // Search in all users
  136. if ($this->shareeEnumerationPhone) {
  137. $usersTmp = $this->userManager->searchKnownUsersByDisplayName($currentUserId, $search, $limit, $offset);
  138. } else {
  139. $usersTmp = $this->userManager->searchDisplayName($search, $limit, $offset);
  140. }
  141. foreach ($usersTmp as $user) {
  142. if ($user->isEnabled()) { // Don't keep deactivated users
  143. $users[$user->getUID()] = $user;
  144. }
  145. }
  146. }
  147. $this->takeOutCurrentUser($users);
  148. if (!$this->shareeEnumeration || count($users) < $limit) {
  149. $hasMoreResults = true;
  150. }
  151. $foundUserById = false;
  152. $lowerSearch = strtolower($search);
  153. $userStatuses = $this->userStatusManager->getUserStatuses(array_keys($users));
  154. foreach ($users as $uid => $user) {
  155. $userDisplayName = $user->getDisplayName();
  156. $userEmail = $user->getSystemEMailAddress();
  157. $uid = (string) $uid;
  158. $status = [];
  159. if (array_key_exists($uid, $userStatuses)) {
  160. $userStatus = $userStatuses[$uid];
  161. $status = [
  162. 'status' => $userStatus->getStatus(),
  163. 'message' => $userStatus->getMessage(),
  164. 'icon' => $userStatus->getIcon(),
  165. 'clearAt' => $userStatus->getClearAt()
  166. ? (int)$userStatus->getClearAt()->format('U')
  167. : null,
  168. ];
  169. }
  170. if (
  171. $this->shareeEnumerationFullMatch &&
  172. $lowerSearch !== '' && (strtolower($uid) === $lowerSearch ||
  173. strtolower($userDisplayName) === $lowerSearch ||
  174. ($this->shareeEnumerationFullMatchIgnoreSecondDisplayName && trim(strtolower(preg_replace('/ \(.*\)$/', '', $userDisplayName))) === $lowerSearch) ||
  175. ($this->shareeEnumerationFullMatchEmail && strtolower($userEmail ?? '') === $lowerSearch))
  176. ) {
  177. if (strtolower($uid) === $lowerSearch) {
  178. $foundUserById = true;
  179. }
  180. $result['exact'][] = [
  181. 'label' => $userDisplayName,
  182. 'subline' => $status['message'] ?? '',
  183. 'icon' => 'icon-user',
  184. 'value' => [
  185. 'shareType' => IShare::TYPE_USER,
  186. 'shareWith' => $uid,
  187. ],
  188. 'shareWithDisplayNameUnique' => !empty($userEmail) ? $userEmail : $uid,
  189. 'status' => $status,
  190. ];
  191. } else {
  192. $addToWideResults = false;
  193. if ($this->shareeEnumeration &&
  194. !($this->shareeEnumerationInGroupOnly || $this->shareeEnumerationPhone)) {
  195. $addToWideResults = true;
  196. }
  197. if ($this->shareeEnumerationPhone && $this->knownUserService->isKnownToUser($currentUserId, $user->getUID())) {
  198. $addToWideResults = true;
  199. }
  200. if (!$addToWideResults && $this->shareeEnumerationInGroupOnly) {
  201. $commonGroups = array_intersect($currentUserGroups, $this->groupManager->getUserGroupIds($user));
  202. if (!empty($commonGroups)) {
  203. $addToWideResults = true;
  204. }
  205. }
  206. if ($addToWideResults) {
  207. $result['wide'][] = [
  208. 'label' => $userDisplayName,
  209. 'subline' => $status['message'] ?? '',
  210. 'icon' => 'icon-user',
  211. 'value' => [
  212. 'shareType' => IShare::TYPE_USER,
  213. 'shareWith' => $uid,
  214. ],
  215. 'shareWithDisplayNameUnique' => !empty($userEmail) ? $userEmail : $uid,
  216. 'status' => $status,
  217. ];
  218. }
  219. }
  220. }
  221. if ($this->shareeEnumerationFullMatch && $this->shareeEnumerationFullMatchUserId && $offset === 0 && !$foundUserById) {
  222. // On page one we try if the search result has a direct hit on the
  223. // user id and if so, we add that to the exact match list
  224. $user = $this->userManager->get($search);
  225. if ($user instanceof IUser) {
  226. $addUser = true;
  227. if ($this->shareWithGroupOnly) {
  228. // Only add, if we have a common group
  229. $commonGroups = array_intersect($currentUserGroups, $this->groupManager->getUserGroupIds($user));
  230. $addUser = !empty($commonGroups);
  231. }
  232. if ($addUser) {
  233. $status = [];
  234. $uid = $user->getUID();
  235. $userEmail = $user->getSystemEMailAddress();
  236. if (array_key_exists($user->getUID(), $userStatuses)) {
  237. $userStatus = $userStatuses[$user->getUID()];
  238. $status = [
  239. 'status' => $userStatus->getStatus(),
  240. 'message' => $userStatus->getMessage(),
  241. 'icon' => $userStatus->getIcon(),
  242. 'clearAt' => $userStatus->getClearAt()
  243. ? (int)$userStatus->getClearAt()->format('U')
  244. : null,
  245. ];
  246. }
  247. $result['exact'][] = [
  248. 'label' => $user->getDisplayName(),
  249. 'icon' => 'icon-user',
  250. 'subline' => $status['message'] ?? '',
  251. 'value' => [
  252. 'shareType' => IShare::TYPE_USER,
  253. 'shareWith' => $user->getUID(),
  254. ],
  255. 'shareWithDisplayNameUnique' => $userEmail !== null && $userEmail !== '' ? $userEmail : $uid,
  256. 'status' => $status,
  257. ];
  258. }
  259. }
  260. }
  261. $type = new SearchResultType('users');
  262. $searchResult->addResultSet($type, $result['wide'], $result['exact']);
  263. if (count($result['exact'])) {
  264. $searchResult->markExactIdMatch($type);
  265. }
  266. return $hasMoreResults;
  267. }
  268. public function takeOutCurrentUser(array &$users) {
  269. $currentUser = $this->userSession->getUser();
  270. if (!is_null($currentUser)) {
  271. if (isset($users[$currentUser->getUID()])) {
  272. unset($users[$currentUser->getUID()]);
  273. }
  274. }
  275. }
  276. }