GroupPlugin.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 Joas Schilling <coding@schilljs.com>
  8. * @author Julius Härtl <jus@bitgrid.net>
  9. * @author Morris Jobke <hey@morrisjobke.de>
  10. * @author Robin Appelman <robin@icewind.nl>
  11. *
  12. * @license GNU AGPL version 3 or any later version
  13. *
  14. * This program is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License as
  16. * published by the Free Software Foundation, either version 3 of the
  17. * License, or (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU Affero General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Affero General Public License
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  26. *
  27. */
  28. namespace OC\Collaboration\Collaborators;
  29. use OCP\Collaboration\Collaborators\ISearchPlugin;
  30. use OCP\Collaboration\Collaborators\ISearchResult;
  31. use OCP\Collaboration\Collaborators\SearchResultType;
  32. use OCP\IConfig;
  33. use OCP\IGroup;
  34. use OCP\IGroupManager;
  35. use OCP\IUserSession;
  36. use OCP\Share\IShare;
  37. class GroupPlugin implements ISearchPlugin {
  38. /** @var bool */
  39. protected $shareeEnumeration;
  40. /** @var bool */
  41. protected $shareWithGroupOnly;
  42. /** @var bool */
  43. protected $shareeEnumerationInGroupOnly;
  44. /** @var bool */
  45. protected $groupSharingDisabled;
  46. /** @var IGroupManager */
  47. private $groupManager;
  48. /** @var IConfig */
  49. private $config;
  50. /** @var IUserSession */
  51. private $userSession;
  52. public function __construct(IConfig $config, IGroupManager $groupManager, IUserSession $userSession) {
  53. $this->groupManager = $groupManager;
  54. $this->config = $config;
  55. $this->userSession = $userSession;
  56. $this->shareeEnumeration = $this->config->getAppValue('core', 'shareapi_allow_share_dialog_user_enumeration', 'yes') === 'yes';
  57. $this->shareWithGroupOnly = $this->config->getAppValue('core', 'shareapi_only_share_with_group_members', 'no') === 'yes';
  58. $this->shareeEnumerationInGroupOnly = $this->shareeEnumeration && $this->config->getAppValue('core', 'shareapi_restrict_user_enumeration_to_group', 'no') === 'yes';
  59. $this->groupSharingDisabled = $this->config->getAppValue('core', 'shareapi_allow_group_sharing', 'yes') === 'no';
  60. }
  61. public function search($search, $limit, $offset, ISearchResult $searchResult) {
  62. if ($this->groupSharingDisabled) {
  63. return false;
  64. }
  65. $hasMoreResults = false;
  66. $result = ['wide' => [], 'exact' => []];
  67. $groups = $this->groupManager->search($search, $limit, $offset);
  68. $groupIds = array_map(function (IGroup $group) {
  69. return $group->getGID();
  70. }, $groups);
  71. if (!$this->shareeEnumeration || count($groups) < $limit) {
  72. $hasMoreResults = true;
  73. }
  74. $userGroups = [];
  75. if (!empty($groups) && ($this->shareWithGroupOnly || $this->shareeEnumerationInGroupOnly)) {
  76. // Intersect all the groups that match with the groups this user is a member of
  77. $userGroups = $this->groupManager->getUserGroups($this->userSession->getUser());
  78. $userGroups = array_map(function (IGroup $group) {
  79. return $group->getGID();
  80. }, $userGroups);
  81. $groupIds = array_intersect($groupIds, $userGroups);
  82. }
  83. $lowerSearch = strtolower($search);
  84. foreach ($groups as $group) {
  85. if ($group->hideFromCollaboration()) {
  86. continue;
  87. }
  88. // FIXME: use a more efficient approach
  89. $gid = $group->getGID();
  90. if (!in_array($gid, $groupIds)) {
  91. continue;
  92. }
  93. if (strtolower($gid) === $lowerSearch || strtolower($group->getDisplayName()) === $lowerSearch) {
  94. $result['exact'][] = [
  95. 'label' => $group->getDisplayName(),
  96. 'value' => [
  97. 'shareType' => IShare::TYPE_GROUP,
  98. 'shareWith' => $gid,
  99. ],
  100. ];
  101. } else {
  102. if ($this->shareeEnumerationInGroupOnly && !in_array($group->getGID(), $userGroups, true)) {
  103. continue;
  104. }
  105. $result['wide'][] = [
  106. 'label' => $group->getDisplayName(),
  107. 'value' => [
  108. 'shareType' => IShare::TYPE_GROUP,
  109. 'shareWith' => $gid,
  110. ],
  111. ];
  112. }
  113. }
  114. if ($offset === 0 && empty($result['exact'])) {
  115. // On page one we try if the search result has a direct hit on the
  116. // user id and if so, we add that to the exact match list
  117. $group = $this->groupManager->get($search);
  118. if ($group instanceof IGroup && !$group->hideFromCollaboration() && (!$this->shareWithGroupOnly || in_array($group->getGID(), $userGroups))) {
  119. $result['exact'][] = [
  120. 'label' => $group->getDisplayName(),
  121. 'value' => [
  122. 'shareType' => IShare::TYPE_GROUP,
  123. 'shareWith' => $group->getGID(),
  124. ],
  125. ];
  126. }
  127. }
  128. if (!$this->shareeEnumeration) {
  129. $result['wide'] = [];
  130. }
  131. $type = new SearchResultType('groups');
  132. $searchResult->addResultSet($type, $result['wide'], $result['exact']);
  133. return $hasMoreResults;
  134. }
  135. }