CollaborationResourcesController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2018 Joas Schilling <coding@schilljs.com>
  5. *
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Julius Härtl <jus@bitgrid.net>
  8. * @author Roeland Jago Douma <roeland@famdouma.nl>
  9. * @author Kate Döen <kate.doeen@nextcloud.com>
  10. *
  11. * @license GNU AGPL version 3 or any later version
  12. *
  13. * This program is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License as
  15. * published by the Free Software Foundation, either version 3 of the
  16. * License, or (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  25. *
  26. */
  27. namespace OC\Core\Controller;
  28. use Exception;
  29. use OCA\Core\ResponseDefinitions;
  30. use OCP\AppFramework\Http;
  31. use OCP\AppFramework\Http\DataResponse;
  32. use OCP\AppFramework\OCSController;
  33. use OCP\Collaboration\Resources\CollectionException;
  34. use OCP\Collaboration\Resources\ICollection;
  35. use OCP\Collaboration\Resources\IManager;
  36. use OCP\Collaboration\Resources\IResource;
  37. use OCP\Collaboration\Resources\ResourceException;
  38. use OCP\IRequest;
  39. use OCP\IUserSession;
  40. use Psr\Log\LoggerInterface;
  41. /**
  42. * @psalm-import-type CoreOpenGraphObject from ResponseDefinitions
  43. * @psalm-import-type CoreCollection from ResponseDefinitions
  44. */
  45. class CollaborationResourcesController extends OCSController {
  46. public function __construct(
  47. string $appName,
  48. IRequest $request,
  49. private IManager $manager,
  50. private IUserSession $userSession,
  51. private LoggerInterface $logger,
  52. ) {
  53. parent::__construct($appName, $request);
  54. }
  55. /**
  56. * @param int $collectionId
  57. * @return ICollection
  58. * @throws CollectionException when the collection was not found for the user
  59. */
  60. protected function getCollection(int $collectionId): ICollection {
  61. $collection = $this->manager->getCollectionForUser($collectionId, $this->userSession->getUser());
  62. if (!$collection->canAccess($this->userSession->getUser())) {
  63. throw new CollectionException('Not found');
  64. }
  65. return $collection;
  66. }
  67. /**
  68. * @NoAdminRequired
  69. *
  70. * Get a collection
  71. *
  72. * @param int $collectionId ID of the collection
  73. * @return DataResponse<Http::STATUS_OK, CoreCollection, array{}>|DataResponse<Http::STATUS_NOT_FOUND|Http::STATUS_INTERNAL_SERVER_ERROR, array<empty>, array{}>
  74. *
  75. * 200: Collection returned
  76. * 404: Collection not found
  77. */
  78. public function listCollection(int $collectionId): DataResponse {
  79. try {
  80. $collection = $this->getCollection($collectionId);
  81. } catch (CollectionException $e) {
  82. return new DataResponse([], Http::STATUS_NOT_FOUND);
  83. }
  84. return $this->respondCollection($collection);
  85. }
  86. /**
  87. * @NoAdminRequired
  88. *
  89. * Search for collections
  90. *
  91. * @param string $filter Filter collections
  92. * @return DataResponse<Http::STATUS_OK, CoreCollection[], array{}>|DataResponse<Http::STATUS_NOT_FOUND, array<empty>, array{}>
  93. *
  94. * 200: Collections returned
  95. * 404: Collection not found
  96. */
  97. public function searchCollections(string $filter): DataResponse {
  98. try {
  99. $collections = $this->manager->searchCollections($this->userSession->getUser(), $filter);
  100. } catch (CollectionException $e) {
  101. return new DataResponse([], Http::STATUS_NOT_FOUND);
  102. }
  103. return new DataResponse($this->prepareCollections($collections));
  104. }
  105. /**
  106. * @NoAdminRequired
  107. *
  108. * Add a resource to a collection
  109. *
  110. * @param int $collectionId ID of the collection
  111. * @param string $resourceType Name of the resource
  112. * @param string $resourceId ID of the resource
  113. * @return DataResponse<Http::STATUS_OK, CoreCollection, array{}>|DataResponse<Http::STATUS_NOT_FOUND|Http::STATUS_INTERNAL_SERVER_ERROR, array<empty>, array{}>
  114. *
  115. * 200: Collection returned
  116. * 404: Collection not found or resource inaccessible
  117. */
  118. public function addResource(int $collectionId, string $resourceType, string $resourceId): DataResponse {
  119. try {
  120. $collection = $this->getCollection($collectionId);
  121. } catch (CollectionException $e) {
  122. return new DataResponse([], Http::STATUS_NOT_FOUND);
  123. }
  124. $resource = $this->manager->createResource($resourceType, $resourceId);
  125. if (!$resource->canAccess($this->userSession->getUser())) {
  126. return new DataResponse([], Http::STATUS_NOT_FOUND);
  127. }
  128. try {
  129. $collection->addResource($resource);
  130. } catch (ResourceException $e) {
  131. }
  132. return $this->respondCollection($collection);
  133. }
  134. /**
  135. * @NoAdminRequired
  136. *
  137. * Remove a resource from a collection
  138. *
  139. * @param int $collectionId ID of the collection
  140. * @param string $resourceType Name of the resource
  141. * @param string $resourceId ID of the resource
  142. * @return DataResponse<Http::STATUS_OK, CoreCollection, array{}>|DataResponse<Http::STATUS_NOT_FOUND|Http::STATUS_INTERNAL_SERVER_ERROR, array<empty>, array{}>
  143. *
  144. * 200: Collection returned
  145. * 404: Collection or resource not found
  146. */
  147. public function removeResource(int $collectionId, string $resourceType, string $resourceId): DataResponse {
  148. try {
  149. $collection = $this->getCollection($collectionId);
  150. } catch (CollectionException $e) {
  151. return new DataResponse([], Http::STATUS_NOT_FOUND);
  152. }
  153. try {
  154. $resource = $this->manager->getResourceForUser($resourceType, $resourceId, $this->userSession->getUser());
  155. } catch (CollectionException $e) {
  156. return new DataResponse([], Http::STATUS_NOT_FOUND);
  157. }
  158. $collection->removeResource($resource);
  159. return $this->respondCollection($collection);
  160. }
  161. /**
  162. * @NoAdminRequired
  163. *
  164. * Get collections by resource
  165. *
  166. * @param string $resourceType Type of the resource
  167. * @param string $resourceId ID of the resource
  168. * @return DataResponse<Http::STATUS_OK, CoreCollection[], array{}>|DataResponse<Http::STATUS_NOT_FOUND, array<empty>, array{}>
  169. *
  170. * 200: Collections returned
  171. * 404: Resource not accessible
  172. */
  173. public function getCollectionsByResource(string $resourceType, string $resourceId): DataResponse {
  174. try {
  175. $resource = $this->manager->getResourceForUser($resourceType, $resourceId, $this->userSession->getUser());
  176. } catch (ResourceException $e) {
  177. $resource = $this->manager->createResource($resourceType, $resourceId);
  178. }
  179. if (!$resource->canAccess($this->userSession->getUser())) {
  180. return new DataResponse([], Http::STATUS_NOT_FOUND);
  181. }
  182. return new DataResponse($this->prepareCollections($resource->getCollections()));
  183. }
  184. /**
  185. * @NoAdminRequired
  186. *
  187. * Create a collection for a resource
  188. *
  189. * @param string $baseResourceType Type of the base resource
  190. * @param string $baseResourceId ID of the base resource
  191. * @param string $name Name of the collection
  192. * @return DataResponse<Http::STATUS_OK, CoreCollection, array{}>|DataResponse<Http::STATUS_BAD_REQUEST|Http::STATUS_NOT_FOUND|Http::STATUS_INTERNAL_SERVER_ERROR, array<empty>, array{}>
  193. *
  194. * 200: Collection returned
  195. * 400: Creating collection is not possible
  196. * 404: Resource inaccessible
  197. */
  198. public function createCollectionOnResource(string $baseResourceType, string $baseResourceId, string $name): DataResponse {
  199. if (!isset($name[0]) || isset($name[64])) {
  200. return new DataResponse([], Http::STATUS_BAD_REQUEST);
  201. }
  202. try {
  203. $resource = $this->manager->createResource($baseResourceType, $baseResourceId);
  204. } catch (CollectionException $e) {
  205. return new DataResponse([], Http::STATUS_NOT_FOUND);
  206. }
  207. if (!$resource->canAccess($this->userSession->getUser())) {
  208. return new DataResponse([], Http::STATUS_NOT_FOUND);
  209. }
  210. $collection = $this->manager->newCollection($name);
  211. $collection->addResource($resource);
  212. return $this->respondCollection($collection);
  213. }
  214. /**
  215. * @NoAdminRequired
  216. *
  217. * Rename a collection
  218. *
  219. * @param int $collectionId ID of the collection
  220. * @param string $collectionName New name
  221. * @return DataResponse<Http::STATUS_OK, CoreCollection, array{}>|DataResponse<Http::STATUS_NOT_FOUND|Http::STATUS_INTERNAL_SERVER_ERROR, array<empty>, array{}>
  222. *
  223. * 200: Collection returned
  224. * 404: Collection not found
  225. */
  226. public function renameCollection(int $collectionId, string $collectionName): DataResponse {
  227. try {
  228. $collection = $this->getCollection($collectionId);
  229. } catch (CollectionException $exception) {
  230. return new DataResponse([], Http::STATUS_NOT_FOUND);
  231. }
  232. $collection->setName($collectionName);
  233. return $this->respondCollection($collection);
  234. }
  235. /**
  236. * @return DataResponse<Http::STATUS_OK, CoreCollection, array{}>|DataResponse<Http::STATUS_NOT_FOUND|Http::STATUS_INTERNAL_SERVER_ERROR, array<empty>, array{}>
  237. */
  238. protected function respondCollection(ICollection $collection): DataResponse {
  239. try {
  240. return new DataResponse($this->prepareCollection($collection));
  241. } catch (CollectionException $e) {
  242. return new DataResponse([], Http::STATUS_NOT_FOUND);
  243. } catch (Exception $e) {
  244. $this->logger->critical($e->getMessage(), ['exception' => $e, 'app' => 'core']);
  245. return new DataResponse([], Http::STATUS_INTERNAL_SERVER_ERROR);
  246. }
  247. }
  248. /**
  249. * @return CoreCollection[]
  250. */
  251. protected function prepareCollections(array $collections): array {
  252. $result = [];
  253. foreach ($collections as $collection) {
  254. try {
  255. $result[] = $this->prepareCollection($collection);
  256. } catch (CollectionException $e) {
  257. } catch (Exception $e) {
  258. $this->logger->critical($e->getMessage(), ['exception' => $e, 'app' => 'core']);
  259. }
  260. }
  261. return $result;
  262. }
  263. /**
  264. * @return CoreCollection
  265. */
  266. protected function prepareCollection(ICollection $collection): array {
  267. if (!$collection->canAccess($this->userSession->getUser())) {
  268. throw new CollectionException('Can not access collection');
  269. }
  270. return [
  271. 'id' => $collection->getId(),
  272. 'name' => $collection->getName(),
  273. 'resources' => $this->prepareResources($collection->getResources()),
  274. ];
  275. }
  276. /**
  277. * @return CoreOpenGraphObject[]
  278. */
  279. protected function prepareResources(array $resources): array {
  280. $result = [];
  281. foreach ($resources as $resource) {
  282. try {
  283. $result[] = $this->prepareResource($resource);
  284. } catch (ResourceException $e) {
  285. } catch (Exception $e) {
  286. $this->logger->critical($e->getMessage(), ['exception' => $e, 'app' => 'core']);
  287. }
  288. }
  289. return $result;
  290. }
  291. /**
  292. * @return CoreOpenGraphObject
  293. */
  294. protected function prepareResource(IResource $resource): array {
  295. if (!$resource->canAccess($this->userSession->getUser())) {
  296. throw new ResourceException('Can not access resource');
  297. }
  298. return $resource->getRichObject();
  299. }
  300. }