UserPlugin.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 Arthur Schiwon <blizzz@arthur-schiwon.de>
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. *
  7. * @license GNU AGPL version 3 or any later version
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. namespace OC\Collaboration\Collaborators;
  24. use OCP\Collaboration\Collaborators\ISearchPlugin;
  25. use OCP\Collaboration\Collaborators\ISearchResult;
  26. use OCP\Collaboration\Collaborators\SearchResultType;
  27. use OCP\IConfig;
  28. use OCP\IGroupManager;
  29. use OCP\IUser;
  30. use OCP\IUserManager;
  31. use OCP\IUserSession;
  32. use OCP\Share;
  33. class UserPlugin implements ISearchPlugin {
  34. /* @var bool */
  35. protected $shareWithGroupOnly;
  36. protected $shareeEnumeration;
  37. /** @var IConfig */
  38. private $config;
  39. /** @var IGroupManager */
  40. private $groupManager;
  41. /** @var IUserSession */
  42. private $userSession;
  43. /** @var IUserManager */
  44. private $userManager;
  45. public function __construct(IConfig $config, IUserManager $userManager, IGroupManager $groupManager, IUserSession $userSession) {
  46. $this->config = $config;
  47. $this->groupManager = $groupManager;
  48. $this->userSession = $userSession;
  49. $this->userManager = $userManager;
  50. $this->shareWithGroupOnly = $this->config->getAppValue('core', 'shareapi_only_share_with_group_members', 'no') === 'yes';
  51. $this->shareeEnumeration = $this->config->getAppValue('core', 'shareapi_allow_share_dialog_user_enumeration', 'yes') === 'yes';
  52. }
  53. public function search($search, $limit, $offset, ISearchResult $searchResult) {
  54. $result = ['wide' => [], 'exact' => []];
  55. $users = [];
  56. $hasMoreResults = false;
  57. $userGroups = [];
  58. if ($this->shareWithGroupOnly) {
  59. // Search in all the groups this user is part of
  60. $userGroups = $this->groupManager->getUserGroupIds($this->userSession->getUser());
  61. foreach ($userGroups as $userGroup) {
  62. $usersTmp = $this->groupManager->displayNamesInGroup($userGroup, $search, $limit, $offset);
  63. foreach ($usersTmp as $uid => $userDisplayName) {
  64. $users[$uid] = $userDisplayName;
  65. }
  66. }
  67. } else {
  68. // Search in all users
  69. $usersTmp = $this->userManager->searchDisplayName($search, $limit, $offset);
  70. foreach ($usersTmp as $user) {
  71. if ($user->isEnabled()) { // Don't keep deactivated users
  72. $users[$user->getUID()] = $user->getDisplayName();
  73. }
  74. }
  75. }
  76. $this->takeOutCurrentUser($users);
  77. if (!$this->shareeEnumeration || count($users) < $limit) {
  78. $hasMoreResults = true;
  79. }
  80. $foundUserById = false;
  81. $lowerSearch = strtolower($search);
  82. foreach ($users as $uid => $userDisplayName) {
  83. if (strtolower($uid) === $lowerSearch || strtolower($userDisplayName) === $lowerSearch) {
  84. if (strtolower($uid) === $lowerSearch) {
  85. $foundUserById = true;
  86. }
  87. $result['exact'][] = [
  88. 'label' => $userDisplayName,
  89. 'value' => [
  90. 'shareType' => Share::SHARE_TYPE_USER,
  91. 'shareWith' => $uid,
  92. ],
  93. ];
  94. } else {
  95. $result['wide'][] = [
  96. 'label' => $userDisplayName,
  97. 'value' => [
  98. 'shareType' => Share::SHARE_TYPE_USER,
  99. 'shareWith' => $uid,
  100. ],
  101. ];
  102. }
  103. }
  104. if ($offset === 0 && !$foundUserById) {
  105. // On page one we try if the search result has a direct hit on the
  106. // user id and if so, we add that to the exact match list
  107. $user = $this->userManager->get($search);
  108. if ($user instanceof IUser) {
  109. $addUser = true;
  110. if ($this->shareWithGroupOnly) {
  111. // Only add, if we have a common group
  112. $commonGroups = array_intersect($userGroups, $this->groupManager->getUserGroupIds($user));
  113. $addUser = !empty($commonGroups);
  114. }
  115. if ($addUser) {
  116. $result['exact'][] = [
  117. 'label' => $user->getDisplayName(),
  118. 'value' => [
  119. 'shareType' => Share::SHARE_TYPE_USER,
  120. 'shareWith' => $user->getUID(),
  121. ],
  122. ];
  123. }
  124. }
  125. }
  126. if (!$this->shareeEnumeration) {
  127. $result['wide'] = [];
  128. }
  129. $type = new SearchResultType('users');
  130. $searchResult->addResultSet($type, $result['wide'], $result['exact']);
  131. return $hasMoreResults;
  132. }
  133. public function takeOutCurrentUser(array &$users) {
  134. $currentUser = $this->userSession->getUser();
  135. if(!is_null($currentUser)) {
  136. if (isset($users[$currentUser->getUID()])) {
  137. unset($users[$currentUser->getUID()]);
  138. }
  139. }
  140. }
  141. }