1
0

TeamManager.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2024 Julius Härtl <jus@bitgrid.net>
  4. *
  5. * @author Julius Härtl <jus@bitgrid.net>
  6. *
  7. * @license GNU AGPL version 3 or any later version
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. */
  22. namespace OC\Teams;
  23. use OC\AppFramework\Bootstrap\Coordinator;
  24. use OCA\Circles\CirclesManager;
  25. use OCA\Circles\Exceptions\CircleNotFoundException;
  26. use OCA\Circles\Model\Circle;
  27. use OCA\Circles\Model\Member;
  28. use OCP\IURLGenerator;
  29. use OCP\Server;
  30. use OCP\Teams\ITeamManager;
  31. use OCP\Teams\ITeamResourceProvider;
  32. use OCP\Teams\Team;
  33. use Psr\Container\ContainerExceptionInterface;
  34. use Psr\Container\NotFoundExceptionInterface;
  35. class TeamManager implements ITeamManager {
  36. /** @var ?ITeamResourceProvider[] */
  37. private ?array $providers = null;
  38. public function __construct(
  39. private Coordinator $bootContext,
  40. private IURLGenerator $urlGenerator,
  41. private ?CirclesManager $circlesManager,
  42. ) {
  43. }
  44. public function hasTeamSupport(): bool {
  45. return $this->circlesManager !== null;
  46. }
  47. public function getProviders(): array {
  48. if ($this->providers !== null) {
  49. return $this->providers;
  50. }
  51. $this->providers = [];
  52. foreach ($this->bootContext->getRegistrationContext()->getTeamResourceProviders() as $providerRegistration) {
  53. try {
  54. /** @var ITeamResourceProvider $provider */
  55. $provider = Server::get($providerRegistration->getService());
  56. $this->providers[$provider->getId()] = $provider;
  57. } catch (NotFoundExceptionInterface|ContainerExceptionInterface $e) {
  58. }
  59. }
  60. return $this->providers;
  61. }
  62. public function getProvider(string $providerId): ITeamResourceProvider {
  63. $providers = $this->getProviders();
  64. if (isset($providers[$providerId])) {
  65. return $providers[$providerId];
  66. }
  67. throw new \RuntimeException('No provider found for id ' .$providerId);
  68. }
  69. public function getSharedWith(string $teamId, string $userId): array {
  70. if ($this->getTeam($teamId, $userId) === null) {
  71. return [];
  72. }
  73. $resources = [];
  74. foreach ($this->getProviders() as $provider) {
  75. array_push($resources, ...$provider->getSharedWith($teamId));
  76. }
  77. return $resources;
  78. }
  79. public function getTeamsForResource(string $providerId, string $resourceId, string $userId): array {
  80. $provider = $this->getProvider($providerId);
  81. return array_values(array_filter(array_map(function ($teamId) use ($userId) {
  82. $team = $this->getTeam($teamId, $userId);
  83. if ($team === null) {
  84. return null;
  85. }
  86. return new Team(
  87. $teamId,
  88. $team->getDisplayName(),
  89. $this->urlGenerator->linkToRouteAbsolute('contacts.contacts.directcircle', ['singleId' => $teamId]),
  90. );
  91. }, $provider->getTeamsForResource($resourceId))));
  92. }
  93. private function getTeam(string $teamId, string $userId): ?Circle {
  94. try {
  95. $federatedUser = $this->circlesManager->getFederatedUser($userId, Member::TYPE_USER);
  96. $this->circlesManager->startSession($federatedUser);
  97. return $this->circlesManager->getCircle($teamId);
  98. } catch (CircleNotFoundException) {
  99. return null;
  100. }
  101. }
  102. }