TeamsApiController.php 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2024 Julius Härtl <jus@bitgrid.net>
  5. *
  6. * @author Julius Härtl <jus@bitgrid.net>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. */
  23. namespace OC\Core\Controller;
  24. use OCA\Core\ResponseDefinitions;
  25. use OCP\AppFramework\Http;
  26. use OCP\AppFramework\Http\Attribute\ApiRoute;
  27. use OCP\AppFramework\Http\Attribute\NoAdminRequired;
  28. use OCP\AppFramework\Http\DataResponse;
  29. use OCP\IRequest;
  30. use OCP\Teams\ITeamManager;
  31. use OCP\Teams\Team;
  32. /**
  33. * @psalm-import-type CoreTeamResource from ResponseDefinitions
  34. * @psalm-import-type CoreTeam from ResponseDefinitions
  35. * @property $userId string
  36. */
  37. class TeamsApiController extends \OCP\AppFramework\OCSController {
  38. public function __construct(
  39. string $appName,
  40. IRequest $request,
  41. private ITeamManager $teamManager,
  42. private ?string $userId,
  43. ) {
  44. parent::__construct($appName, $request);
  45. }
  46. /**
  47. * Get all resources of a team
  48. *
  49. * @param string $teamId Unique id of the team
  50. * @return DataResponse<Http::STATUS_OK, array{resources: CoreTeamResource[]}, array{}>
  51. *
  52. * 200: Resources returned
  53. */
  54. #[NoAdminRequired]
  55. #[ApiRoute(verb: 'GET', url: '/{teamId}/resources', root: '/teams')]
  56. public function resolveOne(string $teamId): DataResponse {
  57. /**
  58. * @var CoreTeamResource[] $resolvedResources
  59. * @psalm-suppress PossiblyNullArgument The route is limited to logged-in users
  60. */
  61. $resolvedResources = $this->teamManager->getSharedWith($teamId, $this->userId);
  62. return new DataResponse(['resources' => $resolvedResources]);
  63. }
  64. /**
  65. * Get all teams of a resource
  66. *
  67. * @param string $providerId Identifier of the provider (e.g. deck, talk, collectives)
  68. * @param string $resourceId Unique id of the resource to list teams for (e.g. deck board id)
  69. * @return DataResponse<Http::STATUS_OK, array{teams: CoreTeam[]}, array{}>
  70. *
  71. * 200: Teams returned
  72. */
  73. #[NoAdminRequired]
  74. #[ApiRoute(verb: 'GET', url: '/resources/{providerId}/{resourceId}', root: '/teams')]
  75. public function listTeams(string $providerId, string $resourceId): DataResponse {
  76. /** @psalm-suppress PossiblyNullArgument The route is limited to logged-in users */
  77. $teams = $this->teamManager->getTeamsForResource($providerId, $resourceId, $this->userId);
  78. /** @var CoreTeam[] $teams */
  79. $teams = array_map(function (Team $team) {
  80. $response = $team->jsonSerialize();
  81. /** @psalm-suppress PossiblyNullArgument The route is limited to logged in users */
  82. $response['resources'] = $this->teamManager->getSharedWith($team->getId(), $this->userId);
  83. return $response;
  84. }, $teams);
  85. return new DataResponse([
  86. 'teams' => $teams,
  87. ]);
  88. }
  89. }