GroupPlugin.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. protected bool $shareeEnumeration;
  39. protected bool $shareWithGroupOnly;
  40. protected bool $shareeEnumerationInGroupOnly;
  41. protected bool $groupSharingDisabled;
  42. public function __construct(
  43. private IConfig $config,
  44. private IGroupManager $groupManager,
  45. private IUserSession $userSession,
  46. ) {
  47. $this->shareeEnumeration = $this->config->getAppValue('core', 'shareapi_allow_share_dialog_user_enumeration', 'yes') === 'yes';
  48. $this->shareWithGroupOnly = $this->config->getAppValue('core', 'shareapi_only_share_with_group_members', 'no') === 'yes';
  49. $this->shareeEnumerationInGroupOnly = $this->shareeEnumeration && $this->config->getAppValue('core', 'shareapi_restrict_user_enumeration_to_group', 'no') === 'yes';
  50. $this->groupSharingDisabled = $this->config->getAppValue('core', 'shareapi_allow_group_sharing', 'yes') === 'no';
  51. }
  52. public function search($search, $limit, $offset, ISearchResult $searchResult): bool {
  53. if ($this->groupSharingDisabled) {
  54. return false;
  55. }
  56. $hasMoreResults = false;
  57. $result = ['wide' => [], 'exact' => []];
  58. $groups = $this->groupManager->search($search, $limit, $offset);
  59. $groupIds = array_map(function (IGroup $group) {
  60. return $group->getGID();
  61. }, $groups);
  62. if (!$this->shareeEnumeration || count($groups) < $limit) {
  63. $hasMoreResults = true;
  64. }
  65. $userGroups = [];
  66. if (!empty($groups) && ($this->shareWithGroupOnly || $this->shareeEnumerationInGroupOnly)) {
  67. // Intersect all the groups that match with the groups this user is a member of
  68. $userGroups = $this->groupManager->getUserGroups($this->userSession->getUser());
  69. $userGroups = array_map(function (IGroup $group) {
  70. return $group->getGID();
  71. }, $userGroups);
  72. $groupIds = array_intersect($groupIds, $userGroups);
  73. }
  74. $lowerSearch = strtolower($search);
  75. foreach ($groups as $group) {
  76. if ($group->hideFromCollaboration()) {
  77. continue;
  78. }
  79. // FIXME: use a more efficient approach
  80. $gid = $group->getGID();
  81. if (!in_array($gid, $groupIds)) {
  82. continue;
  83. }
  84. if (strtolower($gid) === $lowerSearch || strtolower($group->getDisplayName()) === $lowerSearch) {
  85. $result['exact'][] = [
  86. 'label' => $group->getDisplayName(),
  87. 'value' => [
  88. 'shareType' => IShare::TYPE_GROUP,
  89. 'shareWith' => $gid,
  90. ],
  91. ];
  92. } else {
  93. if ($this->shareeEnumerationInGroupOnly && !in_array($group->getGID(), $userGroups, true)) {
  94. continue;
  95. }
  96. $result['wide'][] = [
  97. 'label' => $group->getDisplayName(),
  98. 'value' => [
  99. 'shareType' => IShare::TYPE_GROUP,
  100. 'shareWith' => $gid,
  101. ],
  102. ];
  103. }
  104. }
  105. if ($offset === 0 && empty($result['exact'])) {
  106. // On page one we try if the search result has a direct hit on the
  107. // user id and if so, we add that to the exact match list
  108. $group = $this->groupManager->get($search);
  109. if ($group instanceof IGroup && !$group->hideFromCollaboration() && (!$this->shareWithGroupOnly || in_array($group->getGID(), $userGroups))) {
  110. $result['exact'][] = [
  111. 'label' => $group->getDisplayName(),
  112. 'value' => [
  113. 'shareType' => IShare::TYPE_GROUP,
  114. 'shareWith' => $group->getGID(),
  115. ],
  116. ];
  117. }
  118. }
  119. if (!$this->shareeEnumeration) {
  120. $result['wide'] = [];
  121. }
  122. $type = new SearchResultType('groups');
  123. $searchResult->addResultSet($type, $result['wide'], $result['exact']);
  124. return $hasMoreResults;
  125. }
  126. }