CollaborationResourcesController.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. *
  10. * @license GNU AGPL version 3 or any later version
  11. *
  12. * This program is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License as
  14. * published by the Free Software Foundation, either version 3 of the
  15. * License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. *
  25. */
  26. namespace OC\Core\Controller;
  27. use Exception;
  28. use OCP\AppFramework\Http;
  29. use OCP\AppFramework\Http\DataResponse;
  30. use OCP\AppFramework\OCSController;
  31. use OCP\Collaboration\Resources\CollectionException;
  32. use OCP\Collaboration\Resources\ICollection;
  33. use OCP\Collaboration\Resources\IManager;
  34. use OCP\Collaboration\Resources\IResource;
  35. use OCP\Collaboration\Resources\ResourceException;
  36. use OCP\IRequest;
  37. use OCP\IUserSession;
  38. use Psr\Log\LoggerInterface;
  39. class CollaborationResourcesController extends OCSController {
  40. public function __construct(
  41. string $appName,
  42. IRequest $request,
  43. private IManager $manager,
  44. private IUserSession $userSession,
  45. private LoggerInterface $logger,
  46. ) {
  47. parent::__construct($appName, $request);
  48. }
  49. /**
  50. * @param int $collectionId
  51. * @return ICollection
  52. * @throws CollectionException when the collection was not found for the user
  53. */
  54. protected function getCollection(int $collectionId): ICollection {
  55. $collection = $this->manager->getCollectionForUser($collectionId, $this->userSession->getUser());
  56. if (!$collection->canAccess($this->userSession->getUser())) {
  57. throw new CollectionException('Not found');
  58. }
  59. return $collection;
  60. }
  61. /**
  62. * @NoAdminRequired
  63. *
  64. * @param int $collectionId
  65. * @return DataResponse
  66. */
  67. public function listCollection(int $collectionId): DataResponse {
  68. try {
  69. $collection = $this->getCollection($collectionId);
  70. } catch (CollectionException $e) {
  71. return new DataResponse([], Http::STATUS_NOT_FOUND);
  72. }
  73. return $this->respondCollection($collection);
  74. }
  75. /**
  76. * @NoAdminRequired
  77. *
  78. * @param string $filter
  79. * @return DataResponse
  80. */
  81. public function searchCollections(string $filter): DataResponse {
  82. try {
  83. $collections = $this->manager->searchCollections($this->userSession->getUser(), $filter);
  84. } catch (CollectionException $e) {
  85. return new DataResponse([], Http::STATUS_NOT_FOUND);
  86. }
  87. return new DataResponse($this->prepareCollections($collections));
  88. }
  89. /**
  90. * @NoAdminRequired
  91. *
  92. * @param int $collectionId
  93. * @param string $resourceType
  94. * @param string $resourceId
  95. * @return DataResponse
  96. */
  97. public function addResource(int $collectionId, string $resourceType, string $resourceId): DataResponse {
  98. try {
  99. $collection = $this->getCollection($collectionId);
  100. } catch (CollectionException $e) {
  101. return new DataResponse([], Http::STATUS_NOT_FOUND);
  102. }
  103. $resource = $this->manager->createResource($resourceType, $resourceId);
  104. if (!$resource->canAccess($this->userSession->getUser())) {
  105. return new DataResponse([], Http::STATUS_NOT_FOUND);
  106. }
  107. try {
  108. $collection->addResource($resource);
  109. } catch (ResourceException $e) {
  110. }
  111. return $this->respondCollection($collection);
  112. }
  113. /**
  114. * @NoAdminRequired
  115. *
  116. * @param int $collectionId
  117. * @param string $resourceType
  118. * @param string $resourceId
  119. * @return DataResponse
  120. */
  121. public function removeResource(int $collectionId, string $resourceType, string $resourceId): DataResponse {
  122. try {
  123. $collection = $this->getCollection($collectionId);
  124. } catch (CollectionException $e) {
  125. return new DataResponse([], Http::STATUS_NOT_FOUND);
  126. }
  127. try {
  128. $resource = $this->manager->getResourceForUser($resourceType, $resourceId, $this->userSession->getUser());
  129. } catch (CollectionException $e) {
  130. return new DataResponse([], Http::STATUS_NOT_FOUND);
  131. }
  132. $collection->removeResource($resource);
  133. return $this->respondCollection($collection);
  134. }
  135. /**
  136. * @NoAdminRequired
  137. *
  138. * @param string $resourceType
  139. * @param string $resourceId
  140. * @return DataResponse
  141. */
  142. public function getCollectionsByResource(string $resourceType, string $resourceId): DataResponse {
  143. try {
  144. $resource = $this->manager->getResourceForUser($resourceType, $resourceId, $this->userSession->getUser());
  145. } catch (ResourceException $e) {
  146. $resource = $this->manager->createResource($resourceType, $resourceId);
  147. }
  148. if (!$resource->canAccess($this->userSession->getUser())) {
  149. return new DataResponse([], Http::STATUS_NOT_FOUND);
  150. }
  151. return new DataResponse($this->prepareCollections($resource->getCollections()));
  152. }
  153. /**
  154. * @NoAdminRequired
  155. *
  156. * @param string $baseResourceType
  157. * @param string $baseResourceId
  158. * @param string $name
  159. * @return DataResponse
  160. */
  161. public function createCollectionOnResource(string $baseResourceType, string $baseResourceId, string $name): DataResponse {
  162. if (!isset($name[0]) || isset($name[64])) {
  163. return new DataResponse([], Http::STATUS_BAD_REQUEST);
  164. }
  165. try {
  166. $resource = $this->manager->createResource($baseResourceType, $baseResourceId);
  167. } catch (CollectionException $e) {
  168. return new DataResponse([], Http::STATUS_NOT_FOUND);
  169. }
  170. if (!$resource->canAccess($this->userSession->getUser())) {
  171. return new DataResponse([], Http::STATUS_NOT_FOUND);
  172. }
  173. $collection = $this->manager->newCollection($name);
  174. $collection->addResource($resource);
  175. return $this->respondCollection($collection);
  176. }
  177. /**
  178. * @NoAdminRequired
  179. *
  180. * @param int $collectionId
  181. * @param string $collectionName
  182. * @return DataResponse
  183. */
  184. public function renameCollection(int $collectionId, string $collectionName): DataResponse {
  185. try {
  186. $collection = $this->getCollection($collectionId);
  187. } catch (CollectionException $exception) {
  188. return new DataResponse([], Http::STATUS_NOT_FOUND);
  189. }
  190. $collection->setName($collectionName);
  191. return $this->respondCollection($collection);
  192. }
  193. protected function respondCollection(ICollection $collection): DataResponse {
  194. try {
  195. return new DataResponse($this->prepareCollection($collection));
  196. } catch (CollectionException $e) {
  197. return new DataResponse([], Http::STATUS_NOT_FOUND);
  198. } catch (Exception $e) {
  199. $this->logger->critical($e->getMessage(), ['exception' => $e, 'app' => 'core']);
  200. return new DataResponse([], Http::STATUS_INTERNAL_SERVER_ERROR);
  201. }
  202. }
  203. protected function prepareCollections(array $collections): array {
  204. $result = [];
  205. foreach ($collections as $collection) {
  206. try {
  207. $result[] = $this->prepareCollection($collection);
  208. } catch (CollectionException $e) {
  209. } catch (Exception $e) {
  210. $this->logger->critical($e->getMessage(), ['exception' => $e, 'app' => 'core']);
  211. }
  212. }
  213. return $result;
  214. }
  215. protected function prepareCollection(ICollection $collection): array {
  216. if (!$collection->canAccess($this->userSession->getUser())) {
  217. throw new CollectionException('Can not access collection');
  218. }
  219. return [
  220. 'id' => $collection->getId(),
  221. 'name' => $collection->getName(),
  222. 'resources' => $this->prepareResources($collection->getResources()),
  223. ];
  224. }
  225. protected function prepareResources(array $resources): ?array {
  226. $result = [];
  227. foreach ($resources as $resource) {
  228. try {
  229. $result[] = $this->prepareResource($resource);
  230. } catch (ResourceException $e) {
  231. } catch (Exception $e) {
  232. $this->logger->critical($e->getMessage(), ['exception' => $e, 'app' => 'core']);
  233. }
  234. }
  235. return $result;
  236. }
  237. protected function prepareResource(IResource $resource): array {
  238. if (!$resource->canAccess($this->userSession->getUser())) {
  239. throw new ResourceException('Can not access resource');
  240. }
  241. return $resource->getRichObject();
  242. }
  243. }