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