You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RemoteGroupPlugin.php 3.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. protected $shareeEnumeration;
  35. /** @var ICloudIdManager */
  36. private $cloudIdManager;
  37. /** @var bool */
  38. private $enabled = false;
  39. public function __construct(ICloudFederationProviderManager $cloudFederationProviderManager, ICloudIdManager $cloudIdManager) {
  40. try {
  41. $fileSharingProvider = $cloudFederationProviderManager->getCloudFederationProvider('file');
  42. $supportedShareTypes = $fileSharingProvider->getSupportedShareTypes();
  43. if (in_array('group', $supportedShareTypes)) {
  44. $this->enabled = true;
  45. }
  46. } catch (\Exception $e) {
  47. // do nothing, just don't enable federated group shares
  48. }
  49. $this->cloudIdManager = $cloudIdManager;
  50. }
  51. public function search($search, $limit, $offset, ISearchResult $searchResult) {
  52. $result = ['wide' => [], 'exact' => []];
  53. $resultType = new SearchResultType('remote_groups');
  54. if ($this->enabled && $this->cloudIdManager->isValidCloudId($search) && $offset === 0) {
  55. [$remoteGroup, $serverUrl] = $this->splitGroupRemote($search);
  56. $result['exact'][] = [
  57. 'label' => $remoteGroup . " ($serverUrl)",
  58. 'guid' => $remoteGroup,
  59. 'name' => $remoteGroup,
  60. 'value' => [
  61. 'shareType' => IShare::TYPE_REMOTE_GROUP,
  62. 'shareWith' => $search,
  63. 'server' => $serverUrl,
  64. ],
  65. ];
  66. }
  67. $searchResult->addResultSet($resultType, $result['wide'], $result['exact']);
  68. return true;
  69. }
  70. /**
  71. * split group and remote from federated cloud id
  72. *
  73. * @param string $address federated share address
  74. * @return array [user, remoteURL]
  75. * @throws \InvalidArgumentException
  76. */
  77. public function splitGroupRemote($address) {
  78. try {
  79. $cloudId = $this->cloudIdManager->resolveCloudId($address);
  80. return [$cloudId->getUser(), $cloudId->getRemote()];
  81. } catch (\InvalidArgumentException $e) {
  82. throw new \InvalidArgumentException('Invalid Federated Cloud ID', 0, $e);
  83. }
  84. }
  85. }