CollaborationResourcesController.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OC\Core\Controller;
  8. use Exception;
  9. use OC\Core\ResponseDefinitions;
  10. use OCP\AppFramework\Http;
  11. use OCP\AppFramework\Http\Attribute\ApiRoute;
  12. use OCP\AppFramework\Http\Attribute\NoAdminRequired;
  13. use OCP\AppFramework\Http\DataResponse;
  14. use OCP\AppFramework\OCSController;
  15. use OCP\Collaboration\Resources\CollectionException;
  16. use OCP\Collaboration\Resources\ICollection;
  17. use OCP\Collaboration\Resources\IManager;
  18. use OCP\Collaboration\Resources\IResource;
  19. use OCP\Collaboration\Resources\ResourceException;
  20. use OCP\IRequest;
  21. use OCP\IUserSession;
  22. use Psr\Log\LoggerInterface;
  23. /**
  24. * @psalm-import-type CoreResource from ResponseDefinitions
  25. * @psalm-import-type CoreCollection from ResponseDefinitions
  26. */
  27. class CollaborationResourcesController extends OCSController {
  28. public function __construct(
  29. string $appName,
  30. IRequest $request,
  31. private IManager $manager,
  32. private IUserSession $userSession,
  33. private LoggerInterface $logger,
  34. ) {
  35. parent::__construct($appName, $request);
  36. }
  37. /**
  38. * @param int $collectionId
  39. * @return ICollection
  40. * @throws CollectionException when the collection was not found for the user
  41. */
  42. protected function getCollection(int $collectionId): ICollection {
  43. $collection = $this->manager->getCollectionForUser($collectionId, $this->userSession->getUser());
  44. if (!$collection->canAccess($this->userSession->getUser())) {
  45. throw new CollectionException('Not found');
  46. }
  47. return $collection;
  48. }
  49. /**
  50. * Get a collection
  51. *
  52. * @param int $collectionId ID of the collection
  53. * @return DataResponse<Http::STATUS_OK, CoreCollection, array{}>|DataResponse<Http::STATUS_NOT_FOUND|Http::STATUS_INTERNAL_SERVER_ERROR, list<empty>, array{}>
  54. *
  55. * 200: Collection returned
  56. * 404: Collection not found
  57. */
  58. #[NoAdminRequired]
  59. #[ApiRoute(verb: 'GET', url: '/resources/collections/{collectionId}', root: '/collaboration')]
  60. public function listCollection(int $collectionId): DataResponse {
  61. try {
  62. $collection = $this->getCollection($collectionId);
  63. } catch (CollectionException $e) {
  64. return new DataResponse([], Http::STATUS_NOT_FOUND);
  65. }
  66. return $this->respondCollection($collection);
  67. }
  68. /**
  69. * Search for collections
  70. *
  71. * @param string $filter Filter collections
  72. * @return DataResponse<Http::STATUS_OK, list<CoreCollection>, array{}>|DataResponse<Http::STATUS_NOT_FOUND, list<empty>, array{}>
  73. *
  74. * 200: Collections returned
  75. * 404: Collection not found
  76. */
  77. #[NoAdminRequired]
  78. #[ApiRoute(verb: 'GET', url: '/resources/collections/search/{filter}', root: '/collaboration')]
  79. public function searchCollections(string $filter): DataResponse {
  80. try {
  81. $collections = $this->manager->searchCollections($this->userSession->getUser(), $filter);
  82. } catch (CollectionException $e) {
  83. return new DataResponse([], Http::STATUS_NOT_FOUND);
  84. }
  85. return new DataResponse($this->prepareCollections($collections));
  86. }
  87. /**
  88. * Add a resource to a collection
  89. *
  90. * @param int $collectionId ID of the collection
  91. * @param string $resourceType Name of the resource
  92. * @param string $resourceId ID of the resource
  93. * @return DataResponse<Http::STATUS_OK, CoreCollection, array{}>|DataResponse<Http::STATUS_NOT_FOUND|Http::STATUS_INTERNAL_SERVER_ERROR, list<empty>, array{}>
  94. *
  95. * 200: Collection returned
  96. * 404: Collection not found or resource inaccessible
  97. */
  98. #[NoAdminRequired]
  99. #[ApiRoute(verb: 'POST', url: '/resources/collections/{collectionId}', root: '/collaboration')]
  100. public function addResource(int $collectionId, string $resourceType, string $resourceId): DataResponse {
  101. try {
  102. $collection = $this->getCollection($collectionId);
  103. } catch (CollectionException $e) {
  104. return new DataResponse([], Http::STATUS_NOT_FOUND);
  105. }
  106. $resource = $this->manager->createResource($resourceType, $resourceId);
  107. if (!$resource->canAccess($this->userSession->getUser())) {
  108. return new DataResponse([], Http::STATUS_NOT_FOUND);
  109. }
  110. try {
  111. $collection->addResource($resource);
  112. } catch (ResourceException $e) {
  113. }
  114. return $this->respondCollection($collection);
  115. }
  116. /**
  117. * Remove a resource from a collection
  118. *
  119. * @param int $collectionId ID of the collection
  120. * @param string $resourceType Name of the resource
  121. * @param string $resourceId ID of the resource
  122. * @return DataResponse<Http::STATUS_OK, CoreCollection, array{}>|DataResponse<Http::STATUS_NOT_FOUND|Http::STATUS_INTERNAL_SERVER_ERROR, list<empty>, array{}>
  123. *
  124. * 200: Collection returned
  125. * 404: Collection or resource not found
  126. */
  127. #[NoAdminRequired]
  128. #[ApiRoute(verb: 'DELETE', url: '/resources/collections/{collectionId}', root: '/collaboration')]
  129. public function removeResource(int $collectionId, string $resourceType, string $resourceId): DataResponse {
  130. try {
  131. $collection = $this->getCollection($collectionId);
  132. } catch (CollectionException $e) {
  133. return new DataResponse([], Http::STATUS_NOT_FOUND);
  134. }
  135. try {
  136. $resource = $this->manager->getResourceForUser($resourceType, $resourceId, $this->userSession->getUser());
  137. } catch (CollectionException $e) {
  138. return new DataResponse([], Http::STATUS_NOT_FOUND);
  139. }
  140. $collection->removeResource($resource);
  141. return $this->respondCollection($collection);
  142. }
  143. /**
  144. * Get collections by resource
  145. *
  146. * @param string $resourceType Type of the resource
  147. * @param string $resourceId ID of the resource
  148. * @return DataResponse<Http::STATUS_OK, list<CoreCollection>, array{}>|DataResponse<Http::STATUS_NOT_FOUND, list<empty>, array{}>
  149. *
  150. * 200: Collections returned
  151. * 404: Resource not accessible
  152. */
  153. #[NoAdminRequired]
  154. #[ApiRoute(verb: 'GET', url: '/resources/{resourceType}/{resourceId}', root: '/collaboration')]
  155. public function getCollectionsByResource(string $resourceType, string $resourceId): DataResponse {
  156. try {
  157. $resource = $this->manager->getResourceForUser($resourceType, $resourceId, $this->userSession->getUser());
  158. } catch (ResourceException $e) {
  159. $resource = $this->manager->createResource($resourceType, $resourceId);
  160. }
  161. if (!$resource->canAccess($this->userSession->getUser())) {
  162. return new DataResponse([], Http::STATUS_NOT_FOUND);
  163. }
  164. return new DataResponse($this->prepareCollections($resource->getCollections()));
  165. }
  166. /**
  167. * Create a collection for a resource
  168. *
  169. * @param string $baseResourceType Type of the base resource
  170. * @param string $baseResourceId ID of the base resource
  171. * @param string $name Name of the collection
  172. * @return DataResponse<Http::STATUS_OK, CoreCollection, array{}>|DataResponse<Http::STATUS_BAD_REQUEST|Http::STATUS_NOT_FOUND|Http::STATUS_INTERNAL_SERVER_ERROR, list<empty>, array{}>
  173. *
  174. * 200: Collection returned
  175. * 400: Creating collection is not possible
  176. * 404: Resource inaccessible
  177. */
  178. #[NoAdminRequired]
  179. #[ApiRoute(verb: 'POST', url: '/resources/{baseResourceType}/{baseResourceId}', root: '/collaboration')]
  180. public function createCollectionOnResource(string $baseResourceType, string $baseResourceId, string $name): DataResponse {
  181. if (!isset($name[0]) || isset($name[64])) {
  182. return new DataResponse([], Http::STATUS_BAD_REQUEST);
  183. }
  184. try {
  185. $resource = $this->manager->createResource($baseResourceType, $baseResourceId);
  186. } catch (CollectionException $e) {
  187. return new DataResponse([], Http::STATUS_NOT_FOUND);
  188. }
  189. if (!$resource->canAccess($this->userSession->getUser())) {
  190. return new DataResponse([], Http::STATUS_NOT_FOUND);
  191. }
  192. $collection = $this->manager->newCollection($name);
  193. $collection->addResource($resource);
  194. return $this->respondCollection($collection);
  195. }
  196. /**
  197. * Rename a collection
  198. *
  199. * @param int $collectionId ID of the collection
  200. * @param string $collectionName New name
  201. * @return DataResponse<Http::STATUS_OK, CoreCollection, array{}>|DataResponse<Http::STATUS_NOT_FOUND|Http::STATUS_INTERNAL_SERVER_ERROR, list<empty>, array{}>
  202. *
  203. * 200: Collection returned
  204. * 404: Collection not found
  205. */
  206. #[NoAdminRequired]
  207. #[ApiRoute(verb: 'PUT', url: '/resources/collections/{collectionId}', root: '/collaboration')]
  208. public function renameCollection(int $collectionId, string $collectionName): DataResponse {
  209. try {
  210. $collection = $this->getCollection($collectionId);
  211. } catch (CollectionException $exception) {
  212. return new DataResponse([], Http::STATUS_NOT_FOUND);
  213. }
  214. $collection->setName($collectionName);
  215. return $this->respondCollection($collection);
  216. }
  217. /**
  218. * @return DataResponse<Http::STATUS_OK, CoreCollection, array{}>|DataResponse<Http::STATUS_NOT_FOUND|Http::STATUS_INTERNAL_SERVER_ERROR, list<empty>, array{}>
  219. */
  220. protected function respondCollection(ICollection $collection): DataResponse {
  221. try {
  222. return new DataResponse($this->prepareCollection($collection));
  223. } catch (CollectionException $e) {
  224. return new DataResponse([], Http::STATUS_NOT_FOUND);
  225. } catch (Exception $e) {
  226. $this->logger->critical($e->getMessage(), ['exception' => $e, 'app' => 'core']);
  227. return new DataResponse([], Http::STATUS_INTERNAL_SERVER_ERROR);
  228. }
  229. }
  230. /**
  231. * @return list<CoreCollection>
  232. */
  233. protected function prepareCollections(array $collections): array {
  234. $result = [];
  235. foreach ($collections as $collection) {
  236. try {
  237. $result[] = $this->prepareCollection($collection);
  238. } catch (CollectionException $e) {
  239. } catch (Exception $e) {
  240. $this->logger->critical($e->getMessage(), ['exception' => $e, 'app' => 'core']);
  241. }
  242. }
  243. return $result;
  244. }
  245. /**
  246. * @return CoreCollection
  247. */
  248. protected function prepareCollection(ICollection $collection): array {
  249. if (!$collection->canAccess($this->userSession->getUser())) {
  250. throw new CollectionException('Can not access collection');
  251. }
  252. return [
  253. 'id' => $collection->getId(),
  254. 'name' => $collection->getName(),
  255. 'resources' => $this->prepareResources($collection->getResources()),
  256. ];
  257. }
  258. /**
  259. * @return list<CoreResource>
  260. */
  261. protected function prepareResources(array $resources): array {
  262. $result = [];
  263. foreach ($resources as $resource) {
  264. try {
  265. $result[] = $this->prepareResource($resource);
  266. } catch (ResourceException $e) {
  267. } catch (Exception $e) {
  268. $this->logger->critical($e->getMessage(), ['exception' => $e, 'app' => 'core']);
  269. }
  270. }
  271. return $result;
  272. }
  273. /**
  274. * @return CoreResource
  275. */
  276. protected function prepareResource(IResource $resource): array {
  277. if (!$resource->canAccess($this->userSession->getUser())) {
  278. throw new ResourceException('Can not access resource');
  279. }
  280. return $resource->getRichObject();
  281. }
  282. }