TeamManager.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OC\Teams;
  7. use OC\AppFramework\Bootstrap\Coordinator;
  8. use OCA\Circles\CirclesManager;
  9. use OCA\Circles\Exceptions\CircleNotFoundException;
  10. use OCA\Circles\Model\Circle;
  11. use OCA\Circles\Model\Member;
  12. use OCP\IURLGenerator;
  13. use OCP\Server;
  14. use OCP\Teams\ITeamManager;
  15. use OCP\Teams\ITeamResourceProvider;
  16. use OCP\Teams\Team;
  17. use Psr\Container\ContainerExceptionInterface;
  18. use Psr\Container\NotFoundExceptionInterface;
  19. class TeamManager implements ITeamManager {
  20. /** @var ?ITeamResourceProvider[] */
  21. private ?array $providers = null;
  22. public function __construct(
  23. private Coordinator $bootContext,
  24. private IURLGenerator $urlGenerator,
  25. private ?CirclesManager $circlesManager,
  26. ) {
  27. }
  28. public function hasTeamSupport(): bool {
  29. return $this->circlesManager !== null;
  30. }
  31. public function getProviders(): array {
  32. if (!$this->hasTeamSupport()) {
  33. return [];
  34. }
  35. if ($this->providers !== null) {
  36. return $this->providers;
  37. }
  38. $this->providers = [];
  39. foreach ($this->bootContext->getRegistrationContext()->getTeamResourceProviders() as $providerRegistration) {
  40. try {
  41. /** @var ITeamResourceProvider $provider */
  42. $provider = Server::get($providerRegistration->getService());
  43. $this->providers[$provider->getId()] = $provider;
  44. } catch (NotFoundExceptionInterface|ContainerExceptionInterface $e) {
  45. }
  46. }
  47. return $this->providers;
  48. }
  49. public function getProvider(string $providerId): ITeamResourceProvider {
  50. $providers = $this->getProviders();
  51. if (isset($providers[$providerId])) {
  52. return $providers[$providerId];
  53. }
  54. throw new \RuntimeException('No provider found for id ' .$providerId);
  55. }
  56. public function getSharedWith(string $teamId, string $userId): array {
  57. if (!$this->hasTeamSupport()) {
  58. return [];
  59. }
  60. if ($this->getTeam($teamId, $userId) === null) {
  61. return [];
  62. }
  63. $resources = [];
  64. foreach ($this->getProviders() as $provider) {
  65. array_push($resources, ...$provider->getSharedWith($teamId));
  66. }
  67. return $resources;
  68. }
  69. public function getTeamsForResource(string $providerId, string $resourceId, string $userId): array {
  70. if (!$this->hasTeamSupport()) {
  71. return [];
  72. }
  73. $provider = $this->getProvider($providerId);
  74. return array_values(array_filter(array_map(function ($teamId) use ($userId) {
  75. $team = $this->getTeam($teamId, $userId);
  76. if ($team === null) {
  77. return null;
  78. }
  79. return new Team(
  80. $teamId,
  81. $team->getDisplayName(),
  82. $this->urlGenerator->linkToRouteAbsolute('contacts.contacts.directcircle', ['singleId' => $teamId]),
  83. );
  84. }, $provider->getTeamsForResource($resourceId))));
  85. }
  86. private function getTeam(string $teamId, string $userId): ?Circle {
  87. if (!$this->hasTeamSupport()) {
  88. return null;
  89. }
  90. try {
  91. $federatedUser = $this->circlesManager->getFederatedUser($userId, Member::TYPE_USER);
  92. $this->circlesManager->startSession($federatedUser);
  93. return $this->circlesManager->getCircle($teamId);
  94. } catch (CircleNotFoundException) {
  95. return null;
  96. }
  97. }
  98. }