CollaborationResourcesController.php 8.1 KB

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