You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CollaborationResourcesController.php 7.9KB

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