CollaborationResourcesController.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. private IManager $manager;
  41. private IUserSession $userSession;
  42. private LoggerInterface $logger;
  43. public function __construct(
  44. string $appName,
  45. IRequest $request,
  46. IManager $manager,
  47. IUserSession $userSession,
  48. LoggerInterface $logger
  49. ) {
  50. parent::__construct($appName, $request);
  51. $this->manager = $manager;
  52. $this->userSession = $userSession;
  53. $this->logger = $logger;
  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. * @param int $collectionId
  71. * @return DataResponse
  72. */
  73. public function listCollection(int $collectionId): DataResponse {
  74. try {
  75. $collection = $this->getCollection($collectionId);
  76. } catch (CollectionException $e) {
  77. return new DataResponse([], Http::STATUS_NOT_FOUND);
  78. }
  79. return $this->respondCollection($collection);
  80. }
  81. /**
  82. * @NoAdminRequired
  83. *
  84. * @param string $filter
  85. * @return DataResponse
  86. */
  87. public function searchCollections(string $filter): DataResponse {
  88. try {
  89. $collections = $this->manager->searchCollections($this->userSession->getUser(), $filter);
  90. } catch (CollectionException $e) {
  91. return new DataResponse([], Http::STATUS_NOT_FOUND);
  92. }
  93. return new DataResponse($this->prepareCollections($collections));
  94. }
  95. /**
  96. * @NoAdminRequired
  97. *
  98. * @param int $collectionId
  99. * @param string $resourceType
  100. * @param string $resourceId
  101. * @return DataResponse
  102. */
  103. public function addResource(int $collectionId, string $resourceType, string $resourceId): DataResponse {
  104. try {
  105. $collection = $this->getCollection($collectionId);
  106. } catch (CollectionException $e) {
  107. return new DataResponse([], Http::STATUS_NOT_FOUND);
  108. }
  109. $resource = $this->manager->createResource($resourceType, $resourceId);
  110. if (!$resource->canAccess($this->userSession->getUser())) {
  111. return new DataResponse([], Http::STATUS_NOT_FOUND);
  112. }
  113. try {
  114. $collection->addResource($resource);
  115. } catch (ResourceException $e) {
  116. }
  117. return $this->respondCollection($collection);
  118. }
  119. /**
  120. * @NoAdminRequired
  121. *
  122. * @param int $collectionId
  123. * @param string $resourceType
  124. * @param string $resourceId
  125. * @return DataResponse
  126. */
  127. public function removeResource(int $collectionId, string $resourceType, string $resourceId): DataResponse {
  128. try {
  129. $collection = $this->getCollection($collectionId);
  130. } catch (CollectionException $e) {
  131. return new DataResponse([], Http::STATUS_NOT_FOUND);
  132. }
  133. try {
  134. $resource = $this->manager->getResourceForUser($resourceType, $resourceId, $this->userSession->getUser());
  135. } catch (CollectionException $e) {
  136. return new DataResponse([], Http::STATUS_NOT_FOUND);
  137. }
  138. $collection->removeResource($resource);
  139. return $this->respondCollection($collection);
  140. }
  141. /**
  142. * @NoAdminRequired
  143. *
  144. * @param string $resourceType
  145. * @param string $resourceId
  146. * @return DataResponse
  147. */
  148. public function getCollectionsByResource(string $resourceType, string $resourceId): DataResponse {
  149. try {
  150. $resource = $this->manager->getResourceForUser($resourceType, $resourceId, $this->userSession->getUser());
  151. } catch (ResourceException $e) {
  152. $resource = $this->manager->createResource($resourceType, $resourceId);
  153. }
  154. if (!$resource->canAccess($this->userSession->getUser())) {
  155. return new DataResponse([], Http::STATUS_NOT_FOUND);
  156. }
  157. return new DataResponse($this->prepareCollections($resource->getCollections()));
  158. }
  159. /**
  160. * @NoAdminRequired
  161. *
  162. * @param string $baseResourceType
  163. * @param string $baseResourceId
  164. * @param string $name
  165. * @return DataResponse
  166. */
  167. public function createCollectionOnResource(string $baseResourceType, string $baseResourceId, string $name): DataResponse {
  168. if (!isset($name[0]) || isset($name[64])) {
  169. return new DataResponse([], Http::STATUS_BAD_REQUEST);
  170. }
  171. try {
  172. $resource = $this->manager->createResource($baseResourceType, $baseResourceId);
  173. } catch (CollectionException $e) {
  174. return new DataResponse([], Http::STATUS_NOT_FOUND);
  175. }
  176. if (!$resource->canAccess($this->userSession->getUser())) {
  177. return new DataResponse([], Http::STATUS_NOT_FOUND);
  178. }
  179. $collection = $this->manager->newCollection($name);
  180. $collection->addResource($resource);
  181. return $this->respondCollection($collection);
  182. }
  183. /**
  184. * @NoAdminRequired
  185. *
  186. * @param int $collectionId
  187. * @param string $collectionName
  188. * @return DataResponse
  189. */
  190. public function renameCollection(int $collectionId, string $collectionName): DataResponse {
  191. try {
  192. $collection = $this->getCollection($collectionId);
  193. } catch (CollectionException $exception) {
  194. return new DataResponse([], Http::STATUS_NOT_FOUND);
  195. }
  196. $collection->setName($collectionName);
  197. return $this->respondCollection($collection);
  198. }
  199. protected function respondCollection(ICollection $collection): DataResponse {
  200. try {
  201. return new DataResponse($this->prepareCollection($collection));
  202. } catch (CollectionException $e) {
  203. return new DataResponse([], Http::STATUS_NOT_FOUND);
  204. } catch (Exception $e) {
  205. $this->logger->critical($e->getMessage(), ['exception' => $e, 'app' => 'core']);
  206. return new DataResponse([], Http::STATUS_INTERNAL_SERVER_ERROR);
  207. }
  208. }
  209. protected function prepareCollections(array $collections): array {
  210. $result = [];
  211. foreach ($collections as $collection) {
  212. try {
  213. $result[] = $this->prepareCollection($collection);
  214. } catch (CollectionException $e) {
  215. } catch (Exception $e) {
  216. $this->logger->critical($e->getMessage(), ['exception' => $e, 'app' => 'core']);
  217. }
  218. }
  219. return $result;
  220. }
  221. protected function prepareCollection(ICollection $collection): array {
  222. if (!$collection->canAccess($this->userSession->getUser())) {
  223. throw new CollectionException('Can not access collection');
  224. }
  225. return [
  226. 'id' => $collection->getId(),
  227. 'name' => $collection->getName(),
  228. 'resources' => $this->prepareResources($collection->getResources()),
  229. ];
  230. }
  231. protected function prepareResources(array $resources): ?array {
  232. $result = [];
  233. foreach ($resources as $resource) {
  234. try {
  235. $result[] = $this->prepareResource($resource);
  236. } catch (ResourceException $e) {
  237. } catch (Exception $e) {
  238. $this->logger->critical($e->getMessage(), ['exception' => $e, 'app' => 'core']);
  239. }
  240. }
  241. return $result;
  242. }
  243. protected function prepareResource(IResource $resource): array {
  244. if (!$resource->canAccess($this->userSession->getUser())) {
  245. throw new ResourceException('Can not access resource');
  246. }
  247. return $resource->getRichObject();
  248. }
  249. }