RemoteGroupPlugin.php 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 Arthur Schiwon <blizzz@arthur-schiwon.de>
  4. *
  5. * @author Bjoern Schiessle <bjoern@schiessle.org>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author John Molakvoæ <skjnldsv@protonmail.com>
  8. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. namespace OC\Collaboration\Collaborators;
  26. use OCP\Collaboration\Collaborators\ISearchPlugin;
  27. use OCP\Collaboration\Collaborators\ISearchResult;
  28. use OCP\Collaboration\Collaborators\SearchResultType;
  29. use OCP\Federation\ICloudFederationProviderManager;
  30. use OCP\Federation\ICloudIdManager;
  31. use OCP\Share;
  32. use OCP\Share\IShare;
  33. class RemoteGroupPlugin implements ISearchPlugin {
  34. private bool $enabled = false;
  35. public function __construct(
  36. ICloudFederationProviderManager $cloudFederationProviderManager,
  37. private ICloudIdManager $cloudIdManager,
  38. ) {
  39. try {
  40. $fileSharingProvider = $cloudFederationProviderManager->getCloudFederationProvider('file');
  41. $supportedShareTypes = $fileSharingProvider->getSupportedShareTypes();
  42. if (in_array('group', $supportedShareTypes)) {
  43. $this->enabled = true;
  44. }
  45. } catch (\Exception $e) {
  46. // do nothing, just don't enable federated group shares
  47. }
  48. }
  49. public function search($search, $limit, $offset, ISearchResult $searchResult): bool {
  50. $result = ['wide' => [], 'exact' => []];
  51. $resultType = new SearchResultType('remote_groups');
  52. if ($this->enabled && $this->cloudIdManager->isValidCloudId($search) && $offset === 0) {
  53. [$remoteGroup, $serverUrl] = $this->splitGroupRemote($search);
  54. $result['exact'][] = [
  55. 'label' => $remoteGroup . " ($serverUrl)",
  56. 'guid' => $remoteGroup,
  57. 'name' => $remoteGroup,
  58. 'value' => [
  59. 'shareType' => IShare::TYPE_REMOTE_GROUP,
  60. 'shareWith' => $search,
  61. 'server' => $serverUrl,
  62. ],
  63. ];
  64. }
  65. $searchResult->addResultSet($resultType, $result['wide'], $result['exact']);
  66. return true;
  67. }
  68. /**
  69. * split group and remote from federated cloud id
  70. *
  71. * @param string $address federated share address
  72. * @return array [user, remoteURL]
  73. * @throws \InvalidArgumentException
  74. */
  75. public function splitGroupRemote($address): array {
  76. try {
  77. $cloudId = $this->cloudIdManager->resolveCloudId($address);
  78. return [$cloudId->getUser(), $cloudId->getRemote()];
  79. } catch (\InvalidArgumentException $e) {
  80. throw new \InvalidArgumentException('Invalid Federated Cloud ID', 0, $e);
  81. }
  82. }
  83. }