DeletedShareAPIController.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2016 Roeland Jago Douma <roeland@famdouma.nl>
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Daniel Calviño Sánchez <danxuliu@gmail.com>
  8. * @author Joas Schilling <coding@schilljs.com>
  9. * @author John Molakvoæ <skjnldsv@protonmail.com>
  10. * @author Julius Härtl <jus@bitgrid.net>
  11. * @author Roeland Jago Douma <roeland@famdouma.nl>
  12. * @author Kate Döen <kate.doeen@nextcloud.com>
  13. *
  14. * @license GNU AGPL version 3 or any later version
  15. *
  16. * This program is free software: you can redistribute it and/or modify
  17. * it under the terms of the GNU Affero General Public License as
  18. * published by the Free Software Foundation, either version 3 of the
  19. * License, or (at your option) any later version.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU Affero General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU Affero General Public License
  27. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  28. *
  29. */
  30. namespace OCA\Files_Sharing\Controller;
  31. use OCA\Files_Sharing\ResponseDefinitions;
  32. use OCP\App\IAppManager;
  33. use OCP\AppFramework\Http;
  34. use OCP\AppFramework\Http\DataResponse;
  35. use OCP\AppFramework\OCS\OCSException;
  36. use OCP\AppFramework\OCS\OCSNotFoundException;
  37. use OCP\AppFramework\OCSController;
  38. use OCP\AppFramework\QueryException;
  39. use OCP\Files\IRootFolder;
  40. use OCP\Files\NotFoundException;
  41. use OCP\IGroupManager;
  42. use OCP\IRequest;
  43. use OCP\IServerContainer;
  44. use OCP\IUserManager;
  45. use OCP\Share\Exceptions\GenericShareException;
  46. use OCP\Share\Exceptions\ShareNotFound;
  47. use OCP\Share\IManager as ShareManager;
  48. use OCP\Share\IShare;
  49. /**
  50. * @psalm-import-type Files_SharingDeletedShare from ResponseDefinitions
  51. */
  52. class DeletedShareAPIController extends OCSController {
  53. /** @var ShareManager */
  54. private $shareManager;
  55. /** @var string */
  56. private $userId;
  57. /** @var IUserManager */
  58. private $userManager;
  59. /** @var IGroupManager */
  60. private $groupManager;
  61. /** @var IRootFolder */
  62. private $rootFolder;
  63. /** @var IAppManager */
  64. private $appManager;
  65. /** @var IServerContainer */
  66. private $serverContainer;
  67. public function __construct(string $appName,
  68. IRequest $request,
  69. ShareManager $shareManager,
  70. string $UserId,
  71. IUserManager $userManager,
  72. IGroupManager $groupManager,
  73. IRootFolder $rootFolder,
  74. IAppManager $appManager,
  75. IServerContainer $serverContainer) {
  76. parent::__construct($appName, $request);
  77. $this->shareManager = $shareManager;
  78. $this->userId = $UserId;
  79. $this->userManager = $userManager;
  80. $this->groupManager = $groupManager;
  81. $this->rootFolder = $rootFolder;
  82. $this->appManager = $appManager;
  83. $this->serverContainer = $serverContainer;
  84. }
  85. /**
  86. * @suppress PhanUndeclaredClassMethod
  87. *
  88. * @return Files_SharingDeletedShare
  89. */
  90. private function formatShare(IShare $share): array {
  91. $result = [
  92. 'id' => $share->getFullId(),
  93. 'share_type' => $share->getShareType(),
  94. 'uid_owner' => $share->getSharedBy(),
  95. 'displayname_owner' => $this->userManager->get($share->getSharedBy())->getDisplayName(),
  96. 'permissions' => 0,
  97. 'stime' => $share->getShareTime()->getTimestamp(),
  98. 'parent' => null,
  99. 'expiration' => null,
  100. 'token' => null,
  101. 'uid_file_owner' => $share->getShareOwner(),
  102. 'displayname_file_owner' => $this->userManager->get($share->getShareOwner())->getDisplayName(),
  103. 'path' => $share->getTarget(),
  104. ];
  105. $userFolder = $this->rootFolder->getUserFolder($share->getSharedBy());
  106. $node = $userFolder->getFirstNodeById($share->getNodeId());
  107. if (!$node) {
  108. // fallback to guessing the path
  109. $node = $userFolder->get($share->getTarget());
  110. if ($node === null || $share->getTarget() === '') {
  111. throw new NotFoundException();
  112. }
  113. }
  114. $result['path'] = $userFolder->getRelativePath($node->getPath());
  115. if ($node instanceof \OCP\Files\Folder) {
  116. $result['item_type'] = 'folder';
  117. } else {
  118. $result['item_type'] = 'file';
  119. }
  120. $result['mimetype'] = $node->getMimetype();
  121. $result['storage_id'] = $node->getStorage()->getId();
  122. $result['storage'] = $node->getStorage()->getCache()->getNumericStorageId();
  123. $result['item_source'] = $node->getId();
  124. $result['file_source'] = $node->getId();
  125. $result['file_parent'] = $node->getParent()->getId();
  126. $result['file_target'] = $share->getTarget();
  127. $result['item_size'] = $node->getSize();
  128. $result['item_mtime'] = $node->getMTime();
  129. $expiration = $share->getExpirationDate();
  130. if ($expiration !== null) {
  131. $result['expiration'] = $expiration->format('Y-m-d 00:00:00');
  132. }
  133. if ($share->getShareType() === IShare::TYPE_GROUP) {
  134. $group = $this->groupManager->get($share->getSharedWith());
  135. $result['share_with'] = $share->getSharedWith();
  136. $result['share_with_displayname'] = $group !== null ? $group->getDisplayName() : $share->getSharedWith();
  137. } elseif ($share->getShareType() === IShare::TYPE_ROOM) {
  138. $result['share_with'] = $share->getSharedWith();
  139. $result['share_with_displayname'] = '';
  140. try {
  141. $result = array_merge($result, $this->getRoomShareHelper()->formatShare($share));
  142. } catch (QueryException $e) {
  143. }
  144. } elseif ($share->getShareType() === IShare::TYPE_DECK) {
  145. $result['share_with'] = $share->getSharedWith();
  146. $result['share_with_displayname'] = '';
  147. try {
  148. $result = array_merge($result, $this->getDeckShareHelper()->formatShare($share));
  149. } catch (QueryException $e) {
  150. }
  151. } elseif ($share->getShareType() === IShare::TYPE_SCIENCEMESH) {
  152. $result['share_with'] = $share->getSharedWith();
  153. $result['share_with_displayname'] = '';
  154. try {
  155. $result = array_merge($result, $this->getSciencemeshShareHelper()->formatShare($share));
  156. } catch (QueryException $e) {
  157. }
  158. }
  159. return $result;
  160. }
  161. /**
  162. * @NoAdminRequired
  163. *
  164. * Get a list of all deleted shares
  165. *
  166. * @return DataResponse<Http::STATUS_OK, Files_SharingDeletedShare[], array{}>
  167. *
  168. * 200: Deleted shares returned
  169. */
  170. public function index(): DataResponse {
  171. $groupShares = $this->shareManager->getDeletedSharedWith($this->userId, IShare::TYPE_GROUP, null, -1, 0);
  172. $roomShares = $this->shareManager->getDeletedSharedWith($this->userId, IShare::TYPE_ROOM, null, -1, 0);
  173. $deckShares = $this->shareManager->getDeletedSharedWith($this->userId, IShare::TYPE_DECK, null, -1, 0);
  174. $sciencemeshShares = $this->shareManager->getDeletedSharedWith($this->userId, IShare::TYPE_SCIENCEMESH, null, -1, 0);
  175. $shares = array_merge($groupShares, $roomShares, $deckShares, $sciencemeshShares);
  176. $shares = array_map(function (IShare $share) {
  177. return $this->formatShare($share);
  178. }, $shares);
  179. return new DataResponse($shares);
  180. }
  181. /**
  182. * @NoAdminRequired
  183. *
  184. * Undelete a deleted share
  185. *
  186. * @param string $id ID of the share
  187. * @return DataResponse<Http::STATUS_OK, array<empty>, array{}>
  188. * @throws OCSException
  189. * @throws OCSNotFoundException Share not found
  190. *
  191. * 200: Share undeleted successfully
  192. */
  193. public function undelete(string $id): DataResponse {
  194. try {
  195. $share = $this->shareManager->getShareById($id, $this->userId);
  196. } catch (ShareNotFound $e) {
  197. throw new OCSNotFoundException('Share not found');
  198. }
  199. if ($share->getPermissions() !== 0) {
  200. throw new OCSNotFoundException('No deleted share found');
  201. }
  202. try {
  203. $this->shareManager->restoreShare($share, $this->userId);
  204. } catch (GenericShareException $e) {
  205. throw new OCSException('Something went wrong');
  206. }
  207. return new DataResponse([]);
  208. }
  209. /**
  210. * Returns the helper of DeletedShareAPIController for room shares.
  211. *
  212. * If the Talk application is not enabled or the helper is not available
  213. * a QueryException is thrown instead.
  214. *
  215. * @return \OCA\Talk\Share\Helper\DeletedShareAPIController
  216. * @throws QueryException
  217. */
  218. private function getRoomShareHelper() {
  219. if (!$this->appManager->isEnabledForUser('spreed')) {
  220. throw new QueryException();
  221. }
  222. return $this->serverContainer->get('\OCA\Talk\Share\Helper\DeletedShareAPIController');
  223. }
  224. /**
  225. * Returns the helper of DeletedShareAPIHelper for deck shares.
  226. *
  227. * If the Deck application is not enabled or the helper is not available
  228. * a QueryException is thrown instead.
  229. *
  230. * @return \OCA\Deck\Sharing\ShareAPIHelper
  231. * @throws QueryException
  232. */
  233. private function getDeckShareHelper() {
  234. if (!$this->appManager->isEnabledForUser('deck')) {
  235. throw new QueryException();
  236. }
  237. return $this->serverContainer->get('\OCA\Deck\Sharing\ShareAPIHelper');
  238. }
  239. /**
  240. * Returns the helper of DeletedShareAPIHelper for sciencemesh shares.
  241. *
  242. * If the sciencemesh application is not enabled or the helper is not available
  243. * a QueryException is thrown instead.
  244. *
  245. * @return \OCA\Deck\Sharing\ShareAPIHelper
  246. * @throws QueryException
  247. */
  248. private function getSciencemeshShareHelper() {
  249. if (!$this->appManager->isEnabledForUser('sciencemesh')) {
  250. throw new QueryException();
  251. }
  252. return $this->serverContainer->get('\OCA\ScienceMesh\Sharing\ShareAPIHelper');
  253. }
  254. }