CollaborationResourcesController.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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 OCP\AppFramework\Http;
  24. use OCP\AppFramework\OCSController;
  25. use OCP\AppFramework\Http\DataResponse;
  26. use OCP\Collaboration\Resources\CollectionException;
  27. use OCP\Collaboration\Resources\ICollection;
  28. use OCP\Collaboration\Resources\IManager;
  29. use OCP\Collaboration\Resources\IResource;
  30. use OCP\Collaboration\Resources\ResourceException;
  31. use OCP\IRequest;
  32. use OCP\IUserSession;
  33. class CollaborationResourcesController extends OCSController {
  34. /** @var IManager */
  35. private $manager;
  36. /** @var IUserSession */
  37. private $userSession;
  38. public function __construct(
  39. string $appName,
  40. IRequest $request,
  41. IManager $manager,
  42. IUserSession $userSession
  43. ) {
  44. parent::__construct($appName, $request);
  45. $this->manager = $manager;
  46. $this->userSession = $userSession;
  47. }
  48. /**
  49. * @param int $collectionId
  50. * @return ICollection
  51. * @throws CollectionException when the collection was not found for the user
  52. */
  53. protected function getCollection(int $collectionId): ICollection {
  54. $collection = $this->manager->getCollectionForUser($collectionId, $this->userSession->getUser());
  55. if (!$collection->canAccess($this->userSession->getUser())) {
  56. throw new CollectionException('Not found');
  57. }
  58. return $collection;
  59. }
  60. /**
  61. * @NoAdminRequired
  62. *
  63. * @param int $collectionId
  64. * @return DataResponse
  65. */
  66. public function listCollection(int $collectionId): DataResponse {
  67. try {
  68. $collection = $this->getCollection($collectionId);
  69. } catch (CollectionException $e) {
  70. return new DataResponse([], Http::STATUS_NOT_FOUND);
  71. }
  72. return new DataResponse($this->prepareCollection($collection));
  73. }
  74. /**
  75. * @NoAdminRequired
  76. *
  77. * @param string $filter
  78. * @return DataResponse
  79. */
  80. public function searchCollections(string $filter): DataResponse {
  81. try {
  82. $collections = $this->manager->searchCollections($this->userSession->getUser(), $filter);
  83. } catch (CollectionException $e) {
  84. return new DataResponse([], Http::STATUS_NOT_FOUND);
  85. }
  86. return new DataResponse(array_map([$this, 'prepareCollection'], $collections));
  87. }
  88. /**
  89. * @NoAdminRequired
  90. *
  91. * @param int $collectionId
  92. * @param string $resourceType
  93. * @param string $resourceId
  94. * @return DataResponse
  95. */
  96. public function addResource(int $collectionId, string $resourceType, string $resourceId): DataResponse {
  97. try {
  98. $collection = $this->getCollection($collectionId);
  99. } catch (CollectionException $e) {
  100. return new DataResponse([], Http::STATUS_NOT_FOUND);
  101. }
  102. $resource = $this->manager->createResource($resourceType, $resourceId);
  103. if (!$resource->canAccess($this->userSession->getUser())) {
  104. return new DataResponse([], Http::STATUS_NOT_FOUND);
  105. }
  106. try {
  107. $collection->addResource($resource);
  108. } catch (ResourceException $e) {
  109. }
  110. return new DataResponse($this->prepareCollection($collection));
  111. }
  112. /**
  113. * @NoAdminRequired
  114. *
  115. * @param int $collectionId
  116. * @param string $resourceType
  117. * @param string $resourceId
  118. * @return DataResponse
  119. */
  120. public function removeResource(int $collectionId, string $resourceType, string $resourceId): DataResponse {
  121. try {
  122. $collection = $this->getCollection($collectionId);
  123. } catch (CollectionException $e) {
  124. return new DataResponse([], Http::STATUS_NOT_FOUND);
  125. }
  126. try {
  127. $resource = $this->manager->getResourceForUser($resourceType, $resourceId, $this->userSession->getUser());
  128. } catch (CollectionException $e) {
  129. return new DataResponse([], Http::STATUS_NOT_FOUND);
  130. }
  131. $collection->removeResource($resource);
  132. return new DataResponse($this->prepareCollection($collection));
  133. }
  134. /**
  135. * @NoAdminRequired
  136. *
  137. * @param string $resourceType
  138. * @param string $resourceId
  139. * @return DataResponse
  140. */
  141. public function getCollectionsByResource(string $resourceType, string $resourceId): DataResponse {
  142. try {
  143. $resource = $this->manager->getResourceForUser($resourceType, $resourceId, $this->userSession->getUser());
  144. } catch (ResourceException $e) {
  145. $resource = $this->manager->createResource($resourceType, $resourceId);
  146. }
  147. if (!$resource->canAccess($this->userSession->getUser())) {
  148. return new DataResponse([], Http::STATUS_NOT_FOUND);
  149. }
  150. return new DataResponse(array_map([$this, 'prepareCollection'], $resource->getCollections()));
  151. }
  152. /**
  153. * @NoAdminRequired
  154. *
  155. * @param string $baseResourceType
  156. * @param string $baseResourceId
  157. * @param string $name
  158. * @return DataResponse
  159. */
  160. public function createCollectionOnResource(string $baseResourceType, string $baseResourceId, string $name): DataResponse {
  161. if (!isset($name[0]) || isset($name[64])) {
  162. return new DataResponse([], Http::STATUS_BAD_REQUEST);
  163. }
  164. try {
  165. $resource = $this->manager->createResource($baseResourceType, $baseResourceId);
  166. } catch (CollectionException $e) {
  167. return new DataResponse([], Http::STATUS_NOT_FOUND);
  168. }
  169. if (!$resource->canAccess($this->userSession->getUser())) {
  170. return new DataResponse([], Http::STATUS_NOT_FOUND);
  171. }
  172. $collection = $this->manager->newCollection($name);
  173. $collection->addResource($resource);
  174. return new DataResponse($this->prepareCollection($collection));
  175. }
  176. /**
  177. * @NoAdminRequired
  178. *
  179. * @param int $collectionId
  180. * @param string $collectionName
  181. * @return DataResponse
  182. */
  183. public function renameCollection(int $collectionId, string $collectionName): DataResponse {
  184. try {
  185. $collection = $this->getCollection($collectionId);
  186. } catch (CollectionException $exception) {
  187. return new DataResponse([], Http::STATUS_NOT_FOUND);
  188. }
  189. $collection->setName($collectionName);
  190. return new DataResponse($this->prepareCollection($collection));
  191. }
  192. protected function prepareCollection(ICollection $collection): array {
  193. if (!$collection->canAccess($this->userSession->getUser())) {
  194. return null;
  195. }
  196. return [
  197. 'id' => $collection->getId(),
  198. 'name' => $collection->getName(),
  199. 'resources' => array_values(array_filter(array_map([$this, 'prepareResources'], $collection->getResources()))),
  200. ];
  201. }
  202. protected function prepareResources(IResource $resource): ?array {
  203. if (!$resource->canAccess($this->userSession->getUser())) {
  204. return null;
  205. }
  206. return $resource->getRichObject();
  207. }
  208. }