UserPlugin.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. protected bool $shareWithGroupOnly;
  46. protected bool $shareeEnumeration;
  47. protected bool $shareeEnumerationInGroupOnly;
  48. protected bool $shareeEnumerationPhone;
  49. protected bool $shareeEnumerationFullMatch;
  50. protected bool $shareeEnumerationFullMatchUserId;
  51. protected bool $shareeEnumerationFullMatchEmail;
  52. protected bool $shareeEnumerationFullMatchIgnoreSecondDisplayName;
  53. public function __construct(
  54. private IConfig $config,
  55. private IUserManager $userManager,
  56. private IGroupManager $groupManager,
  57. private IUserSession $userSession,
  58. private KnownUserService $knownUserService,
  59. private IUserStatusManager $userStatusManager,
  60. ) {
  61. $this->shareWithGroupOnly = $this->config->getAppValue('core', 'shareapi_only_share_with_group_members', 'no') === 'yes';
  62. $this->shareeEnumeration = $this->config->getAppValue('core', 'shareapi_allow_share_dialog_user_enumeration', 'yes') === 'yes';
  63. $this->shareeEnumerationInGroupOnly = $this->shareeEnumeration && $this->config->getAppValue('core', 'shareapi_restrict_user_enumeration_to_group', 'no') === 'yes';
  64. $this->shareeEnumerationPhone = $this->shareeEnumeration && $this->config->getAppValue('core', 'shareapi_restrict_user_enumeration_to_phone', 'no') === 'yes';
  65. $this->shareeEnumerationFullMatch = $this->config->getAppValue('core', 'shareapi_restrict_user_enumeration_full_match', 'yes') === 'yes';
  66. $this->shareeEnumerationFullMatchUserId = $this->config->getAppValue('core', 'shareapi_restrict_user_enumeration_full_match_userid', 'yes') === 'yes';
  67. $this->shareeEnumerationFullMatchEmail = $this->config->getAppValue('core', 'shareapi_restrict_user_enumeration_full_match_email', 'yes') === 'yes';
  68. $this->shareeEnumerationFullMatchIgnoreSecondDisplayName = $this->config->getAppValue('core', 'shareapi_restrict_user_enumeration_full_match_ignore_second_dn', 'no') === 'yes';
  69. }
  70. public function search($search, $limit, $offset, ISearchResult $searchResult): bool {
  71. $result = ['wide' => [], 'exact' => []];
  72. $users = [];
  73. $hasMoreResults = false;
  74. $currentUserId = $this->userSession->getUser()->getUID();
  75. $currentUserGroups = $this->groupManager->getUserGroupIds($this->userSession->getUser());
  76. if ($this->shareWithGroupOnly || $this->shareeEnumerationInGroupOnly) {
  77. // Search in all the groups this user is part of
  78. foreach ($currentUserGroups as $userGroupId) {
  79. $usersInGroup = $this->groupManager->displayNamesInGroup($userGroupId, $search, $limit, $offset);
  80. foreach ($usersInGroup as $userId => $displayName) {
  81. $userId = (string) $userId;
  82. $user = $this->userManager->get($userId);
  83. if (!$user->isEnabled()) {
  84. // Ignore disabled users
  85. continue;
  86. }
  87. $users[$userId] = $user;
  88. }
  89. if (count($usersInGroup) >= $limit) {
  90. $hasMoreResults = true;
  91. }
  92. }
  93. if (!$this->shareWithGroupOnly && $this->shareeEnumerationPhone) {
  94. $usersTmp = $this->userManager->searchKnownUsersByDisplayName($currentUserId, $search, $limit, $offset);
  95. if (!empty($usersTmp)) {
  96. foreach ($usersTmp as $user) {
  97. if ($user->isEnabled()) { // Don't keep deactivated users
  98. $users[$user->getUID()] = $user;
  99. }
  100. }
  101. uasort($users, function ($a, $b) {
  102. /**
  103. * @var \OC\User\User $a
  104. * @var \OC\User\User $b
  105. */
  106. return strcasecmp($a->getDisplayName(), $b->getDisplayName());
  107. });
  108. }
  109. }
  110. } else {
  111. // Search in all users
  112. if ($this->shareeEnumerationPhone) {
  113. $usersTmp = $this->userManager->searchKnownUsersByDisplayName($currentUserId, $search, $limit, $offset);
  114. } else {
  115. $usersTmp = $this->userManager->searchDisplayName($search, $limit, $offset);
  116. }
  117. foreach ($usersTmp as $user) {
  118. if ($user->isEnabled()) { // Don't keep deactivated users
  119. $users[$user->getUID()] = $user;
  120. }
  121. }
  122. }
  123. $this->takeOutCurrentUser($users);
  124. if (!$this->shareeEnumeration || count($users) < $limit) {
  125. $hasMoreResults = true;
  126. }
  127. $foundUserById = false;
  128. $lowerSearch = strtolower($search);
  129. $userStatuses = $this->userStatusManager->getUserStatuses(array_keys($users));
  130. foreach ($users as $uid => $user) {
  131. $userDisplayName = $user->getDisplayName();
  132. $userEmail = $user->getSystemEMailAddress();
  133. $uid = (string) $uid;
  134. $status = [];
  135. if (array_key_exists($uid, $userStatuses)) {
  136. $userStatus = $userStatuses[$uid];
  137. $status = [
  138. 'status' => $userStatus->getStatus(),
  139. 'message' => $userStatus->getMessage(),
  140. 'icon' => $userStatus->getIcon(),
  141. 'clearAt' => $userStatus->getClearAt()
  142. ? (int)$userStatus->getClearAt()->format('U')
  143. : null,
  144. ];
  145. }
  146. if (
  147. $this->shareeEnumerationFullMatch &&
  148. $lowerSearch !== '' && (strtolower($uid) === $lowerSearch ||
  149. strtolower($userDisplayName) === $lowerSearch ||
  150. ($this->shareeEnumerationFullMatchIgnoreSecondDisplayName && trim(strtolower(preg_replace('/ \(.*\)$/', '', $userDisplayName))) === $lowerSearch) ||
  151. ($this->shareeEnumerationFullMatchEmail && strtolower($userEmail ?? '') === $lowerSearch))
  152. ) {
  153. if (strtolower($uid) === $lowerSearch) {
  154. $foundUserById = true;
  155. }
  156. $result['exact'][] = [
  157. 'label' => $userDisplayName,
  158. 'subline' => $status['message'] ?? '',
  159. 'icon' => 'icon-user',
  160. 'value' => [
  161. 'shareType' => IShare::TYPE_USER,
  162. 'shareWith' => $uid,
  163. ],
  164. 'shareWithDisplayNameUnique' => !empty($userEmail) ? $userEmail : $uid,
  165. 'status' => $status,
  166. ];
  167. } else {
  168. $addToWideResults = false;
  169. if ($this->shareeEnumeration &&
  170. !($this->shareeEnumerationInGroupOnly || $this->shareeEnumerationPhone)) {
  171. $addToWideResults = true;
  172. }
  173. if ($this->shareeEnumerationPhone && $this->knownUserService->isKnownToUser($currentUserId, $user->getUID())) {
  174. $addToWideResults = true;
  175. }
  176. if (!$addToWideResults && $this->shareeEnumerationInGroupOnly) {
  177. $commonGroups = array_intersect($currentUserGroups, $this->groupManager->getUserGroupIds($user));
  178. if (!empty($commonGroups)) {
  179. $addToWideResults = true;
  180. }
  181. }
  182. if ($addToWideResults) {
  183. $result['wide'][] = [
  184. 'label' => $userDisplayName,
  185. 'subline' => $status['message'] ?? '',
  186. 'icon' => 'icon-user',
  187. 'value' => [
  188. 'shareType' => IShare::TYPE_USER,
  189. 'shareWith' => $uid,
  190. ],
  191. 'shareWithDisplayNameUnique' => !empty($userEmail) ? $userEmail : $uid,
  192. 'status' => $status,
  193. ];
  194. }
  195. }
  196. }
  197. if ($this->shareeEnumerationFullMatch && $this->shareeEnumerationFullMatchUserId && $offset === 0 && !$foundUserById) {
  198. // On page one we try if the search result has a direct hit on the
  199. // user id and if so, we add that to the exact match list
  200. $user = $this->userManager->get($search);
  201. if ($user instanceof IUser) {
  202. $addUser = true;
  203. if ($this->shareWithGroupOnly) {
  204. // Only add, if we have a common group
  205. $commonGroups = array_intersect($currentUserGroups, $this->groupManager->getUserGroupIds($user));
  206. $addUser = !empty($commonGroups);
  207. }
  208. if ($addUser) {
  209. $status = [];
  210. $uid = $user->getUID();
  211. $userEmail = $user->getSystemEMailAddress();
  212. if (array_key_exists($user->getUID(), $userStatuses)) {
  213. $userStatus = $userStatuses[$user->getUID()];
  214. $status = [
  215. 'status' => $userStatus->getStatus(),
  216. 'message' => $userStatus->getMessage(),
  217. 'icon' => $userStatus->getIcon(),
  218. 'clearAt' => $userStatus->getClearAt()
  219. ? (int)$userStatus->getClearAt()->format('U')
  220. : null,
  221. ];
  222. }
  223. $result['exact'][] = [
  224. 'label' => $user->getDisplayName(),
  225. 'icon' => 'icon-user',
  226. 'subline' => $status['message'] ?? '',
  227. 'value' => [
  228. 'shareType' => IShare::TYPE_USER,
  229. 'shareWith' => $user->getUID(),
  230. ],
  231. 'shareWithDisplayNameUnique' => $userEmail !== null && $userEmail !== '' ? $userEmail : $uid,
  232. 'status' => $status,
  233. ];
  234. }
  235. }
  236. }
  237. $type = new SearchResultType('users');
  238. $searchResult->addResultSet($type, $result['wide'], $result['exact']);
  239. if (count($result['exact'])) {
  240. $searchResult->markExactIdMatch($type);
  241. }
  242. return $hasMoreResults;
  243. }
  244. public function takeOutCurrentUser(array &$users): void {
  245. $currentUser = $this->userSession->getUser();
  246. if (!is_null($currentUser)) {
  247. if (isset($users[$currentUser->getUID()])) {
  248. unset($users[$currentUser->getUID()]);
  249. }
  250. }
  251. }
  252. }