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.

DeletedShareAPIController.php 9.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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 FilesSharingDeletedShare 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 FilesSharingDeletedShare
  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. $nodes = $userFolder->getById($share->getNodeId());
  107. if (empty($nodes)) {
  108. // fallback to guessing the path
  109. $node = $userFolder->get($share->getTarget());
  110. if ($node === null || $share->getTarget() === '') {
  111. throw new NotFoundException();
  112. }
  113. } else {
  114. $node = $nodes[0];
  115. }
  116. $result['path'] = $userFolder->getRelativePath($node->getPath());
  117. if ($node instanceof \OCP\Files\Folder) {
  118. $result['item_type'] = 'folder';
  119. } else {
  120. $result['item_type'] = 'file';
  121. }
  122. $result['mimetype'] = $node->getMimetype();
  123. $result['storage_id'] = $node->getStorage()->getId();
  124. $result['storage'] = $node->getStorage()->getCache()->getNumericStorageId();
  125. $result['item_source'] = $node->getId();
  126. $result['file_source'] = $node->getId();
  127. $result['file_parent'] = $node->getParent()->getId();
  128. $result['file_target'] = $share->getTarget();
  129. $result['item_size'] = $node->getSize();
  130. $result['item_mtime'] = $node->getMTime();
  131. $expiration = $share->getExpirationDate();
  132. if ($expiration !== null) {
  133. $result['expiration'] = $expiration->format('Y-m-d 00:00:00');
  134. }
  135. if ($share->getShareType() === IShare::TYPE_GROUP) {
  136. $group = $this->groupManager->get($share->getSharedWith());
  137. $result['share_with'] = $share->getSharedWith();
  138. $result['share_with_displayname'] = $group !== null ? $group->getDisplayName() : $share->getSharedWith();
  139. } elseif ($share->getShareType() === IShare::TYPE_ROOM) {
  140. $result['share_with'] = $share->getSharedWith();
  141. $result['share_with_displayname'] = '';
  142. try {
  143. $result = array_merge($result, $this->getRoomShareHelper()->formatShare($share));
  144. } catch (QueryException $e) {
  145. }
  146. } elseif ($share->getShareType() === IShare::TYPE_DECK) {
  147. $result['share_with'] = $share->getSharedWith();
  148. $result['share_with_displayname'] = '';
  149. try {
  150. $result = array_merge($result, $this->getDeckShareHelper()->formatShare($share));
  151. } catch (QueryException $e) {
  152. }
  153. } elseif ($share->getShareType() === IShare::TYPE_SCIENCEMESH) {
  154. $result['share_with'] = $share->getSharedWith();
  155. $result['share_with_displayname'] = '';
  156. try {
  157. $result = array_merge($result, $this->getSciencemeshShareHelper()->formatShare($share));
  158. } catch (QueryException $e) {
  159. }
  160. }
  161. return $result;
  162. }
  163. /**
  164. * @NoAdminRequired
  165. *
  166. * Get a list of all deleted shares
  167. *
  168. * @return DataResponse<Http::STATUS_OK, FilesSharingDeletedShare[], array{}>
  169. *
  170. * 200: Deleted shares returned
  171. */
  172. public function index(): DataResponse {
  173. $groupShares = $this->shareManager->getDeletedSharedWith($this->userId, IShare::TYPE_GROUP, null, -1, 0);
  174. $roomShares = $this->shareManager->getDeletedSharedWith($this->userId, IShare::TYPE_ROOM, null, -1, 0);
  175. $deckShares = $this->shareManager->getDeletedSharedWith($this->userId, IShare::TYPE_DECK, null, -1, 0);
  176. $sciencemeshShares = $this->shareManager->getDeletedSharedWith($this->userId, IShare::TYPE_SCIENCEMESH, null, -1, 0);
  177. $shares = array_merge($groupShares, $roomShares, $deckShares, $sciencemeshShares);
  178. $shares = array_map(function (IShare $share) {
  179. return $this->formatShare($share);
  180. }, $shares);
  181. return new DataResponse($shares);
  182. }
  183. /**
  184. * @NoAdminRequired
  185. *
  186. * Undelete a deleted share
  187. *
  188. * @param string $id ID of the share
  189. * @return DataResponse<Http::STATUS_OK, array<empty>, array{}>
  190. * @throws OCSException
  191. * @throws OCSNotFoundException Share not found
  192. *
  193. * 200: Share undeleted successfully
  194. */
  195. public function undelete(string $id): DataResponse {
  196. try {
  197. $share = $this->shareManager->getShareById($id, $this->userId);
  198. } catch (ShareNotFound $e) {
  199. throw new OCSNotFoundException('Share not found');
  200. }
  201. if ($share->getPermissions() !== 0) {
  202. throw new OCSNotFoundException('No deleted share found');
  203. }
  204. try {
  205. $this->shareManager->restoreShare($share, $this->userId);
  206. } catch (GenericShareException $e) {
  207. throw new OCSException('Something went wrong');
  208. }
  209. return new DataResponse([]);
  210. }
  211. /**
  212. * Returns the helper of DeletedShareAPIController for room shares.
  213. *
  214. * If the Talk application is not enabled or the helper is not available
  215. * a QueryException is thrown instead.
  216. *
  217. * @return \OCA\Talk\Share\Helper\DeletedShareAPIController
  218. * @throws QueryException
  219. */
  220. private function getRoomShareHelper() {
  221. if (!$this->appManager->isEnabledForUser('spreed')) {
  222. throw new QueryException();
  223. }
  224. return $this->serverContainer->get('\OCA\Talk\Share\Helper\DeletedShareAPIController');
  225. }
  226. /**
  227. * Returns the helper of DeletedShareAPIHelper for deck shares.
  228. *
  229. * If the Deck application is not enabled or the helper is not available
  230. * a QueryException is thrown instead.
  231. *
  232. * @return \OCA\Deck\Sharing\ShareAPIHelper
  233. * @throws QueryException
  234. */
  235. private function getDeckShareHelper() {
  236. if (!$this->appManager->isEnabledForUser('deck')) {
  237. throw new QueryException();
  238. }
  239. return $this->serverContainer->get('\OCA\Deck\Sharing\ShareAPIHelper');
  240. }
  241. /**
  242. * Returns the helper of DeletedShareAPIHelper for sciencemesh shares.
  243. *
  244. * If the sciencemesh application is not enabled or the helper is not available
  245. * a QueryException is thrown instead.
  246. *
  247. * @return \OCA\Deck\Sharing\ShareAPIHelper
  248. * @throws QueryException
  249. */
  250. private function getSciencemeshShareHelper() {
  251. if (!$this->appManager->isEnabledForUser('sciencemesh')) {
  252. throw new QueryException();
  253. }
  254. return $this->serverContainer->get('\OCA\ScienceMesh\Sharing\ShareAPIHelper');
  255. }
  256. }