GroupPlugin.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OC\Collaboration\Collaborators;
  7. use OCP\Collaboration\Collaborators\ISearchPlugin;
  8. use OCP\Collaboration\Collaborators\ISearchResult;
  9. use OCP\Collaboration\Collaborators\SearchResultType;
  10. use OCP\IConfig;
  11. use OCP\IGroup;
  12. use OCP\IGroupManager;
  13. use OCP\IUserSession;
  14. use OCP\Share\IShare;
  15. class GroupPlugin implements ISearchPlugin {
  16. protected bool $shareeEnumeration;
  17. protected bool $shareWithGroupOnly;
  18. protected bool $shareeEnumerationInGroupOnly;
  19. protected bool $groupSharingDisabled;
  20. public function __construct(
  21. private IConfig $config,
  22. private IGroupManager $groupManager,
  23. private IUserSession $userSession,
  24. private mixed $shareWithGroupOnlyExcludeGroupsList = [],
  25. ) {
  26. $this->shareeEnumeration = $this->config->getAppValue('core', 'shareapi_allow_share_dialog_user_enumeration', 'yes') === 'yes';
  27. $this->shareWithGroupOnly = $this->config->getAppValue('core', 'shareapi_only_share_with_group_members', 'no') === 'yes';
  28. $this->shareeEnumerationInGroupOnly = $this->shareeEnumeration && $this->config->getAppValue('core', 'shareapi_restrict_user_enumeration_to_group', 'no') === 'yes';
  29. $this->groupSharingDisabled = $this->config->getAppValue('core', 'shareapi_allow_group_sharing', 'yes') === 'no';
  30. if ($this->shareWithGroupOnly) {
  31. $this->shareWithGroupOnlyExcludeGroupsList = json_decode($this->config->getAppValue('core', 'shareapi_only_share_with_group_members_exclude_group_list', ''), true) ?? [];
  32. }
  33. }
  34. public function search($search, $limit, $offset, ISearchResult $searchResult): bool {
  35. if ($this->groupSharingDisabled) {
  36. return false;
  37. }
  38. $hasMoreResults = false;
  39. $result = ['wide' => [], 'exact' => []];
  40. $groups = $this->groupManager->search($search, $limit, $offset);
  41. $groupIds = array_map(function (IGroup $group) {
  42. return $group->getGID();
  43. }, $groups);
  44. if (!$this->shareeEnumeration || count($groups) < $limit) {
  45. $hasMoreResults = true;
  46. }
  47. $userGroups = [];
  48. if (!empty($groups) && ($this->shareWithGroupOnly || $this->shareeEnumerationInGroupOnly)) {
  49. // Intersect all the groups that match with the groups this user is a member of
  50. $userGroups = $this->groupManager->getUserGroups($this->userSession->getUser());
  51. $userGroups = array_map(function (IGroup $group) {
  52. return $group->getGID();
  53. }, $userGroups);
  54. $groupIds = array_intersect($groupIds, $userGroups);
  55. // ShareWithGroupOnly filtering
  56. $groupIds = array_diff($groupIds, $this->shareWithGroupOnlyExcludeGroupsList);
  57. }
  58. $lowerSearch = strtolower($search);
  59. foreach ($groups as $group) {
  60. if ($group->hideFromCollaboration()) {
  61. continue;
  62. }
  63. // FIXME: use a more efficient approach
  64. $gid = $group->getGID();
  65. if (!in_array($gid, $groupIds)) {
  66. continue;
  67. }
  68. if (strtolower($gid) === $lowerSearch || strtolower($group->getDisplayName()) === $lowerSearch) {
  69. $result['exact'][] = [
  70. 'label' => $group->getDisplayName(),
  71. 'value' => [
  72. 'shareType' => IShare::TYPE_GROUP,
  73. 'shareWith' => $gid,
  74. ],
  75. ];
  76. } else {
  77. if ($this->shareeEnumerationInGroupOnly && !in_array($group->getGID(), $userGroups, true)) {
  78. continue;
  79. }
  80. $result['wide'][] = [
  81. 'label' => $group->getDisplayName(),
  82. 'value' => [
  83. 'shareType' => IShare::TYPE_GROUP,
  84. 'shareWith' => $gid,
  85. ],
  86. ];
  87. }
  88. }
  89. if ($offset === 0 && empty($result['exact'])) {
  90. // On page one we try if the search result has a direct hit on the
  91. // user id and if so, we add that to the exact match list
  92. $group = $this->groupManager->get($search);
  93. if ($group instanceof IGroup && !$group->hideFromCollaboration() && (!$this->shareWithGroupOnly || in_array($group->getGID(), $userGroups))) {
  94. $result['exact'][] = [
  95. 'label' => $group->getDisplayName(),
  96. 'value' => [
  97. 'shareType' => IShare::TYPE_GROUP,
  98. 'shareWith' => $group->getGID(),
  99. ],
  100. ];
  101. }
  102. }
  103. if (!$this->shareeEnumeration) {
  104. $result['wide'] = [];
  105. }
  106. $type = new SearchResultType('groups');
  107. $searchResult->addResultSet($type, $result['wide'], $result['exact']);
  108. return $hasMoreResults;
  109. }
  110. }