TeamsApiController.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OC\Core\Controller;
  8. use OC\Core\ResponseDefinitions;
  9. use OCP\AppFramework\Http;
  10. use OCP\AppFramework\Http\Attribute\ApiRoute;
  11. use OCP\AppFramework\Http\Attribute\NoAdminRequired;
  12. use OCP\AppFramework\Http\DataResponse;
  13. use OCP\IRequest;
  14. use OCP\Teams\ITeamManager;
  15. use OCP\Teams\Team;
  16. /**
  17. * @psalm-import-type CoreTeamResource from ResponseDefinitions
  18. * @psalm-import-type CoreTeam from ResponseDefinitions
  19. * @property $userId string
  20. */
  21. class TeamsApiController extends \OCP\AppFramework\OCSController {
  22. public function __construct(
  23. string $appName,
  24. IRequest $request,
  25. private ITeamManager $teamManager,
  26. private ?string $userId,
  27. ) {
  28. parent::__construct($appName, $request);
  29. }
  30. /**
  31. * Get all resources of a team
  32. *
  33. * @param string $teamId Unique id of the team
  34. * @return DataResponse<Http::STATUS_OK, array{resources: CoreTeamResource[]}, array{}>
  35. *
  36. * 200: Resources returned
  37. */
  38. #[NoAdminRequired]
  39. #[ApiRoute(verb: 'GET', url: '/{teamId}/resources', root: '/teams')]
  40. public function resolveOne(string $teamId): DataResponse {
  41. /**
  42. * @var CoreTeamResource[] $resolvedResources
  43. * @psalm-suppress PossiblyNullArgument The route is limited to logged-in users
  44. */
  45. $resolvedResources = $this->teamManager->getSharedWith($teamId, $this->userId);
  46. return new DataResponse(['resources' => $resolvedResources]);
  47. }
  48. /**
  49. * Get all teams of a resource
  50. *
  51. * @param string $providerId Identifier of the provider (e.g. deck, talk, collectives)
  52. * @param string $resourceId Unique id of the resource to list teams for (e.g. deck board id)
  53. * @return DataResponse<Http::STATUS_OK, array{teams: CoreTeam[]}, array{}>
  54. *
  55. * 200: Teams returned
  56. */
  57. #[NoAdminRequired]
  58. #[ApiRoute(verb: 'GET', url: '/resources/{providerId}/{resourceId}', root: '/teams')]
  59. public function listTeams(string $providerId, string $resourceId): DataResponse {
  60. /** @psalm-suppress PossiblyNullArgument The route is limited to logged-in users */
  61. $teams = $this->teamManager->getTeamsForResource($providerId, $resourceId, $this->userId);
  62. /** @var CoreTeam[] $teams */
  63. $teams = array_map(function (Team $team) {
  64. $response = $team->jsonSerialize();
  65. /** @psalm-suppress PossiblyNullArgument The route is limited to logged in users */
  66. $response['resources'] = $this->teamManager->getSharedWith($team->getId(), $this->userId);
  67. return $response;
  68. }, $teams);
  69. return new DataResponse([
  70. 'teams' => $teams,
  71. ]);
  72. }
  73. }