TeamManager.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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->providers !== null) {
  33. return $this->providers;
  34. }
  35. $this->providers = [];
  36. foreach ($this->bootContext->getRegistrationContext()->getTeamResourceProviders() as $providerRegistration) {
  37. try {
  38. /** @var ITeamResourceProvider $provider */
  39. $provider = Server::get($providerRegistration->getService());
  40. $this->providers[$provider->getId()] = $provider;
  41. } catch (NotFoundExceptionInterface|ContainerExceptionInterface $e) {
  42. }
  43. }
  44. return $this->providers;
  45. }
  46. public function getProvider(string $providerId): ITeamResourceProvider {
  47. $providers = $this->getProviders();
  48. if (isset($providers[$providerId])) {
  49. return $providers[$providerId];
  50. }
  51. throw new \RuntimeException('No provider found for id ' .$providerId);
  52. }
  53. public function getSharedWith(string $teamId, string $userId): array {
  54. if ($this->getTeam($teamId, $userId) === null) {
  55. return [];
  56. }
  57. $resources = [];
  58. foreach ($this->getProviders() as $provider) {
  59. array_push($resources, ...$provider->getSharedWith($teamId));
  60. }
  61. return $resources;
  62. }
  63. public function getTeamsForResource(string $providerId, string $resourceId, string $userId): array {
  64. $provider = $this->getProvider($providerId);
  65. return array_values(array_filter(array_map(function ($teamId) use ($userId) {
  66. $team = $this->getTeam($teamId, $userId);
  67. if ($team === null) {
  68. return null;
  69. }
  70. return new Team(
  71. $teamId,
  72. $team->getDisplayName(),
  73. $this->urlGenerator->linkToRouteAbsolute('contacts.contacts.directcircle', ['singleId' => $teamId]),
  74. );
  75. }, $provider->getTeamsForResource($resourceId))));
  76. }
  77. private function getTeam(string $teamId, string $userId): ?Circle {
  78. try {
  79. $federatedUser = $this->circlesManager->getFederatedUser($userId, Member::TYPE_USER);
  80. $this->circlesManager->startSession($federatedUser);
  81. return $this->circlesManager->getCircle($teamId);
  82. } catch (CircleNotFoundException) {
  83. return null;
  84. }
  85. }
  86. }