1
0

CollaborationResourcesController.php 10 KB

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