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.

80 lines
2.8 KiB

  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\AppFramework\OCSController;
  14. use OCP\IRequest;
  15. use OCP\Teams\ITeamManager;
  16. use OCP\Teams\Team;
  17. use OCP\Teams\TeamResource;
  18. /**
  19. * @psalm-import-type CoreTeamResource from ResponseDefinitions
  20. * @psalm-import-type CoreTeam from ResponseDefinitions
  21. * @psalm-import-type CoreTeamWithResources from ResponseDefinitions
  22. * @property $userId string
  23. */
  24. class TeamsApiController extends OCSController {
  25. public function __construct(
  26. string $appName,
  27. IRequest $request,
  28. private ITeamManager $teamManager,
  29. private ?string $userId,
  30. ) {
  31. parent::__construct($appName, $request);
  32. }
  33. /**
  34. * Get all resources of a team
  35. *
  36. * @param string $teamId Unique id of the team
  37. * @return DataResponse<Http::STATUS_OK, array{resources: list<CoreTeamResource>}, array{}>
  38. *
  39. * 200: Resources returned
  40. */
  41. #[NoAdminRequired]
  42. #[ApiRoute(verb: 'GET', url: '/{teamId}/resources', root: '/teams')]
  43. public function resolveOne(string $teamId): DataResponse {
  44. /** @psalm-suppress PossiblyNullArgument The route is limited to logged-in users */
  45. $resolvedResources = $this->teamManager->getSharedWith($teamId, $this->userId);
  46. return new DataResponse(['resources' => array_map(static fn (TeamResource $resource) => $resource->jsonSerialize(), $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: list<CoreTeamWithResources>}, 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. $sharesPerTeams = $this->teamManager->getSharedWithList(array_map(fn (Team $team): string => $team->getId(), $teams), $this->userId);
  63. $listTeams = array_values(array_map(function (Team $team) use ($sharesPerTeams) {
  64. $response = $team->jsonSerialize();
  65. $response['resources'] = array_map(static fn (TeamResource $resource) => $resource->jsonSerialize(), $sharesPerTeams[$team->getId()] ?? []);
  66. return $response;
  67. }, $teams));
  68. return new DataResponse([
  69. 'teams' => $listTeams,
  70. ]);
  71. }
  72. }