RemoteGroupPlugin.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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\Federation\ICloudFederationProviderManager;
  11. use OCP\Federation\ICloudIdManager;
  12. use OCP\Share;
  13. use OCP\Share\IShare;
  14. class RemoteGroupPlugin implements ISearchPlugin {
  15. private bool $enabled = false;
  16. public function __construct(
  17. ICloudFederationProviderManager $cloudFederationProviderManager,
  18. private ICloudIdManager $cloudIdManager,
  19. ) {
  20. try {
  21. $fileSharingProvider = $cloudFederationProviderManager->getCloudFederationProvider('file');
  22. $supportedShareTypes = $fileSharingProvider->getSupportedShareTypes();
  23. if (in_array('group', $supportedShareTypes)) {
  24. $this->enabled = true;
  25. }
  26. } catch (\Exception $e) {
  27. // do nothing, just don't enable federated group shares
  28. }
  29. }
  30. public function search($search, $limit, $offset, ISearchResult $searchResult): bool {
  31. $result = ['wide' => [], 'exact' => []];
  32. $resultType = new SearchResultType('remote_groups');
  33. if ($this->enabled && $this->cloudIdManager->isValidCloudId($search) && $offset === 0) {
  34. [$remoteGroup, $serverUrl] = $this->splitGroupRemote($search);
  35. $result['exact'][] = [
  36. 'label' => $remoteGroup . " ($serverUrl)",
  37. 'guid' => $remoteGroup,
  38. 'name' => $remoteGroup,
  39. 'value' => [
  40. 'shareType' => IShare::TYPE_REMOTE_GROUP,
  41. 'shareWith' => $search,
  42. 'server' => $serverUrl,
  43. ],
  44. ];
  45. }
  46. $searchResult->addResultSet($resultType, $result['wide'], $result['exact']);
  47. return true;
  48. }
  49. /**
  50. * split group and remote from federated cloud id
  51. *
  52. * @param string $address federated share address
  53. * @return array [user, remoteURL]
  54. * @throws \InvalidArgumentException
  55. */
  56. public function splitGroupRemote($address): array {
  57. try {
  58. $cloudId = $this->cloudIdManager->resolveCloudId($address);
  59. return [$cloudId->getUser(), $cloudId->getRemote()];
  60. } catch (\InvalidArgumentException $e) {
  61. throw new \InvalidArgumentException('Invalid Federated Cloud ID', 0, $e);
  62. }
  63. }
  64. }