1
0

DeletedShareAPIController.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\Files_Sharing\Controller;
  8. use OCA\Files_Sharing\ResponseDefinitions;
  9. use OCP\App\IAppManager;
  10. use OCP\AppFramework\Http;
  11. use OCP\AppFramework\Http\Attribute\NoAdminRequired;
  12. use OCP\AppFramework\Http\DataResponse;
  13. use OCP\AppFramework\OCS\OCSException;
  14. use OCP\AppFramework\OCS\OCSNotFoundException;
  15. use OCP\AppFramework\OCSController;
  16. use OCP\AppFramework\QueryException;
  17. use OCP\Files\Folder;
  18. use OCP\Files\IRootFolder;
  19. use OCP\Files\NotFoundException;
  20. use OCP\IGroupManager;
  21. use OCP\IRequest;
  22. use OCP\IServerContainer;
  23. use OCP\IUserManager;
  24. use OCP\Share\Exceptions\GenericShareException;
  25. use OCP\Share\Exceptions\ShareNotFound;
  26. use OCP\Share\IManager as ShareManager;
  27. use OCP\Share\IShare;
  28. /**
  29. * @psalm-import-type Files_SharingDeletedShare from ResponseDefinitions
  30. */
  31. class DeletedShareAPIController extends OCSController {
  32. public function __construct(
  33. string $appName,
  34. IRequest $request,
  35. private ShareManager $shareManager,
  36. private string $userId,
  37. private IUserManager $userManager,
  38. private IGroupManager $groupManager,
  39. private IRootFolder $rootFolder,
  40. private IAppManager $appManager,
  41. private IServerContainer $serverContainer,
  42. ) {
  43. parent::__construct($appName, $request);
  44. }
  45. /**
  46. * @suppress PhanUndeclaredClassMethod
  47. *
  48. * @return Files_SharingDeletedShare
  49. */
  50. private function formatShare(IShare $share): array {
  51. $result = [
  52. 'id' => $share->getFullId(),
  53. 'share_type' => $share->getShareType(),
  54. 'uid_owner' => $share->getSharedBy(),
  55. 'displayname_owner' => $this->userManager->get($share->getSharedBy())->getDisplayName(),
  56. 'permissions' => 0,
  57. 'stime' => $share->getShareTime()->getTimestamp(),
  58. 'parent' => null,
  59. 'expiration' => null,
  60. 'token' => null,
  61. 'uid_file_owner' => $share->getShareOwner(),
  62. 'displayname_file_owner' => $this->userManager->get($share->getShareOwner())->getDisplayName(),
  63. 'path' => $share->getTarget(),
  64. ];
  65. $userFolder = $this->rootFolder->getUserFolder($share->getSharedBy());
  66. $node = $userFolder->getFirstNodeById($share->getNodeId());
  67. if (!$node) {
  68. // fallback to guessing the path
  69. $node = $userFolder->get($share->getTarget());
  70. if ($node === null || $share->getTarget() === '') {
  71. throw new NotFoundException();
  72. }
  73. }
  74. $result['path'] = $userFolder->getRelativePath($node->getPath());
  75. if ($node instanceof Folder) {
  76. $result['item_type'] = 'folder';
  77. } else {
  78. $result['item_type'] = 'file';
  79. }
  80. $result['mimetype'] = $node->getMimetype();
  81. $result['storage_id'] = $node->getStorage()->getId();
  82. $result['storage'] = $node->getStorage()->getCache()->getNumericStorageId();
  83. $result['item_source'] = $node->getId();
  84. $result['file_source'] = $node->getId();
  85. $result['file_parent'] = $node->getParent()->getId();
  86. $result['file_target'] = $share->getTarget();
  87. $result['item_size'] = $node->getSize();
  88. $result['item_mtime'] = $node->getMTime();
  89. $expiration = $share->getExpirationDate();
  90. if ($expiration !== null) {
  91. $result['expiration'] = $expiration->format('Y-m-d 00:00:00');
  92. }
  93. if ($share->getShareType() === IShare::TYPE_GROUP) {
  94. $group = $this->groupManager->get($share->getSharedWith());
  95. $result['share_with'] = $share->getSharedWith();
  96. $result['share_with_displayname'] = $group !== null ? $group->getDisplayName() : $share->getSharedWith();
  97. } elseif ($share->getShareType() === IShare::TYPE_ROOM) {
  98. $result['share_with'] = $share->getSharedWith();
  99. $result['share_with_displayname'] = '';
  100. try {
  101. $result = array_merge($result, $this->getRoomShareHelper()->formatShare($share));
  102. } catch (QueryException $e) {
  103. }
  104. } elseif ($share->getShareType() === IShare::TYPE_DECK) {
  105. $result['share_with'] = $share->getSharedWith();
  106. $result['share_with_displayname'] = '';
  107. try {
  108. $result = array_merge($result, $this->getDeckShareHelper()->formatShare($share));
  109. } catch (QueryException $e) {
  110. }
  111. } elseif ($share->getShareType() === IShare::TYPE_SCIENCEMESH) {
  112. $result['share_with'] = $share->getSharedWith();
  113. $result['share_with_displayname'] = '';
  114. try {
  115. $result = array_merge($result, $this->getSciencemeshShareHelper()->formatShare($share));
  116. } catch (QueryException $e) {
  117. }
  118. }
  119. return $result;
  120. }
  121. /**
  122. * Get a list of all deleted shares
  123. *
  124. * @return DataResponse<Http::STATUS_OK, list<Files_SharingDeletedShare>, array{}>
  125. *
  126. * 200: Deleted shares returned
  127. */
  128. #[NoAdminRequired]
  129. public function index(): DataResponse {
  130. $groupShares = $this->shareManager->getDeletedSharedWith($this->userId, IShare::TYPE_GROUP, null, -1, 0);
  131. $roomShares = $this->shareManager->getDeletedSharedWith($this->userId, IShare::TYPE_ROOM, null, -1, 0);
  132. $deckShares = $this->shareManager->getDeletedSharedWith($this->userId, IShare::TYPE_DECK, null, -1, 0);
  133. $sciencemeshShares = $this->shareManager->getDeletedSharedWith($this->userId, IShare::TYPE_SCIENCEMESH, null, -1, 0);
  134. $shares = array_merge($groupShares, $roomShares, $deckShares, $sciencemeshShares);
  135. $shares = array_values(array_map(function (IShare $share) {
  136. return $this->formatShare($share);
  137. }, $shares));
  138. return new DataResponse($shares);
  139. }
  140. /**
  141. * Undelete a deleted share
  142. *
  143. * @param string $id ID of the share
  144. * @return DataResponse<Http::STATUS_OK, list<empty>, array{}>
  145. * @throws OCSException
  146. * @throws OCSNotFoundException Share not found
  147. *
  148. * 200: Share undeleted successfully
  149. */
  150. #[NoAdminRequired]
  151. public function undelete(string $id): DataResponse {
  152. try {
  153. $share = $this->shareManager->getShareById($id, $this->userId);
  154. } catch (ShareNotFound $e) {
  155. throw new OCSNotFoundException('Share not found');
  156. }
  157. if ($share->getPermissions() !== 0) {
  158. throw new OCSNotFoundException('No deleted share found');
  159. }
  160. try {
  161. $this->shareManager->restoreShare($share, $this->userId);
  162. } catch (GenericShareException $e) {
  163. throw new OCSException('Something went wrong');
  164. }
  165. return new DataResponse([]);
  166. }
  167. /**
  168. * Returns the helper of DeletedShareAPIController for room shares.
  169. *
  170. * If the Talk application is not enabled or the helper is not available
  171. * a QueryException is thrown instead.
  172. *
  173. * @return \OCA\Talk\Share\Helper\DeletedShareAPIController
  174. * @throws QueryException
  175. */
  176. private function getRoomShareHelper() {
  177. if (!$this->appManager->isEnabledForUser('spreed')) {
  178. throw new QueryException();
  179. }
  180. return $this->serverContainer->get('\OCA\Talk\Share\Helper\DeletedShareAPIController');
  181. }
  182. /**
  183. * Returns the helper of DeletedShareAPIHelper for deck shares.
  184. *
  185. * If the Deck application is not enabled or the helper is not available
  186. * a QueryException is thrown instead.
  187. *
  188. * @return \OCA\Deck\Sharing\ShareAPIHelper
  189. * @throws QueryException
  190. */
  191. private function getDeckShareHelper() {
  192. if (!$this->appManager->isEnabledForUser('deck')) {
  193. throw new QueryException();
  194. }
  195. return $this->serverContainer->get('\OCA\Deck\Sharing\ShareAPIHelper');
  196. }
  197. /**
  198. * Returns the helper of DeletedShareAPIHelper for sciencemesh shares.
  199. *
  200. * If the sciencemesh application is not enabled or the helper is not available
  201. * a QueryException is thrown instead.
  202. *
  203. * @return \OCA\Deck\Sharing\ShareAPIHelper
  204. * @throws QueryException
  205. */
  206. private function getSciencemeshShareHelper() {
  207. if (!$this->appManager->isEnabledForUser('sciencemesh')) {
  208. throw new QueryException();
  209. }
  210. return $this->serverContainer->get('\OCA\ScienceMesh\Sharing\ShareAPIHelper');
  211. }
  212. }