GroupPlugin.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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\IGroup;
  29. use OCP\IGroupManager;
  30. use OCP\IUserSession;
  31. use OCP\Share;
  32. class GroupPlugin implements ISearchPlugin {
  33. protected $shareeEnumeration;
  34. protected $shareWithGroupOnly;
  35. /** @var IGroupManager */
  36. private $groupManager;
  37. /** @var IConfig */
  38. private $config;
  39. /** @var IUserSession */
  40. private $userSession;
  41. public function __construct(IConfig $config, IGroupManager $groupManager, IUserSession $userSession) {
  42. $this->groupManager = $groupManager;
  43. $this->config = $config;
  44. $this->userSession = $userSession;
  45. $this->shareeEnumeration = $this->config->getAppValue('core', 'shareapi_allow_share_dialog_user_enumeration', 'yes') === 'yes';
  46. $this->shareWithGroupOnly = $this->config->getAppValue('core', 'shareapi_only_share_with_group_members', 'no') === 'yes';
  47. }
  48. public function search($search, $limit, $offset, ISearchResult $searchResult) {
  49. $hasMoreResults = false;
  50. $result = ['wide' => [], 'exact' => []];
  51. $groups = $this->groupManager->search($search, $limit, $offset);
  52. $groupIds = array_map(function (IGroup $group) { return $group->getGID(); }, $groups);
  53. if (!$this->shareeEnumeration || count($groups) < $limit) {
  54. $hasMoreResults = true;
  55. }
  56. $userGroups = [];
  57. if (!empty($groups) && $this->shareWithGroupOnly) {
  58. // Intersect all the groups that match with the groups this user is a member of
  59. $userGroups = $this->groupManager->getUserGroups($this->userSession->getUser());
  60. $userGroups = array_map(function (IGroup $group) { return $group->getGID(); }, $userGroups);
  61. $groupIds = array_intersect($groupIds, $userGroups);
  62. }
  63. $lowerSearch = strtolower($search);
  64. foreach ($groups as $group) {
  65. // FIXME: use a more efficient approach
  66. $gid = $group->getGID();
  67. if (!in_array($gid, $groupIds)) {
  68. continue;
  69. }
  70. if (strtolower($gid) === $lowerSearch || strtolower($group->getDisplayName()) === $lowerSearch) {
  71. $result['exact'][] = [
  72. 'label' => $group->getDisplayName(),
  73. 'value' => [
  74. 'shareType' => Share::SHARE_TYPE_GROUP,
  75. 'shareWith' => $gid,
  76. ],
  77. ];
  78. } else {
  79. $result['wide'][] = [
  80. 'label' => $group->getDisplayName(),
  81. 'value' => [
  82. 'shareType' => Share::SHARE_TYPE_GROUP,
  83. 'shareWith' => $gid,
  84. ],
  85. ];
  86. }
  87. }
  88. if ($offset === 0 && empty($result['exact'])) {
  89. // On page one we try if the search result has a direct hit on the
  90. // user id and if so, we add that to the exact match list
  91. $group = $this->groupManager->get($search);
  92. if ($group instanceof IGroup && (!$this->shareWithGroupOnly || in_array($group->getGID(), $userGroups))) {
  93. $result['exact'][] = [
  94. 'label' => $group->getDisplayName(),
  95. 'value' => [
  96. 'shareType' => Share::SHARE_TYPE_GROUP,
  97. 'shareWith' => $group->getGID(),
  98. ],
  99. ];
  100. }
  101. }
  102. if (!$this->shareeEnumeration) {
  103. $result['wide'] = [];
  104. }
  105. $type = new SearchResultType('groups');
  106. $searchResult->addResultSet($type, $result['wide'], $result['exact']);
  107. return $hasMoreResults;
  108. }
  109. }