1
0

DeletedShareAPIController.php 8.3 KB

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