1
0

DeletedShareAPIController.php 6.4 KB

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