@@ -25,36 +25,36 @@ |
||
25 | 25 | |
26 | 26 | class CloudFederationNotification implements ICloudFederationNotification { |
27 | 27 | |
28 | - private $message = []; |
|
28 | + private $message = []; |
|
29 | 29 | |
30 | - /** |
|
31 | - * add a message to the notification |
|
32 | - * |
|
33 | - * @param string $notificationType (e.g. SHARE_ACCEPTED) |
|
34 | - * @param string $resourceType (e.g. file, calendar, contact,...) |
|
35 | - * @param string $providerId id of the share |
|
36 | - * @param array $notification payload of the notification |
|
37 | - * |
|
38 | - * @since 14.0.0 |
|
39 | - */ |
|
40 | - public function setMessage($notificationType, $resourceType, $providerId, array $notification) { |
|
41 | - $this->message = [ |
|
42 | - 'notificationType' => $notificationType, |
|
43 | - 'resourceType' => $resourceType, |
|
44 | - 'providerId' => $providerId, |
|
45 | - 'notification' => $notification, |
|
46 | - ]; |
|
30 | + /** |
|
31 | + * add a message to the notification |
|
32 | + * |
|
33 | + * @param string $notificationType (e.g. SHARE_ACCEPTED) |
|
34 | + * @param string $resourceType (e.g. file, calendar, contact,...) |
|
35 | + * @param string $providerId id of the share |
|
36 | + * @param array $notification payload of the notification |
|
37 | + * |
|
38 | + * @since 14.0.0 |
|
39 | + */ |
|
40 | + public function setMessage($notificationType, $resourceType, $providerId, array $notification) { |
|
41 | + $this->message = [ |
|
42 | + 'notificationType' => $notificationType, |
|
43 | + 'resourceType' => $resourceType, |
|
44 | + 'providerId' => $providerId, |
|
45 | + 'notification' => $notification, |
|
46 | + ]; |
|
47 | 47 | |
48 | - } |
|
48 | + } |
|
49 | 49 | |
50 | - /** |
|
51 | - * get message, ready to send out |
|
52 | - * |
|
53 | - * @return array |
|
54 | - * |
|
55 | - * @since 14.0.0 |
|
56 | - */ |
|
57 | - public function getMessage() { |
|
58 | - return $this->message; |
|
59 | - } |
|
50 | + /** |
|
51 | + * get message, ready to send out |
|
52 | + * |
|
53 | + * @return array |
|
54 | + * |
|
55 | + * @since 14.0.0 |
|
56 | + */ |
|
57 | + public function getMessage() { |
|
58 | + return $this->message; |
|
59 | + } |
|
60 | 60 | } |
@@ -27,37 +27,37 @@ |
||
27 | 27 | |
28 | 28 | class CloudFederationFactory implements ICloudFederationFactory { |
29 | 29 | |
30 | - /** |
|
31 | - * get a CloudFederationShare Object to prepare a share you want to send |
|
32 | - * |
|
33 | - * @param string $shareWith |
|
34 | - * @param string $name resource name (e.g. document.odt) |
|
35 | - * @param string $description share description (optional) |
|
36 | - * @param string $providerId resource UID on the provider side |
|
37 | - * @param string $owner provider specific UID of the user who owns the resource |
|
38 | - * @param string $ownerDisplayName display name of the user who shared the item |
|
39 | - * @param string $sharedBy provider specific UID of the user who shared the resource |
|
40 | - * @param string $sharedByDisplayName display name of the user who shared the resource |
|
41 | - * @param string $sharedSecret used to authenticate requests across servers |
|
42 | - * @param string $shareType ('group' or 'user' share) |
|
43 | - * @param $resourceType ('file', 'calendar',...) |
|
44 | - * @return ICloudFederationShare |
|
45 | - * |
|
46 | - * @since 14.0.0 |
|
47 | - */ |
|
48 | - public function getCloudFederationShare($shareWith, $name, $description, $providerId, $owner, $ownerDisplayName, $sharedBy, $sharedByDisplayName, $sharedSecret, $shareType, $resourceType) { |
|
49 | - return new CloudFederationShare($shareWith, $name, $description, $providerId, $owner, $ownerDisplayName, $sharedBy, $sharedByDisplayName, $shareType, $resourceType, $sharedSecret); |
|
50 | - } |
|
30 | + /** |
|
31 | + * get a CloudFederationShare Object to prepare a share you want to send |
|
32 | + * |
|
33 | + * @param string $shareWith |
|
34 | + * @param string $name resource name (e.g. document.odt) |
|
35 | + * @param string $description share description (optional) |
|
36 | + * @param string $providerId resource UID on the provider side |
|
37 | + * @param string $owner provider specific UID of the user who owns the resource |
|
38 | + * @param string $ownerDisplayName display name of the user who shared the item |
|
39 | + * @param string $sharedBy provider specific UID of the user who shared the resource |
|
40 | + * @param string $sharedByDisplayName display name of the user who shared the resource |
|
41 | + * @param string $sharedSecret used to authenticate requests across servers |
|
42 | + * @param string $shareType ('group' or 'user' share) |
|
43 | + * @param $resourceType ('file', 'calendar',...) |
|
44 | + * @return ICloudFederationShare |
|
45 | + * |
|
46 | + * @since 14.0.0 |
|
47 | + */ |
|
48 | + public function getCloudFederationShare($shareWith, $name, $description, $providerId, $owner, $ownerDisplayName, $sharedBy, $sharedByDisplayName, $sharedSecret, $shareType, $resourceType) { |
|
49 | + return new CloudFederationShare($shareWith, $name, $description, $providerId, $owner, $ownerDisplayName, $sharedBy, $sharedByDisplayName, $shareType, $resourceType, $sharedSecret); |
|
50 | + } |
|
51 | 51 | |
52 | - /** |
|
53 | - * get a Cloud FederationNotification object to prepare a notification you |
|
54 | - * want to send |
|
55 | - * |
|
56 | - * @return ICloudFederationNotification |
|
57 | - * |
|
58 | - * @since 14.0.0 |
|
59 | - */ |
|
60 | - public function getCloudFederationNotification() { |
|
61 | - return new CloudFederationNotification(); |
|
62 | - } |
|
52 | + /** |
|
53 | + * get a Cloud FederationNotification object to prepare a notification you |
|
54 | + * want to send |
|
55 | + * |
|
56 | + * @return ICloudFederationNotification |
|
57 | + * |
|
58 | + * @since 14.0.0 |
|
59 | + */ |
|
60 | + public function getCloudFederationNotification() { |
|
61 | + return new CloudFederationNotification(); |
|
62 | + } |
|
63 | 63 | } |
@@ -33,358 +33,358 @@ |
||
33 | 33 | use OCP\OCS\IDiscoveryService; |
34 | 34 | |
35 | 35 | class Notifications { |
36 | - const RESPONSE_FORMAT = 'json'; // default response format for ocs calls |
|
37 | - |
|
38 | - /** @var AddressHandler */ |
|
39 | - private $addressHandler; |
|
40 | - |
|
41 | - /** @var IClientService */ |
|
42 | - private $httpClientService; |
|
43 | - |
|
44 | - /** @var IDiscoveryService */ |
|
45 | - private $discoveryService; |
|
46 | - |
|
47 | - /** @var IJobList */ |
|
48 | - private $jobList; |
|
49 | - |
|
50 | - /** @var ICloudFederationProviderManager */ |
|
51 | - private $federationProviderManager; |
|
52 | - |
|
53 | - /** @var ICloudFederationFactory */ |
|
54 | - private $cloudFederationFactory; |
|
55 | - |
|
56 | - /** |
|
57 | - * @param AddressHandler $addressHandler |
|
58 | - * @param IClientService $httpClientService |
|
59 | - * @param IDiscoveryService $discoveryService |
|
60 | - * @param IJobList $jobList |
|
61 | - * @param ICloudFederationProviderManager $federationProviderManager |
|
62 | - * @param ICloudFederationFactory $cloudFederationFactory |
|
63 | - */ |
|
64 | - public function __construct( |
|
65 | - AddressHandler $addressHandler, |
|
66 | - IClientService $httpClientService, |
|
67 | - IDiscoveryService $discoveryService, |
|
68 | - IJobList $jobList, |
|
69 | - ICloudFederationProviderManager $federationProviderManager, |
|
70 | - ICloudFederationFactory $cloudFederationFactory |
|
71 | - ) { |
|
72 | - $this->addressHandler = $addressHandler; |
|
73 | - $this->httpClientService = $httpClientService; |
|
74 | - $this->discoveryService = $discoveryService; |
|
75 | - $this->jobList = $jobList; |
|
76 | - $this->federationProviderManager = $federationProviderManager; |
|
77 | - $this->cloudFederationFactory = $cloudFederationFactory; |
|
78 | - } |
|
79 | - |
|
80 | - /** |
|
81 | - * send server-to-server share to remote server |
|
82 | - * |
|
83 | - * @param string $token |
|
84 | - * @param string $shareWith |
|
85 | - * @param string $name |
|
86 | - * @param int $remote_id |
|
87 | - * @param string $owner |
|
88 | - * @param string $ownerFederatedId |
|
89 | - * @param string $sharedBy |
|
90 | - * @param string $sharedByFederatedId |
|
91 | - * @return bool |
|
92 | - * @throws \OC\HintException |
|
93 | - * @throws \OC\ServerNotAvailableException |
|
94 | - */ |
|
95 | - public function sendRemoteShare($token, $shareWith, $name, $remote_id, $owner, $ownerFederatedId, $sharedBy, $sharedByFederatedId) { |
|
96 | - |
|
97 | - list($user, $remote) = $this->addressHandler->splitUserRemote($shareWith); |
|
98 | - |
|
99 | - if ($user && $remote) { |
|
100 | - $local = $this->addressHandler->generateRemoteURL(); |
|
101 | - |
|
102 | - $fields = array( |
|
103 | - 'shareWith' => $user, |
|
104 | - 'token' => $token, |
|
105 | - 'name' => $name, |
|
106 | - 'remoteId' => $remote_id, |
|
107 | - 'owner' => $owner, |
|
108 | - 'ownerFederatedId' => $ownerFederatedId, |
|
109 | - 'sharedBy' => $sharedBy, |
|
110 | - 'sharedByFederatedId' => $sharedByFederatedId, |
|
111 | - 'remote' => $local, |
|
112 | - ); |
|
113 | - |
|
114 | - $result = $this->tryHttpPostToShareEndpoint($remote, '', $fields); |
|
115 | - $status = json_decode($result['result'], true); |
|
116 | - |
|
117 | - $ocsStatus = isset($status['ocs']); |
|
118 | - $ocsSuccess = $ocsStatus && ($status['ocs']['meta']['statuscode'] === 100 || $status['ocs']['meta']['statuscode'] === 200); |
|
119 | - |
|
120 | - if ($result['success'] && (!$ocsStatus ||$ocsSuccess)) { |
|
121 | - \OC_Hook::emit('OCP\Share', 'federated_share_added', ['server' => $remote]); |
|
122 | - return true; |
|
123 | - } |
|
124 | - |
|
125 | - } |
|
126 | - |
|
127 | - return false; |
|
128 | - } |
|
129 | - |
|
130 | - /** |
|
131 | - * ask owner to re-share the file with the given user |
|
132 | - * |
|
133 | - * @param string $token |
|
134 | - * @param int $id remote Id |
|
135 | - * @param int $shareId internal share Id |
|
136 | - * @param string $remote remote address of the owner |
|
137 | - * @param string $shareWith |
|
138 | - * @param int $permission |
|
139 | - * @return bool |
|
140 | - * @throws \OC\HintException |
|
141 | - * @throws \OC\ServerNotAvailableException |
|
142 | - */ |
|
143 | - public function requestReShare($token, $id, $shareId, $remote, $shareWith, $permission) { |
|
144 | - |
|
145 | - $fields = array( |
|
146 | - 'shareWith' => $shareWith, |
|
147 | - 'token' => $token, |
|
148 | - 'permission' => $permission, |
|
149 | - 'remoteId' => $shareId |
|
150 | - ); |
|
151 | - |
|
152 | - $result = $this->tryHttpPostToShareEndpoint(rtrim($remote, '/'), '/' . $id . '/reshare', $fields); |
|
153 | - $status = json_decode($result['result'], true); |
|
154 | - |
|
155 | - $httpRequestSuccessful = $result['success']; |
|
156 | - $ocsCallSuccessful = $status['ocs']['meta']['statuscode'] === 100 || $status['ocs']['meta']['statuscode'] === 200; |
|
157 | - $validToken = isset($status['ocs']['data']['token']) && is_string($status['ocs']['data']['token']); |
|
158 | - $validRemoteId = isset($status['ocs']['data']['remoteId']); |
|
159 | - |
|
160 | - if ($httpRequestSuccessful && $ocsCallSuccessful && $validToken && $validRemoteId) { |
|
161 | - return [ |
|
162 | - $status['ocs']['data']['token'], |
|
163 | - (int)$status['ocs']['data']['remoteId'] |
|
164 | - ]; |
|
165 | - } |
|
166 | - |
|
167 | - return false; |
|
168 | - } |
|
169 | - |
|
170 | - /** |
|
171 | - * send server-to-server unshare to remote server |
|
172 | - * |
|
173 | - * @param string $remote url |
|
174 | - * @param int $id share id |
|
175 | - * @param string $token |
|
176 | - * @return bool |
|
177 | - */ |
|
178 | - public function sendRemoteUnShare($remote, $id, $token) { |
|
179 | - $this->sendUpdateToRemote($remote, $id, $token, 'unshare'); |
|
180 | - } |
|
181 | - |
|
182 | - /** |
|
183 | - * send server-to-server unshare to remote server |
|
184 | - * |
|
185 | - * @param string $remote url |
|
186 | - * @param int $id share id |
|
187 | - * @param string $token |
|
188 | - * @return bool |
|
189 | - */ |
|
190 | - public function sendRevokeShare($remote, $id, $token) { |
|
191 | - $this->sendUpdateToRemote($remote, $id, $token, 'revoke'); |
|
192 | - } |
|
193 | - |
|
194 | - /** |
|
195 | - * send notification to remote server if the permissions was changed |
|
196 | - * |
|
197 | - * @param string $remote |
|
198 | - * @param int $remoteId |
|
199 | - * @param string $token |
|
200 | - * @param int $permissions |
|
201 | - * @return bool |
|
202 | - */ |
|
203 | - public function sendPermissionChange($remote, $remoteId, $token, $permissions) { |
|
204 | - $this->sendUpdateToRemote($remote, $remoteId, $token, 'permissions', ['permissions' => $permissions]); |
|
205 | - } |
|
206 | - |
|
207 | - /** |
|
208 | - * forward accept reShare to remote server |
|
209 | - * |
|
210 | - * @param string $remote |
|
211 | - * @param int $remoteId |
|
212 | - * @param string $token |
|
213 | - */ |
|
214 | - public function sendAcceptShare($remote, $remoteId, $token) { |
|
215 | - $this->sendUpdateToRemote($remote, $remoteId, $token, 'accept'); |
|
216 | - } |
|
217 | - |
|
218 | - /** |
|
219 | - * forward decline reShare to remote server |
|
220 | - * |
|
221 | - * @param string $remote |
|
222 | - * @param int $remoteId |
|
223 | - * @param string $token |
|
224 | - */ |
|
225 | - public function sendDeclineShare($remote, $remoteId, $token) { |
|
226 | - $this->sendUpdateToRemote($remote, $remoteId, $token, 'decline'); |
|
227 | - } |
|
228 | - |
|
229 | - /** |
|
230 | - * inform remote server whether server-to-server share was accepted/declined |
|
231 | - * |
|
232 | - * @param string $remote |
|
233 | - * @param string $token |
|
234 | - * @param int $remoteId Share id on the remote host |
|
235 | - * @param string $action possible actions: accept, decline, unshare, revoke, permissions |
|
236 | - * @param array $data |
|
237 | - * @param int $try |
|
238 | - * @return boolean |
|
239 | - */ |
|
240 | - public function sendUpdateToRemote($remote, $remoteId, $token, $action, $data = [], $try = 0) { |
|
241 | - |
|
242 | - $fields = array('token' => $token); |
|
243 | - foreach ($data as $key => $value) { |
|
244 | - $fields[$key] = $value; |
|
245 | - } |
|
246 | - |
|
247 | - $result = $this->tryHttpPostToShareEndpoint(rtrim($remote, '/'), '/' . $remoteId . '/' . $action, $fields); |
|
248 | - $status = json_decode($result['result'], true); |
|
249 | - |
|
250 | - if ($result['success'] && |
|
251 | - ($status['ocs']['meta']['statuscode'] === 100 || |
|
252 | - $status['ocs']['meta']['statuscode'] === 200 |
|
253 | - ) |
|
254 | - ) { |
|
255 | - return true; |
|
256 | - } elseif ($try === 0) { |
|
257 | - // only add new job on first try |
|
258 | - $this->jobList->add('OCA\FederatedFileSharing\BackgroundJob\RetryJob', |
|
259 | - [ |
|
260 | - 'remote' => $remote, |
|
261 | - 'remoteId' => $remoteId, |
|
262 | - 'token' => $token, |
|
263 | - 'action' => $action, |
|
264 | - 'data' => json_encode($data), |
|
265 | - 'try' => $try, |
|
266 | - 'lastRun' => $this->getTimestamp() |
|
267 | - ] |
|
268 | - ); |
|
269 | - } |
|
270 | - |
|
271 | - return false; |
|
272 | - } |
|
273 | - |
|
274 | - |
|
275 | - /** |
|
276 | - * return current timestamp |
|
277 | - * |
|
278 | - * @return int |
|
279 | - */ |
|
280 | - protected function getTimestamp() { |
|
281 | - return time(); |
|
282 | - } |
|
283 | - |
|
284 | - /** |
|
285 | - * try http post with the given protocol, if no protocol is given we pick |
|
286 | - * the secure one (https) |
|
287 | - * |
|
288 | - * @param string $remoteDomain |
|
289 | - * @param string $urlSuffix |
|
290 | - * @param array $fields post parameters |
|
291 | - * @param string $action define the action (possible values: share, reshare, accept, decline, unshare, revoke, permissions) |
|
292 | - * @return array |
|
293 | - * @throws \Exception |
|
294 | - */ |
|
295 | - protected function tryHttpPostToShareEndpoint($remoteDomain, $urlSuffix, array $fields, $action="share") { |
|
296 | - |
|
297 | - if ($this->addressHandler->urlContainProtocol($remoteDomain) === false) { |
|
298 | - $remoteDomain = 'https://' . $remoteDomain; |
|
299 | - } |
|
300 | - |
|
301 | - $result = [ |
|
302 | - 'success' => false, |
|
303 | - 'result' => '', |
|
304 | - ]; |
|
305 | - |
|
306 | - // if possible we use the new OCM API |
|
307 | - $ocmResult = $this->tryOCMEndPoint($remoteDomain, $fields, $action); |
|
308 | - if ($ocmResult) { |
|
309 | - $result['success'] = true; |
|
310 | - return $result; |
|
311 | - } |
|
312 | - |
|
313 | - // Fall back to old API |
|
314 | - $client = $this->httpClientService->newClient(); |
|
315 | - $federationEndpoints = $this->discoveryService->discover($remoteDomain, 'FEDERATED_SHARING'); |
|
316 | - $endpoint = isset($federationEndpoints['share']) ? $federationEndpoints['share'] : '/ocs/v2.php/cloud/shares'; |
|
317 | - try { |
|
318 | - $response = $client->post($remoteDomain . $endpoint . $urlSuffix . '?format=' . self::RESPONSE_FORMAT, [ |
|
319 | - 'body' => $fields, |
|
320 | - 'timeout' => 10, |
|
321 | - 'connect_timeout' => 10, |
|
322 | - ]); |
|
323 | - $result['result'] = $response->getBody(); |
|
324 | - $result['success'] = true; |
|
325 | - } catch (\Exception $e) { |
|
326 | - // if flat re-sharing is not supported by the remote server |
|
327 | - // we re-throw the exception and fall back to the old behaviour. |
|
328 | - // (flat re-shares has been introduced in Nextcloud 9.1) |
|
329 | - if ($e->getCode() === Http::STATUS_INTERNAL_SERVER_ERROR) { |
|
330 | - throw $e; |
|
331 | - } |
|
332 | - } |
|
333 | - |
|
334 | - return $result; |
|
335 | - } |
|
336 | - |
|
337 | - /** |
|
338 | - * check if server supports the new OCM api and ask for the correct end-point |
|
339 | - * |
|
340 | - * @param string $url |
|
341 | - * @return string |
|
342 | - */ |
|
343 | - protected function getOCMEndPoint($url) { |
|
344 | - $client = $this->httpClientService->newClient(); |
|
345 | - try { |
|
346 | - $response = $client->get($url, ['timeout' => 10, 'connect_timeout' => 10]); |
|
347 | - } catch (\Exception $e) { |
|
348 | - return ''; |
|
349 | - } |
|
350 | - |
|
351 | - $result = $response->getBody(); |
|
352 | - $result = json_decode($result, true); |
|
353 | - |
|
354 | - if (isset($result['end-point'])) { |
|
355 | - return $result['end-point']; |
|
356 | - } |
|
357 | - |
|
358 | - return ''; |
|
359 | - } |
|
360 | - |
|
361 | - /** |
|
362 | - * send action regarding federated sharing to the remote server using the OCM API |
|
363 | - * |
|
364 | - * @param $remoteDomain |
|
365 | - * @param $fields |
|
366 | - * @param $action |
|
367 | - * |
|
368 | - * @return bool |
|
369 | - */ |
|
370 | - protected function tryOCMEndPoint($remoteDomain, $fields, $action) { |
|
371 | - switch ($action) { |
|
372 | - case 'share': |
|
373 | - $share = $this->cloudFederationFactory->getCloudFederationShare( |
|
374 | - $fields['shareWith'] . '@' . $remoteDomain, |
|
375 | - $fields['name'], |
|
376 | - '', |
|
377 | - $fields['remoteId'], |
|
378 | - $fields['ownerFederatedId'], |
|
379 | - $fields['owner'], |
|
380 | - $fields['sharedByFederatedId'], |
|
381 | - $fields['sharedBy'], |
|
382 | - $fields['token'], |
|
383 | - 'user', |
|
384 | - 'file' |
|
385 | - ); |
|
386 | - return $this->federationProviderManager->sendShare($share); |
|
387 | - } |
|
388 | - |
|
389 | - } |
|
36 | + const RESPONSE_FORMAT = 'json'; // default response format for ocs calls |
|
37 | + |
|
38 | + /** @var AddressHandler */ |
|
39 | + private $addressHandler; |
|
40 | + |
|
41 | + /** @var IClientService */ |
|
42 | + private $httpClientService; |
|
43 | + |
|
44 | + /** @var IDiscoveryService */ |
|
45 | + private $discoveryService; |
|
46 | + |
|
47 | + /** @var IJobList */ |
|
48 | + private $jobList; |
|
49 | + |
|
50 | + /** @var ICloudFederationProviderManager */ |
|
51 | + private $federationProviderManager; |
|
52 | + |
|
53 | + /** @var ICloudFederationFactory */ |
|
54 | + private $cloudFederationFactory; |
|
55 | + |
|
56 | + /** |
|
57 | + * @param AddressHandler $addressHandler |
|
58 | + * @param IClientService $httpClientService |
|
59 | + * @param IDiscoveryService $discoveryService |
|
60 | + * @param IJobList $jobList |
|
61 | + * @param ICloudFederationProviderManager $federationProviderManager |
|
62 | + * @param ICloudFederationFactory $cloudFederationFactory |
|
63 | + */ |
|
64 | + public function __construct( |
|
65 | + AddressHandler $addressHandler, |
|
66 | + IClientService $httpClientService, |
|
67 | + IDiscoveryService $discoveryService, |
|
68 | + IJobList $jobList, |
|
69 | + ICloudFederationProviderManager $federationProviderManager, |
|
70 | + ICloudFederationFactory $cloudFederationFactory |
|
71 | + ) { |
|
72 | + $this->addressHandler = $addressHandler; |
|
73 | + $this->httpClientService = $httpClientService; |
|
74 | + $this->discoveryService = $discoveryService; |
|
75 | + $this->jobList = $jobList; |
|
76 | + $this->federationProviderManager = $federationProviderManager; |
|
77 | + $this->cloudFederationFactory = $cloudFederationFactory; |
|
78 | + } |
|
79 | + |
|
80 | + /** |
|
81 | + * send server-to-server share to remote server |
|
82 | + * |
|
83 | + * @param string $token |
|
84 | + * @param string $shareWith |
|
85 | + * @param string $name |
|
86 | + * @param int $remote_id |
|
87 | + * @param string $owner |
|
88 | + * @param string $ownerFederatedId |
|
89 | + * @param string $sharedBy |
|
90 | + * @param string $sharedByFederatedId |
|
91 | + * @return bool |
|
92 | + * @throws \OC\HintException |
|
93 | + * @throws \OC\ServerNotAvailableException |
|
94 | + */ |
|
95 | + public function sendRemoteShare($token, $shareWith, $name, $remote_id, $owner, $ownerFederatedId, $sharedBy, $sharedByFederatedId) { |
|
96 | + |
|
97 | + list($user, $remote) = $this->addressHandler->splitUserRemote($shareWith); |
|
98 | + |
|
99 | + if ($user && $remote) { |
|
100 | + $local = $this->addressHandler->generateRemoteURL(); |
|
101 | + |
|
102 | + $fields = array( |
|
103 | + 'shareWith' => $user, |
|
104 | + 'token' => $token, |
|
105 | + 'name' => $name, |
|
106 | + 'remoteId' => $remote_id, |
|
107 | + 'owner' => $owner, |
|
108 | + 'ownerFederatedId' => $ownerFederatedId, |
|
109 | + 'sharedBy' => $sharedBy, |
|
110 | + 'sharedByFederatedId' => $sharedByFederatedId, |
|
111 | + 'remote' => $local, |
|
112 | + ); |
|
113 | + |
|
114 | + $result = $this->tryHttpPostToShareEndpoint($remote, '', $fields); |
|
115 | + $status = json_decode($result['result'], true); |
|
116 | + |
|
117 | + $ocsStatus = isset($status['ocs']); |
|
118 | + $ocsSuccess = $ocsStatus && ($status['ocs']['meta']['statuscode'] === 100 || $status['ocs']['meta']['statuscode'] === 200); |
|
119 | + |
|
120 | + if ($result['success'] && (!$ocsStatus ||$ocsSuccess)) { |
|
121 | + \OC_Hook::emit('OCP\Share', 'federated_share_added', ['server' => $remote]); |
|
122 | + return true; |
|
123 | + } |
|
124 | + |
|
125 | + } |
|
126 | + |
|
127 | + return false; |
|
128 | + } |
|
129 | + |
|
130 | + /** |
|
131 | + * ask owner to re-share the file with the given user |
|
132 | + * |
|
133 | + * @param string $token |
|
134 | + * @param int $id remote Id |
|
135 | + * @param int $shareId internal share Id |
|
136 | + * @param string $remote remote address of the owner |
|
137 | + * @param string $shareWith |
|
138 | + * @param int $permission |
|
139 | + * @return bool |
|
140 | + * @throws \OC\HintException |
|
141 | + * @throws \OC\ServerNotAvailableException |
|
142 | + */ |
|
143 | + public function requestReShare($token, $id, $shareId, $remote, $shareWith, $permission) { |
|
144 | + |
|
145 | + $fields = array( |
|
146 | + 'shareWith' => $shareWith, |
|
147 | + 'token' => $token, |
|
148 | + 'permission' => $permission, |
|
149 | + 'remoteId' => $shareId |
|
150 | + ); |
|
151 | + |
|
152 | + $result = $this->tryHttpPostToShareEndpoint(rtrim($remote, '/'), '/' . $id . '/reshare', $fields); |
|
153 | + $status = json_decode($result['result'], true); |
|
154 | + |
|
155 | + $httpRequestSuccessful = $result['success']; |
|
156 | + $ocsCallSuccessful = $status['ocs']['meta']['statuscode'] === 100 || $status['ocs']['meta']['statuscode'] === 200; |
|
157 | + $validToken = isset($status['ocs']['data']['token']) && is_string($status['ocs']['data']['token']); |
|
158 | + $validRemoteId = isset($status['ocs']['data']['remoteId']); |
|
159 | + |
|
160 | + if ($httpRequestSuccessful && $ocsCallSuccessful && $validToken && $validRemoteId) { |
|
161 | + return [ |
|
162 | + $status['ocs']['data']['token'], |
|
163 | + (int)$status['ocs']['data']['remoteId'] |
|
164 | + ]; |
|
165 | + } |
|
166 | + |
|
167 | + return false; |
|
168 | + } |
|
169 | + |
|
170 | + /** |
|
171 | + * send server-to-server unshare to remote server |
|
172 | + * |
|
173 | + * @param string $remote url |
|
174 | + * @param int $id share id |
|
175 | + * @param string $token |
|
176 | + * @return bool |
|
177 | + */ |
|
178 | + public function sendRemoteUnShare($remote, $id, $token) { |
|
179 | + $this->sendUpdateToRemote($remote, $id, $token, 'unshare'); |
|
180 | + } |
|
181 | + |
|
182 | + /** |
|
183 | + * send server-to-server unshare to remote server |
|
184 | + * |
|
185 | + * @param string $remote url |
|
186 | + * @param int $id share id |
|
187 | + * @param string $token |
|
188 | + * @return bool |
|
189 | + */ |
|
190 | + public function sendRevokeShare($remote, $id, $token) { |
|
191 | + $this->sendUpdateToRemote($remote, $id, $token, 'revoke'); |
|
192 | + } |
|
193 | + |
|
194 | + /** |
|
195 | + * send notification to remote server if the permissions was changed |
|
196 | + * |
|
197 | + * @param string $remote |
|
198 | + * @param int $remoteId |
|
199 | + * @param string $token |
|
200 | + * @param int $permissions |
|
201 | + * @return bool |
|
202 | + */ |
|
203 | + public function sendPermissionChange($remote, $remoteId, $token, $permissions) { |
|
204 | + $this->sendUpdateToRemote($remote, $remoteId, $token, 'permissions', ['permissions' => $permissions]); |
|
205 | + } |
|
206 | + |
|
207 | + /** |
|
208 | + * forward accept reShare to remote server |
|
209 | + * |
|
210 | + * @param string $remote |
|
211 | + * @param int $remoteId |
|
212 | + * @param string $token |
|
213 | + */ |
|
214 | + public function sendAcceptShare($remote, $remoteId, $token) { |
|
215 | + $this->sendUpdateToRemote($remote, $remoteId, $token, 'accept'); |
|
216 | + } |
|
217 | + |
|
218 | + /** |
|
219 | + * forward decline reShare to remote server |
|
220 | + * |
|
221 | + * @param string $remote |
|
222 | + * @param int $remoteId |
|
223 | + * @param string $token |
|
224 | + */ |
|
225 | + public function sendDeclineShare($remote, $remoteId, $token) { |
|
226 | + $this->sendUpdateToRemote($remote, $remoteId, $token, 'decline'); |
|
227 | + } |
|
228 | + |
|
229 | + /** |
|
230 | + * inform remote server whether server-to-server share was accepted/declined |
|
231 | + * |
|
232 | + * @param string $remote |
|
233 | + * @param string $token |
|
234 | + * @param int $remoteId Share id on the remote host |
|
235 | + * @param string $action possible actions: accept, decline, unshare, revoke, permissions |
|
236 | + * @param array $data |
|
237 | + * @param int $try |
|
238 | + * @return boolean |
|
239 | + */ |
|
240 | + public function sendUpdateToRemote($remote, $remoteId, $token, $action, $data = [], $try = 0) { |
|
241 | + |
|
242 | + $fields = array('token' => $token); |
|
243 | + foreach ($data as $key => $value) { |
|
244 | + $fields[$key] = $value; |
|
245 | + } |
|
246 | + |
|
247 | + $result = $this->tryHttpPostToShareEndpoint(rtrim($remote, '/'), '/' . $remoteId . '/' . $action, $fields); |
|
248 | + $status = json_decode($result['result'], true); |
|
249 | + |
|
250 | + if ($result['success'] && |
|
251 | + ($status['ocs']['meta']['statuscode'] === 100 || |
|
252 | + $status['ocs']['meta']['statuscode'] === 200 |
|
253 | + ) |
|
254 | + ) { |
|
255 | + return true; |
|
256 | + } elseif ($try === 0) { |
|
257 | + // only add new job on first try |
|
258 | + $this->jobList->add('OCA\FederatedFileSharing\BackgroundJob\RetryJob', |
|
259 | + [ |
|
260 | + 'remote' => $remote, |
|
261 | + 'remoteId' => $remoteId, |
|
262 | + 'token' => $token, |
|
263 | + 'action' => $action, |
|
264 | + 'data' => json_encode($data), |
|
265 | + 'try' => $try, |
|
266 | + 'lastRun' => $this->getTimestamp() |
|
267 | + ] |
|
268 | + ); |
|
269 | + } |
|
270 | + |
|
271 | + return false; |
|
272 | + } |
|
273 | + |
|
274 | + |
|
275 | + /** |
|
276 | + * return current timestamp |
|
277 | + * |
|
278 | + * @return int |
|
279 | + */ |
|
280 | + protected function getTimestamp() { |
|
281 | + return time(); |
|
282 | + } |
|
283 | + |
|
284 | + /** |
|
285 | + * try http post with the given protocol, if no protocol is given we pick |
|
286 | + * the secure one (https) |
|
287 | + * |
|
288 | + * @param string $remoteDomain |
|
289 | + * @param string $urlSuffix |
|
290 | + * @param array $fields post parameters |
|
291 | + * @param string $action define the action (possible values: share, reshare, accept, decline, unshare, revoke, permissions) |
|
292 | + * @return array |
|
293 | + * @throws \Exception |
|
294 | + */ |
|
295 | + protected function tryHttpPostToShareEndpoint($remoteDomain, $urlSuffix, array $fields, $action="share") { |
|
296 | + |
|
297 | + if ($this->addressHandler->urlContainProtocol($remoteDomain) === false) { |
|
298 | + $remoteDomain = 'https://' . $remoteDomain; |
|
299 | + } |
|
300 | + |
|
301 | + $result = [ |
|
302 | + 'success' => false, |
|
303 | + 'result' => '', |
|
304 | + ]; |
|
305 | + |
|
306 | + // if possible we use the new OCM API |
|
307 | + $ocmResult = $this->tryOCMEndPoint($remoteDomain, $fields, $action); |
|
308 | + if ($ocmResult) { |
|
309 | + $result['success'] = true; |
|
310 | + return $result; |
|
311 | + } |
|
312 | + |
|
313 | + // Fall back to old API |
|
314 | + $client = $this->httpClientService->newClient(); |
|
315 | + $federationEndpoints = $this->discoveryService->discover($remoteDomain, 'FEDERATED_SHARING'); |
|
316 | + $endpoint = isset($federationEndpoints['share']) ? $federationEndpoints['share'] : '/ocs/v2.php/cloud/shares'; |
|
317 | + try { |
|
318 | + $response = $client->post($remoteDomain . $endpoint . $urlSuffix . '?format=' . self::RESPONSE_FORMAT, [ |
|
319 | + 'body' => $fields, |
|
320 | + 'timeout' => 10, |
|
321 | + 'connect_timeout' => 10, |
|
322 | + ]); |
|
323 | + $result['result'] = $response->getBody(); |
|
324 | + $result['success'] = true; |
|
325 | + } catch (\Exception $e) { |
|
326 | + // if flat re-sharing is not supported by the remote server |
|
327 | + // we re-throw the exception and fall back to the old behaviour. |
|
328 | + // (flat re-shares has been introduced in Nextcloud 9.1) |
|
329 | + if ($e->getCode() === Http::STATUS_INTERNAL_SERVER_ERROR) { |
|
330 | + throw $e; |
|
331 | + } |
|
332 | + } |
|
333 | + |
|
334 | + return $result; |
|
335 | + } |
|
336 | + |
|
337 | + /** |
|
338 | + * check if server supports the new OCM api and ask for the correct end-point |
|
339 | + * |
|
340 | + * @param string $url |
|
341 | + * @return string |
|
342 | + */ |
|
343 | + protected function getOCMEndPoint($url) { |
|
344 | + $client = $this->httpClientService->newClient(); |
|
345 | + try { |
|
346 | + $response = $client->get($url, ['timeout' => 10, 'connect_timeout' => 10]); |
|
347 | + } catch (\Exception $e) { |
|
348 | + return ''; |
|
349 | + } |
|
350 | + |
|
351 | + $result = $response->getBody(); |
|
352 | + $result = json_decode($result, true); |
|
353 | + |
|
354 | + if (isset($result['end-point'])) { |
|
355 | + return $result['end-point']; |
|
356 | + } |
|
357 | + |
|
358 | + return ''; |
|
359 | + } |
|
360 | + |
|
361 | + /** |
|
362 | + * send action regarding federated sharing to the remote server using the OCM API |
|
363 | + * |
|
364 | + * @param $remoteDomain |
|
365 | + * @param $fields |
|
366 | + * @param $action |
|
367 | + * |
|
368 | + * @return bool |
|
369 | + */ |
|
370 | + protected function tryOCMEndPoint($remoteDomain, $fields, $action) { |
|
371 | + switch ($action) { |
|
372 | + case 'share': |
|
373 | + $share = $this->cloudFederationFactory->getCloudFederationShare( |
|
374 | + $fields['shareWith'] . '@' . $remoteDomain, |
|
375 | + $fields['name'], |
|
376 | + '', |
|
377 | + $fields['remoteId'], |
|
378 | + $fields['ownerFederatedId'], |
|
379 | + $fields['owner'], |
|
380 | + $fields['sharedByFederatedId'], |
|
381 | + $fields['sharedBy'], |
|
382 | + $fields['token'], |
|
383 | + 'user', |
|
384 | + 'file' |
|
385 | + ); |
|
386 | + return $this->federationProviderManager->sendShare($share); |
|
387 | + } |
|
388 | + |
|
389 | + } |
|
390 | 390 | } |
@@ -44,497 +44,497 @@ |
||
44 | 44 | use OCP\Share; |
45 | 45 | |
46 | 46 | class Manager { |
47 | - const STORAGE = '\OCA\Files_Sharing\External\Storage'; |
|
48 | - |
|
49 | - /** |
|
50 | - * @var string |
|
51 | - */ |
|
52 | - private $uid; |
|
53 | - |
|
54 | - /** |
|
55 | - * @var IDBConnection |
|
56 | - */ |
|
57 | - private $connection; |
|
58 | - |
|
59 | - /** |
|
60 | - * @var \OC\Files\Mount\Manager |
|
61 | - */ |
|
62 | - private $mountManager; |
|
63 | - |
|
64 | - /** |
|
65 | - * @var IStorageFactory |
|
66 | - */ |
|
67 | - private $storageLoader; |
|
68 | - |
|
69 | - /** |
|
70 | - * @var IClientService |
|
71 | - */ |
|
72 | - private $clientService; |
|
73 | - |
|
74 | - /** |
|
75 | - * @var IManager |
|
76 | - */ |
|
77 | - private $notificationManager; |
|
78 | - |
|
79 | - /** |
|
80 | - * @var IDiscoveryService |
|
81 | - */ |
|
82 | - private $discoveryService; |
|
83 | - |
|
84 | - /** @var ICloudFederationProviderManager */ |
|
85 | - private $cloudFederationProviderManager; |
|
86 | - |
|
87 | - /** @var ICloudFederationFactory */ |
|
88 | - private $cloudFederationFactory; |
|
89 | - |
|
90 | - /** |
|
91 | - * @param IDBConnection $connection |
|
92 | - * @param \OC\Files\Mount\Manager $mountManager |
|
93 | - * @param IStorageFactory $storageLoader |
|
94 | - * @param IClientService $clientService |
|
95 | - * @param IManager $notificationManager |
|
96 | - * @param IDiscoveryService $discoveryService |
|
97 | - * @param ICloudFederationProviderManager $cloudFederationProviderManager |
|
98 | - * @param ICloudFederationFactory $cloudFederationFactory |
|
99 | - * @param string $uid |
|
100 | - */ |
|
101 | - public function __construct(IDBConnection $connection, |
|
102 | - \OC\Files\Mount\Manager $mountManager, |
|
103 | - IStorageFactory $storageLoader, |
|
104 | - IClientService $clientService, |
|
105 | - IManager $notificationManager, |
|
106 | - IDiscoveryService $discoveryService, |
|
107 | - ICloudFederationProviderManager $cloudFederationProviderManager, |
|
108 | - ICloudFederationFactory $cloudFederationFactory, |
|
109 | - $uid) { |
|
110 | - $this->connection = $connection; |
|
111 | - $this->mountManager = $mountManager; |
|
112 | - $this->storageLoader = $storageLoader; |
|
113 | - $this->clientService = $clientService; |
|
114 | - $this->uid = $uid; |
|
115 | - $this->notificationManager = $notificationManager; |
|
116 | - $this->discoveryService = $discoveryService; |
|
117 | - $this->cloudFederationProviderManager = $cloudFederationProviderManager; |
|
118 | - $this->cloudFederationFactory = $cloudFederationFactory; |
|
119 | - } |
|
120 | - |
|
121 | - /** |
|
122 | - * add new server-to-server share |
|
123 | - * |
|
124 | - * @param string $remote |
|
125 | - * @param string $token |
|
126 | - * @param string $password |
|
127 | - * @param string $name |
|
128 | - * @param string $owner |
|
129 | - * @param boolean $accepted |
|
130 | - * @param string $user |
|
131 | - * @param int $remoteId |
|
132 | - * @return Mount|null |
|
133 | - */ |
|
134 | - public function addShare($remote, $token, $password, $name, $owner, $accepted=false, $user = null, $remoteId = -1) { |
|
135 | - |
|
136 | - $user = $user ? $user : $this->uid; |
|
137 | - $accepted = $accepted ? 1 : 0; |
|
138 | - $name = Filesystem::normalizePath('/' . $name); |
|
139 | - |
|
140 | - if (!$accepted) { |
|
141 | - // To avoid conflicts with the mount point generation later, |
|
142 | - // we only use a temporary mount point name here. The real |
|
143 | - // mount point name will be generated when accepting the share, |
|
144 | - // using the original share item name. |
|
145 | - $tmpMountPointName = '{{TemporaryMountPointName#' . $name . '}}'; |
|
146 | - $mountPoint = $tmpMountPointName; |
|
147 | - $hash = md5($tmpMountPointName); |
|
148 | - $data = [ |
|
149 | - 'remote' => $remote, |
|
150 | - 'share_token' => $token, |
|
151 | - 'password' => $password, |
|
152 | - 'name' => $name, |
|
153 | - 'owner' => $owner, |
|
154 | - 'user' => $user, |
|
155 | - 'mountpoint' => $mountPoint, |
|
156 | - 'mountpoint_hash' => $hash, |
|
157 | - 'accepted' => $accepted, |
|
158 | - 'remote_id' => $remoteId, |
|
159 | - ]; |
|
160 | - |
|
161 | - $i = 1; |
|
162 | - while (!$this->connection->insertIfNotExist('*PREFIX*share_external', $data, ['user', 'mountpoint_hash'])) { |
|
163 | - // The external share already exists for the user |
|
164 | - $data['mountpoint'] = $tmpMountPointName . '-' . $i; |
|
165 | - $data['mountpoint_hash'] = md5($data['mountpoint']); |
|
166 | - $i++; |
|
167 | - } |
|
168 | - return null; |
|
169 | - } |
|
170 | - |
|
171 | - $mountPoint = Files::buildNotExistingFileName('/', $name); |
|
172 | - $mountPoint = Filesystem::normalizePath('/' . $mountPoint); |
|
173 | - $hash = md5($mountPoint); |
|
174 | - |
|
175 | - $query = $this->connection->prepare(' |
|
47 | + const STORAGE = '\OCA\Files_Sharing\External\Storage'; |
|
48 | + |
|
49 | + /** |
|
50 | + * @var string |
|
51 | + */ |
|
52 | + private $uid; |
|
53 | + |
|
54 | + /** |
|
55 | + * @var IDBConnection |
|
56 | + */ |
|
57 | + private $connection; |
|
58 | + |
|
59 | + /** |
|
60 | + * @var \OC\Files\Mount\Manager |
|
61 | + */ |
|
62 | + private $mountManager; |
|
63 | + |
|
64 | + /** |
|
65 | + * @var IStorageFactory |
|
66 | + */ |
|
67 | + private $storageLoader; |
|
68 | + |
|
69 | + /** |
|
70 | + * @var IClientService |
|
71 | + */ |
|
72 | + private $clientService; |
|
73 | + |
|
74 | + /** |
|
75 | + * @var IManager |
|
76 | + */ |
|
77 | + private $notificationManager; |
|
78 | + |
|
79 | + /** |
|
80 | + * @var IDiscoveryService |
|
81 | + */ |
|
82 | + private $discoveryService; |
|
83 | + |
|
84 | + /** @var ICloudFederationProviderManager */ |
|
85 | + private $cloudFederationProviderManager; |
|
86 | + |
|
87 | + /** @var ICloudFederationFactory */ |
|
88 | + private $cloudFederationFactory; |
|
89 | + |
|
90 | + /** |
|
91 | + * @param IDBConnection $connection |
|
92 | + * @param \OC\Files\Mount\Manager $mountManager |
|
93 | + * @param IStorageFactory $storageLoader |
|
94 | + * @param IClientService $clientService |
|
95 | + * @param IManager $notificationManager |
|
96 | + * @param IDiscoveryService $discoveryService |
|
97 | + * @param ICloudFederationProviderManager $cloudFederationProviderManager |
|
98 | + * @param ICloudFederationFactory $cloudFederationFactory |
|
99 | + * @param string $uid |
|
100 | + */ |
|
101 | + public function __construct(IDBConnection $connection, |
|
102 | + \OC\Files\Mount\Manager $mountManager, |
|
103 | + IStorageFactory $storageLoader, |
|
104 | + IClientService $clientService, |
|
105 | + IManager $notificationManager, |
|
106 | + IDiscoveryService $discoveryService, |
|
107 | + ICloudFederationProviderManager $cloudFederationProviderManager, |
|
108 | + ICloudFederationFactory $cloudFederationFactory, |
|
109 | + $uid) { |
|
110 | + $this->connection = $connection; |
|
111 | + $this->mountManager = $mountManager; |
|
112 | + $this->storageLoader = $storageLoader; |
|
113 | + $this->clientService = $clientService; |
|
114 | + $this->uid = $uid; |
|
115 | + $this->notificationManager = $notificationManager; |
|
116 | + $this->discoveryService = $discoveryService; |
|
117 | + $this->cloudFederationProviderManager = $cloudFederationProviderManager; |
|
118 | + $this->cloudFederationFactory = $cloudFederationFactory; |
|
119 | + } |
|
120 | + |
|
121 | + /** |
|
122 | + * add new server-to-server share |
|
123 | + * |
|
124 | + * @param string $remote |
|
125 | + * @param string $token |
|
126 | + * @param string $password |
|
127 | + * @param string $name |
|
128 | + * @param string $owner |
|
129 | + * @param boolean $accepted |
|
130 | + * @param string $user |
|
131 | + * @param int $remoteId |
|
132 | + * @return Mount|null |
|
133 | + */ |
|
134 | + public function addShare($remote, $token, $password, $name, $owner, $accepted=false, $user = null, $remoteId = -1) { |
|
135 | + |
|
136 | + $user = $user ? $user : $this->uid; |
|
137 | + $accepted = $accepted ? 1 : 0; |
|
138 | + $name = Filesystem::normalizePath('/' . $name); |
|
139 | + |
|
140 | + if (!$accepted) { |
|
141 | + // To avoid conflicts with the mount point generation later, |
|
142 | + // we only use a temporary mount point name here. The real |
|
143 | + // mount point name will be generated when accepting the share, |
|
144 | + // using the original share item name. |
|
145 | + $tmpMountPointName = '{{TemporaryMountPointName#' . $name . '}}'; |
|
146 | + $mountPoint = $tmpMountPointName; |
|
147 | + $hash = md5($tmpMountPointName); |
|
148 | + $data = [ |
|
149 | + 'remote' => $remote, |
|
150 | + 'share_token' => $token, |
|
151 | + 'password' => $password, |
|
152 | + 'name' => $name, |
|
153 | + 'owner' => $owner, |
|
154 | + 'user' => $user, |
|
155 | + 'mountpoint' => $mountPoint, |
|
156 | + 'mountpoint_hash' => $hash, |
|
157 | + 'accepted' => $accepted, |
|
158 | + 'remote_id' => $remoteId, |
|
159 | + ]; |
|
160 | + |
|
161 | + $i = 1; |
|
162 | + while (!$this->connection->insertIfNotExist('*PREFIX*share_external', $data, ['user', 'mountpoint_hash'])) { |
|
163 | + // The external share already exists for the user |
|
164 | + $data['mountpoint'] = $tmpMountPointName . '-' . $i; |
|
165 | + $data['mountpoint_hash'] = md5($data['mountpoint']); |
|
166 | + $i++; |
|
167 | + } |
|
168 | + return null; |
|
169 | + } |
|
170 | + |
|
171 | + $mountPoint = Files::buildNotExistingFileName('/', $name); |
|
172 | + $mountPoint = Filesystem::normalizePath('/' . $mountPoint); |
|
173 | + $hash = md5($mountPoint); |
|
174 | + |
|
175 | + $query = $this->connection->prepare(' |
|
176 | 176 | INSERT INTO `*PREFIX*share_external` |
177 | 177 | (`remote`, `share_token`, `password`, `name`, `owner`, `user`, `mountpoint`, `mountpoint_hash`, `accepted`, `remote_id`) |
178 | 178 | VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?) |
179 | 179 | '); |
180 | - $query->execute(array($remote, $token, $password, $name, $owner, $user, $mountPoint, $hash, $accepted, $remoteId)); |
|
181 | - |
|
182 | - $options = array( |
|
183 | - 'remote' => $remote, |
|
184 | - 'token' => $token, |
|
185 | - 'password' => $password, |
|
186 | - 'mountpoint' => $mountPoint, |
|
187 | - 'owner' => $owner |
|
188 | - ); |
|
189 | - return $this->mountShare($options); |
|
190 | - } |
|
191 | - |
|
192 | - /** |
|
193 | - * get share |
|
194 | - * |
|
195 | - * @param int $id share id |
|
196 | - * @return mixed share of false |
|
197 | - */ |
|
198 | - public function getShare($id) { |
|
199 | - $getShare = $this->connection->prepare(' |
|
180 | + $query->execute(array($remote, $token, $password, $name, $owner, $user, $mountPoint, $hash, $accepted, $remoteId)); |
|
181 | + |
|
182 | + $options = array( |
|
183 | + 'remote' => $remote, |
|
184 | + 'token' => $token, |
|
185 | + 'password' => $password, |
|
186 | + 'mountpoint' => $mountPoint, |
|
187 | + 'owner' => $owner |
|
188 | + ); |
|
189 | + return $this->mountShare($options); |
|
190 | + } |
|
191 | + |
|
192 | + /** |
|
193 | + * get share |
|
194 | + * |
|
195 | + * @param int $id share id |
|
196 | + * @return mixed share of false |
|
197 | + */ |
|
198 | + public function getShare($id) { |
|
199 | + $getShare = $this->connection->prepare(' |
|
200 | 200 | SELECT `id`, `remote`, `remote_id`, `share_token`, `name`, `owner`, `user`, `mountpoint`, `accepted` |
201 | 201 | FROM `*PREFIX*share_external` |
202 | 202 | WHERE `id` = ? AND `user` = ?'); |
203 | - $result = $getShare->execute(array($id, $this->uid)); |
|
204 | - |
|
205 | - return $result ? $getShare->fetch() : false; |
|
206 | - } |
|
207 | - |
|
208 | - /** |
|
209 | - * accept server-to-server share |
|
210 | - * |
|
211 | - * @param int $id |
|
212 | - * @return bool True if the share could be accepted, false otherwise |
|
213 | - */ |
|
214 | - public function acceptShare($id) { |
|
215 | - |
|
216 | - $share = $this->getShare($id); |
|
217 | - $result = false; |
|
218 | - |
|
219 | - if ($share) { |
|
220 | - \OC_Util::setupFS($this->uid); |
|
221 | - $shareFolder = Helper::getShareFolder(); |
|
222 | - $mountPoint = Files::buildNotExistingFileName($shareFolder, $share['name']); |
|
223 | - $mountPoint = Filesystem::normalizePath($mountPoint); |
|
224 | - $hash = md5($mountPoint); |
|
225 | - |
|
226 | - $acceptShare = $this->connection->prepare(' |
|
203 | + $result = $getShare->execute(array($id, $this->uid)); |
|
204 | + |
|
205 | + return $result ? $getShare->fetch() : false; |
|
206 | + } |
|
207 | + |
|
208 | + /** |
|
209 | + * accept server-to-server share |
|
210 | + * |
|
211 | + * @param int $id |
|
212 | + * @return bool True if the share could be accepted, false otherwise |
|
213 | + */ |
|
214 | + public function acceptShare($id) { |
|
215 | + |
|
216 | + $share = $this->getShare($id); |
|
217 | + $result = false; |
|
218 | + |
|
219 | + if ($share) { |
|
220 | + \OC_Util::setupFS($this->uid); |
|
221 | + $shareFolder = Helper::getShareFolder(); |
|
222 | + $mountPoint = Files::buildNotExistingFileName($shareFolder, $share['name']); |
|
223 | + $mountPoint = Filesystem::normalizePath($mountPoint); |
|
224 | + $hash = md5($mountPoint); |
|
225 | + |
|
226 | + $acceptShare = $this->connection->prepare(' |
|
227 | 227 | UPDATE `*PREFIX*share_external` |
228 | 228 | SET `accepted` = ?, |
229 | 229 | `mountpoint` = ?, |
230 | 230 | `mountpoint_hash` = ? |
231 | 231 | WHERE `id` = ? AND `user` = ?'); |
232 | - $updated = $acceptShare->execute(array(1, $mountPoint, $hash, $id, $this->uid)); |
|
233 | - if ($updated === true) { |
|
234 | - $this->sendFeedbackToRemote($share['remote'], $share['share_token'], $share['remote_id'], 'accept'); |
|
235 | - \OC_Hook::emit(Share::class, 'federated_share_added', ['server' => $share['remote']]); |
|
236 | - $result = true; |
|
237 | - } |
|
238 | - } |
|
239 | - |
|
240 | - // Make sure the user has no notification for something that does not exist anymore. |
|
241 | - $this->processNotification($id); |
|
242 | - |
|
243 | - return $result; |
|
244 | - } |
|
245 | - |
|
246 | - /** |
|
247 | - * decline server-to-server share |
|
248 | - * |
|
249 | - * @param int $id |
|
250 | - * @return bool True if the share could be declined, false otherwise |
|
251 | - */ |
|
252 | - public function declineShare($id) { |
|
253 | - |
|
254 | - $share = $this->getShare($id); |
|
255 | - |
|
256 | - if ($share) { |
|
257 | - $removeShare = $this->connection->prepare(' |
|
232 | + $updated = $acceptShare->execute(array(1, $mountPoint, $hash, $id, $this->uid)); |
|
233 | + if ($updated === true) { |
|
234 | + $this->sendFeedbackToRemote($share['remote'], $share['share_token'], $share['remote_id'], 'accept'); |
|
235 | + \OC_Hook::emit(Share::class, 'federated_share_added', ['server' => $share['remote']]); |
|
236 | + $result = true; |
|
237 | + } |
|
238 | + } |
|
239 | + |
|
240 | + // Make sure the user has no notification for something that does not exist anymore. |
|
241 | + $this->processNotification($id); |
|
242 | + |
|
243 | + return $result; |
|
244 | + } |
|
245 | + |
|
246 | + /** |
|
247 | + * decline server-to-server share |
|
248 | + * |
|
249 | + * @param int $id |
|
250 | + * @return bool True if the share could be declined, false otherwise |
|
251 | + */ |
|
252 | + public function declineShare($id) { |
|
253 | + |
|
254 | + $share = $this->getShare($id); |
|
255 | + |
|
256 | + if ($share) { |
|
257 | + $removeShare = $this->connection->prepare(' |
|
258 | 258 | DELETE FROM `*PREFIX*share_external` WHERE `id` = ? AND `user` = ?'); |
259 | - $removeShare->execute(array($id, $this->uid)); |
|
260 | - $this->sendFeedbackToRemote($share['remote'], $share['share_token'], $share['remote_id'], 'decline'); |
|
261 | - |
|
262 | - $this->processNotification($id); |
|
263 | - return true; |
|
264 | - } |
|
265 | - |
|
266 | - return false; |
|
267 | - } |
|
268 | - |
|
269 | - /** |
|
270 | - * @param int $remoteShare |
|
271 | - */ |
|
272 | - public function processNotification($remoteShare) { |
|
273 | - $filter = $this->notificationManager->createNotification(); |
|
274 | - $filter->setApp('files_sharing') |
|
275 | - ->setUser($this->uid) |
|
276 | - ->setObject('remote_share', (int) $remoteShare); |
|
277 | - $this->notificationManager->markProcessed($filter); |
|
278 | - } |
|
279 | - |
|
280 | - /** |
|
281 | - * inform remote server whether server-to-server share was accepted/declined |
|
282 | - * |
|
283 | - * @param string $remote |
|
284 | - * @param string $token |
|
285 | - * @param int $remoteId Share id on the remote host |
|
286 | - * @param string $feedback |
|
287 | - * @return boolean |
|
288 | - */ |
|
289 | - private function sendFeedbackToRemote($remote, $token, $remoteId, $feedback) { |
|
290 | - |
|
291 | - $result = $this->tryOCMEndPoint($remote, $token, $remoteId, $feedback); |
|
292 | - |
|
293 | - if($result === true) { |
|
294 | - return true; |
|
295 | - } |
|
296 | - |
|
297 | - $federationEndpoints = $this->discoveryService->discover($remote, 'FEDERATED_SHARING'); |
|
298 | - $endpoint = isset($federationEndpoints['share']) ? $federationEndpoints['share'] : '/ocs/v2.php/cloud/shares'; |
|
299 | - |
|
300 | - $url = rtrim($remote, '/') . $endpoint . '/' . $remoteId . '/' . $feedback . '?format=' . \OCP\Share::RESPONSE_FORMAT; |
|
301 | - $fields = array('token' => $token); |
|
302 | - |
|
303 | - $client = $this->clientService->newClient(); |
|
304 | - |
|
305 | - try { |
|
306 | - $response = $client->post( |
|
307 | - $url, |
|
308 | - [ |
|
309 | - 'body' => $fields, |
|
310 | - 'connect_timeout' => 10, |
|
311 | - ] |
|
312 | - ); |
|
313 | - } catch (\Exception $e) { |
|
314 | - return false; |
|
315 | - } |
|
316 | - |
|
317 | - $status = json_decode($response->getBody(), true); |
|
318 | - |
|
319 | - return ($status['ocs']['meta']['statuscode'] === 100 || $status['ocs']['meta']['statuscode'] === 200); |
|
320 | - } |
|
321 | - |
|
322 | - /** |
|
323 | - * try send accept message to ocm end-point |
|
324 | - * |
|
325 | - * @param string $remoteDomain |
|
326 | - * @param string $token |
|
327 | - * @param $remoteId id of the share |
|
328 | - * @param string $feedback |
|
329 | - * @return mixed |
|
330 | - */ |
|
331 | - protected function tryOCMEndPoint($remoteDomain, $token, $remoteId, $feedback) { |
|
332 | - switch ($feedback) { |
|
333 | - case 'accept': |
|
334 | - $notification = $this->cloudFederationFactory->getCloudFederationNotification(); |
|
335 | - $notification->setMessage( |
|
336 | - 'SHARE_ACCEPTED', |
|
337 | - 'file', |
|
338 | - $remoteId, |
|
339 | - [ |
|
340 | - 'sharedSecret' => $token, |
|
341 | - 'message' => 'Recipient accept the share' |
|
342 | - ] |
|
343 | - |
|
344 | - ); |
|
345 | - return $this->cloudFederationProviderManager->sendNotification($remoteDomain, $notification); |
|
346 | - } |
|
347 | - |
|
348 | - } |
|
349 | - |
|
350 | - |
|
351 | - /** |
|
352 | - * remove '/user/files' from the path and trailing slashes |
|
353 | - * |
|
354 | - * @param string $path |
|
355 | - * @return string |
|
356 | - */ |
|
357 | - protected function stripPath($path) { |
|
358 | - $prefix = '/' . $this->uid . '/files'; |
|
359 | - return rtrim(substr($path, strlen($prefix)), '/'); |
|
360 | - } |
|
361 | - |
|
362 | - public function getMount($data) { |
|
363 | - $data['manager'] = $this; |
|
364 | - $mountPoint = '/' . $this->uid . '/files' . $data['mountpoint']; |
|
365 | - $data['mountpoint'] = $mountPoint; |
|
366 | - $data['certificateManager'] = \OC::$server->getCertificateManager($this->uid); |
|
367 | - return new Mount(self::STORAGE, $mountPoint, $data, $this, $this->storageLoader); |
|
368 | - } |
|
369 | - |
|
370 | - /** |
|
371 | - * @param array $data |
|
372 | - * @return Mount |
|
373 | - */ |
|
374 | - protected function mountShare($data) { |
|
375 | - $mount = $this->getMount($data); |
|
376 | - $this->mountManager->addMount($mount); |
|
377 | - return $mount; |
|
378 | - } |
|
379 | - |
|
380 | - /** |
|
381 | - * @return \OC\Files\Mount\Manager |
|
382 | - */ |
|
383 | - public function getMountManager() { |
|
384 | - return $this->mountManager; |
|
385 | - } |
|
386 | - |
|
387 | - /** |
|
388 | - * @param string $source |
|
389 | - * @param string $target |
|
390 | - * @return bool |
|
391 | - */ |
|
392 | - public function setMountPoint($source, $target) { |
|
393 | - $source = $this->stripPath($source); |
|
394 | - $target = $this->stripPath($target); |
|
395 | - $sourceHash = md5($source); |
|
396 | - $targetHash = md5($target); |
|
397 | - |
|
398 | - $query = $this->connection->prepare(' |
|
259 | + $removeShare->execute(array($id, $this->uid)); |
|
260 | + $this->sendFeedbackToRemote($share['remote'], $share['share_token'], $share['remote_id'], 'decline'); |
|
261 | + |
|
262 | + $this->processNotification($id); |
|
263 | + return true; |
|
264 | + } |
|
265 | + |
|
266 | + return false; |
|
267 | + } |
|
268 | + |
|
269 | + /** |
|
270 | + * @param int $remoteShare |
|
271 | + */ |
|
272 | + public function processNotification($remoteShare) { |
|
273 | + $filter = $this->notificationManager->createNotification(); |
|
274 | + $filter->setApp('files_sharing') |
|
275 | + ->setUser($this->uid) |
|
276 | + ->setObject('remote_share', (int) $remoteShare); |
|
277 | + $this->notificationManager->markProcessed($filter); |
|
278 | + } |
|
279 | + |
|
280 | + /** |
|
281 | + * inform remote server whether server-to-server share was accepted/declined |
|
282 | + * |
|
283 | + * @param string $remote |
|
284 | + * @param string $token |
|
285 | + * @param int $remoteId Share id on the remote host |
|
286 | + * @param string $feedback |
|
287 | + * @return boolean |
|
288 | + */ |
|
289 | + private function sendFeedbackToRemote($remote, $token, $remoteId, $feedback) { |
|
290 | + |
|
291 | + $result = $this->tryOCMEndPoint($remote, $token, $remoteId, $feedback); |
|
292 | + |
|
293 | + if($result === true) { |
|
294 | + return true; |
|
295 | + } |
|
296 | + |
|
297 | + $federationEndpoints = $this->discoveryService->discover($remote, 'FEDERATED_SHARING'); |
|
298 | + $endpoint = isset($federationEndpoints['share']) ? $federationEndpoints['share'] : '/ocs/v2.php/cloud/shares'; |
|
299 | + |
|
300 | + $url = rtrim($remote, '/') . $endpoint . '/' . $remoteId . '/' . $feedback . '?format=' . \OCP\Share::RESPONSE_FORMAT; |
|
301 | + $fields = array('token' => $token); |
|
302 | + |
|
303 | + $client = $this->clientService->newClient(); |
|
304 | + |
|
305 | + try { |
|
306 | + $response = $client->post( |
|
307 | + $url, |
|
308 | + [ |
|
309 | + 'body' => $fields, |
|
310 | + 'connect_timeout' => 10, |
|
311 | + ] |
|
312 | + ); |
|
313 | + } catch (\Exception $e) { |
|
314 | + return false; |
|
315 | + } |
|
316 | + |
|
317 | + $status = json_decode($response->getBody(), true); |
|
318 | + |
|
319 | + return ($status['ocs']['meta']['statuscode'] === 100 || $status['ocs']['meta']['statuscode'] === 200); |
|
320 | + } |
|
321 | + |
|
322 | + /** |
|
323 | + * try send accept message to ocm end-point |
|
324 | + * |
|
325 | + * @param string $remoteDomain |
|
326 | + * @param string $token |
|
327 | + * @param $remoteId id of the share |
|
328 | + * @param string $feedback |
|
329 | + * @return mixed |
|
330 | + */ |
|
331 | + protected function tryOCMEndPoint($remoteDomain, $token, $remoteId, $feedback) { |
|
332 | + switch ($feedback) { |
|
333 | + case 'accept': |
|
334 | + $notification = $this->cloudFederationFactory->getCloudFederationNotification(); |
|
335 | + $notification->setMessage( |
|
336 | + 'SHARE_ACCEPTED', |
|
337 | + 'file', |
|
338 | + $remoteId, |
|
339 | + [ |
|
340 | + 'sharedSecret' => $token, |
|
341 | + 'message' => 'Recipient accept the share' |
|
342 | + ] |
|
343 | + |
|
344 | + ); |
|
345 | + return $this->cloudFederationProviderManager->sendNotification($remoteDomain, $notification); |
|
346 | + } |
|
347 | + |
|
348 | + } |
|
349 | + |
|
350 | + |
|
351 | + /** |
|
352 | + * remove '/user/files' from the path and trailing slashes |
|
353 | + * |
|
354 | + * @param string $path |
|
355 | + * @return string |
|
356 | + */ |
|
357 | + protected function stripPath($path) { |
|
358 | + $prefix = '/' . $this->uid . '/files'; |
|
359 | + return rtrim(substr($path, strlen($prefix)), '/'); |
|
360 | + } |
|
361 | + |
|
362 | + public function getMount($data) { |
|
363 | + $data['manager'] = $this; |
|
364 | + $mountPoint = '/' . $this->uid . '/files' . $data['mountpoint']; |
|
365 | + $data['mountpoint'] = $mountPoint; |
|
366 | + $data['certificateManager'] = \OC::$server->getCertificateManager($this->uid); |
|
367 | + return new Mount(self::STORAGE, $mountPoint, $data, $this, $this->storageLoader); |
|
368 | + } |
|
369 | + |
|
370 | + /** |
|
371 | + * @param array $data |
|
372 | + * @return Mount |
|
373 | + */ |
|
374 | + protected function mountShare($data) { |
|
375 | + $mount = $this->getMount($data); |
|
376 | + $this->mountManager->addMount($mount); |
|
377 | + return $mount; |
|
378 | + } |
|
379 | + |
|
380 | + /** |
|
381 | + * @return \OC\Files\Mount\Manager |
|
382 | + */ |
|
383 | + public function getMountManager() { |
|
384 | + return $this->mountManager; |
|
385 | + } |
|
386 | + |
|
387 | + /** |
|
388 | + * @param string $source |
|
389 | + * @param string $target |
|
390 | + * @return bool |
|
391 | + */ |
|
392 | + public function setMountPoint($source, $target) { |
|
393 | + $source = $this->stripPath($source); |
|
394 | + $target = $this->stripPath($target); |
|
395 | + $sourceHash = md5($source); |
|
396 | + $targetHash = md5($target); |
|
397 | + |
|
398 | + $query = $this->connection->prepare(' |
|
399 | 399 | UPDATE `*PREFIX*share_external` |
400 | 400 | SET `mountpoint` = ?, `mountpoint_hash` = ? |
401 | 401 | WHERE `mountpoint_hash` = ? |
402 | 402 | AND `user` = ? |
403 | 403 | '); |
404 | - $result = (bool)$query->execute(array($target, $targetHash, $sourceHash, $this->uid)); |
|
404 | + $result = (bool)$query->execute(array($target, $targetHash, $sourceHash, $this->uid)); |
|
405 | 405 | |
406 | - return $result; |
|
407 | - } |
|
406 | + return $result; |
|
407 | + } |
|
408 | 408 | |
409 | - public function removeShare($mountPoint) { |
|
409 | + public function removeShare($mountPoint) { |
|
410 | 410 | |
411 | - $mountPointObj = $this->mountManager->find($mountPoint); |
|
412 | - $id = $mountPointObj->getStorage()->getCache()->getId(''); |
|
411 | + $mountPointObj = $this->mountManager->find($mountPoint); |
|
412 | + $id = $mountPointObj->getStorage()->getCache()->getId(''); |
|
413 | 413 | |
414 | - $mountPoint = $this->stripPath($mountPoint); |
|
415 | - $hash = md5($mountPoint); |
|
414 | + $mountPoint = $this->stripPath($mountPoint); |
|
415 | + $hash = md5($mountPoint); |
|
416 | 416 | |
417 | - $getShare = $this->connection->prepare(' |
|
417 | + $getShare = $this->connection->prepare(' |
|
418 | 418 | SELECT `remote`, `share_token`, `remote_id` |
419 | 419 | FROM `*PREFIX*share_external` |
420 | 420 | WHERE `mountpoint_hash` = ? AND `user` = ?'); |
421 | - $result = $getShare->execute(array($hash, $this->uid)); |
|
422 | - |
|
423 | - if ($result) { |
|
424 | - try { |
|
425 | - $share = $getShare->fetch(); |
|
426 | - $this->sendFeedbackToRemote($share['remote'], $share['share_token'], $share['remote_id'], 'decline'); |
|
427 | - } catch (\Exception $e) { |
|
428 | - // if we fail to notify the remote (probably cause the remote is down) |
|
429 | - // we still want the share to be gone to prevent undeletable remotes |
|
430 | - } |
|
431 | - } |
|
432 | - $getShare->closeCursor(); |
|
433 | - |
|
434 | - $query = $this->connection->prepare(' |
|
421 | + $result = $getShare->execute(array($hash, $this->uid)); |
|
422 | + |
|
423 | + if ($result) { |
|
424 | + try { |
|
425 | + $share = $getShare->fetch(); |
|
426 | + $this->sendFeedbackToRemote($share['remote'], $share['share_token'], $share['remote_id'], 'decline'); |
|
427 | + } catch (\Exception $e) { |
|
428 | + // if we fail to notify the remote (probably cause the remote is down) |
|
429 | + // we still want the share to be gone to prevent undeletable remotes |
|
430 | + } |
|
431 | + } |
|
432 | + $getShare->closeCursor(); |
|
433 | + |
|
434 | + $query = $this->connection->prepare(' |
|
435 | 435 | DELETE FROM `*PREFIX*share_external` |
436 | 436 | WHERE `mountpoint_hash` = ? |
437 | 437 | AND `user` = ? |
438 | 438 | '); |
439 | - $result = (bool)$query->execute(array($hash, $this->uid)); |
|
440 | - |
|
441 | - if($result) { |
|
442 | - $this->removeReShares($id); |
|
443 | - } |
|
444 | - |
|
445 | - return $result; |
|
446 | - } |
|
447 | - |
|
448 | - /** |
|
449 | - * remove re-shares from share table and mapping in the federated_reshares table |
|
450 | - * |
|
451 | - * @param $mountPointId |
|
452 | - */ |
|
453 | - protected function removeReShares($mountPointId) { |
|
454 | - $selectQuery = $this->connection->getQueryBuilder(); |
|
455 | - $query = $this->connection->getQueryBuilder(); |
|
456 | - $selectQuery->select('id')->from('share') |
|
457 | - ->where($selectQuery->expr()->eq('file_source', $query->createNamedParameter($mountPointId))); |
|
458 | - $select = $selectQuery->getSQL(); |
|
459 | - |
|
460 | - |
|
461 | - $query->delete('federated_reshares') |
|
462 | - ->where($query->expr()->in('share_id', $query->createFunction('(' . $select . ')'))); |
|
463 | - $query->execute(); |
|
464 | - |
|
465 | - $deleteReShares = $this->connection->getQueryBuilder(); |
|
466 | - $deleteReShares->delete('share') |
|
467 | - ->where($deleteReShares->expr()->eq('file_source', $deleteReShares->createNamedParameter($mountPointId))); |
|
468 | - $deleteReShares->execute(); |
|
469 | - } |
|
470 | - |
|
471 | - /** |
|
472 | - * remove all shares for user $uid if the user was deleted |
|
473 | - * |
|
474 | - * @param string $uid |
|
475 | - * @return bool |
|
476 | - */ |
|
477 | - public function removeUserShares($uid) { |
|
478 | - $getShare = $this->connection->prepare(' |
|
439 | + $result = (bool)$query->execute(array($hash, $this->uid)); |
|
440 | + |
|
441 | + if($result) { |
|
442 | + $this->removeReShares($id); |
|
443 | + } |
|
444 | + |
|
445 | + return $result; |
|
446 | + } |
|
447 | + |
|
448 | + /** |
|
449 | + * remove re-shares from share table and mapping in the federated_reshares table |
|
450 | + * |
|
451 | + * @param $mountPointId |
|
452 | + */ |
|
453 | + protected function removeReShares($mountPointId) { |
|
454 | + $selectQuery = $this->connection->getQueryBuilder(); |
|
455 | + $query = $this->connection->getQueryBuilder(); |
|
456 | + $selectQuery->select('id')->from('share') |
|
457 | + ->where($selectQuery->expr()->eq('file_source', $query->createNamedParameter($mountPointId))); |
|
458 | + $select = $selectQuery->getSQL(); |
|
459 | + |
|
460 | + |
|
461 | + $query->delete('federated_reshares') |
|
462 | + ->where($query->expr()->in('share_id', $query->createFunction('(' . $select . ')'))); |
|
463 | + $query->execute(); |
|
464 | + |
|
465 | + $deleteReShares = $this->connection->getQueryBuilder(); |
|
466 | + $deleteReShares->delete('share') |
|
467 | + ->where($deleteReShares->expr()->eq('file_source', $deleteReShares->createNamedParameter($mountPointId))); |
|
468 | + $deleteReShares->execute(); |
|
469 | + } |
|
470 | + |
|
471 | + /** |
|
472 | + * remove all shares for user $uid if the user was deleted |
|
473 | + * |
|
474 | + * @param string $uid |
|
475 | + * @return bool |
|
476 | + */ |
|
477 | + public function removeUserShares($uid) { |
|
478 | + $getShare = $this->connection->prepare(' |
|
479 | 479 | SELECT `remote`, `share_token`, `remote_id` |
480 | 480 | FROM `*PREFIX*share_external` |
481 | 481 | WHERE `user` = ?'); |
482 | - $result = $getShare->execute(array($uid)); |
|
482 | + $result = $getShare->execute(array($uid)); |
|
483 | 483 | |
484 | - if ($result) { |
|
485 | - $shares = $getShare->fetchAll(); |
|
486 | - foreach($shares as $share) { |
|
487 | - $this->sendFeedbackToRemote($share['remote'], $share['share_token'], $share['remote_id'], 'decline'); |
|
488 | - } |
|
489 | - } |
|
484 | + if ($result) { |
|
485 | + $shares = $getShare->fetchAll(); |
|
486 | + foreach($shares as $share) { |
|
487 | + $this->sendFeedbackToRemote($share['remote'], $share['share_token'], $share['remote_id'], 'decline'); |
|
488 | + } |
|
489 | + } |
|
490 | 490 | |
491 | - $query = $this->connection->prepare(' |
|
491 | + $query = $this->connection->prepare(' |
|
492 | 492 | DELETE FROM `*PREFIX*share_external` |
493 | 493 | WHERE `user` = ? |
494 | 494 | '); |
495 | - return (bool)$query->execute(array($uid)); |
|
496 | - } |
|
497 | - |
|
498 | - /** |
|
499 | - * return a list of shares which are not yet accepted by the user |
|
500 | - * |
|
501 | - * @return array list of open server-to-server shares |
|
502 | - */ |
|
503 | - public function getOpenShares() { |
|
504 | - return $this->getShares(false); |
|
505 | - } |
|
506 | - |
|
507 | - /** |
|
508 | - * return a list of shares which are accepted by the user |
|
509 | - * |
|
510 | - * @return array list of accepted server-to-server shares |
|
511 | - */ |
|
512 | - public function getAcceptedShares() { |
|
513 | - return $this->getShares(true); |
|
514 | - } |
|
515 | - |
|
516 | - /** |
|
517 | - * return a list of shares for the user |
|
518 | - * |
|
519 | - * @param bool|null $accepted True for accepted only, |
|
520 | - * false for not accepted, |
|
521 | - * null for all shares of the user |
|
522 | - * @return array list of open server-to-server shares |
|
523 | - */ |
|
524 | - private function getShares($accepted) { |
|
525 | - $query = 'SELECT `id`, `remote`, `remote_id`, `share_token`, `name`, `owner`, `user`, `mountpoint`, `accepted` |
|
495 | + return (bool)$query->execute(array($uid)); |
|
496 | + } |
|
497 | + |
|
498 | + /** |
|
499 | + * return a list of shares which are not yet accepted by the user |
|
500 | + * |
|
501 | + * @return array list of open server-to-server shares |
|
502 | + */ |
|
503 | + public function getOpenShares() { |
|
504 | + return $this->getShares(false); |
|
505 | + } |
|
506 | + |
|
507 | + /** |
|
508 | + * return a list of shares which are accepted by the user |
|
509 | + * |
|
510 | + * @return array list of accepted server-to-server shares |
|
511 | + */ |
|
512 | + public function getAcceptedShares() { |
|
513 | + return $this->getShares(true); |
|
514 | + } |
|
515 | + |
|
516 | + /** |
|
517 | + * return a list of shares for the user |
|
518 | + * |
|
519 | + * @param bool|null $accepted True for accepted only, |
|
520 | + * false for not accepted, |
|
521 | + * null for all shares of the user |
|
522 | + * @return array list of open server-to-server shares |
|
523 | + */ |
|
524 | + private function getShares($accepted) { |
|
525 | + $query = 'SELECT `id`, `remote`, `remote_id`, `share_token`, `name`, `owner`, `user`, `mountpoint`, `accepted` |
|
526 | 526 | FROM `*PREFIX*share_external` |
527 | 527 | WHERE `user` = ?'; |
528 | - $parameters = [$this->uid]; |
|
529 | - if (!is_null($accepted)) { |
|
530 | - $query .= ' AND `accepted` = ?'; |
|
531 | - $parameters[] = (int) $accepted; |
|
532 | - } |
|
533 | - $query .= ' ORDER BY `id` ASC'; |
|
534 | - |
|
535 | - $shares = $this->connection->prepare($query); |
|
536 | - $result = $shares->execute($parameters); |
|
537 | - |
|
538 | - return $result ? $shares->fetchAll() : []; |
|
539 | - } |
|
528 | + $parameters = [$this->uid]; |
|
529 | + if (!is_null($accepted)) { |
|
530 | + $query .= ' AND `accepted` = ?'; |
|
531 | + $parameters[] = (int) $accepted; |
|
532 | + } |
|
533 | + $query .= ' ORDER BY `id` ASC'; |
|
534 | + |
|
535 | + $shares = $this->connection->prepare($query); |
|
536 | + $result = $shares->execute($parameters); |
|
537 | + |
|
538 | + return $result ? $shares->fetchAll() : []; |
|
539 | + } |
|
540 | 540 | } |
@@ -25,42 +25,42 @@ |
||
25 | 25 | |
26 | 26 | class BadRequestException extends HintException { |
27 | 27 | |
28 | - private $parameterList; |
|
28 | + private $parameterList; |
|
29 | 29 | |
30 | - /** |
|
31 | - * BadRequestException constructor. |
|
32 | - * |
|
33 | - * @param array $missingParameters |
|
34 | - */ |
|
35 | - public function __construct(array $missingParameters) { |
|
36 | - $l = \OC::$server->getL10N('federation'); |
|
37 | - $this->parameterList = $missingParameters; |
|
38 | - $parameterList = implode(',', $missingParameters); |
|
39 | - $message = 'Parameters missing in order to complete the request. Missing Parameters: ' . $parameterList; |
|
40 | - $hint = $l->t('Parameters missing in order to complete the request. Missing Parameters: "%s"', [$parameterList]); |
|
41 | - parent::__construct($message, $hint); |
|
42 | - } |
|
30 | + /** |
|
31 | + * BadRequestException constructor. |
|
32 | + * |
|
33 | + * @param array $missingParameters |
|
34 | + */ |
|
35 | + public function __construct(array $missingParameters) { |
|
36 | + $l = \OC::$server->getL10N('federation'); |
|
37 | + $this->parameterList = $missingParameters; |
|
38 | + $parameterList = implode(',', $missingParameters); |
|
39 | + $message = 'Parameters missing in order to complete the request. Missing Parameters: ' . $parameterList; |
|
40 | + $hint = $l->t('Parameters missing in order to complete the request. Missing Parameters: "%s"', [$parameterList]); |
|
41 | + parent::__construct($message, $hint); |
|
42 | + } |
|
43 | 43 | |
44 | - /** |
|
45 | - * get array with the return message as defined in the OCM API |
|
46 | - * |
|
47 | - * @return array |
|
48 | - */ |
|
49 | - public function getReturnMessage() { |
|
50 | - $result = [ |
|
51 | - 'message' => 'RESOURCE_NOT_FOUND', |
|
52 | - 'validationErrors' =>[ |
|
53 | - ] |
|
54 | - ]; |
|
44 | + /** |
|
45 | + * get array with the return message as defined in the OCM API |
|
46 | + * |
|
47 | + * @return array |
|
48 | + */ |
|
49 | + public function getReturnMessage() { |
|
50 | + $result = [ |
|
51 | + 'message' => 'RESOURCE_NOT_FOUND', |
|
52 | + 'validationErrors' =>[ |
|
53 | + ] |
|
54 | + ]; |
|
55 | 55 | |
56 | - foreach ($this->parameterList as $missingParameter) { |
|
57 | - $result['validationErrors'] = [ |
|
58 | - 'name' => $missingParameter, |
|
59 | - 'message' => 'NOT_FOUND' |
|
60 | - ]; |
|
61 | - } |
|
56 | + foreach ($this->parameterList as $missingParameter) { |
|
57 | + $result['validationErrors'] = [ |
|
58 | + 'name' => $missingParameter, |
|
59 | + 'message' => 'NOT_FOUND' |
|
60 | + ]; |
|
61 | + } |
|
62 | 62 | |
63 | - return $result; |
|
64 | - } |
|
63 | + return $result; |
|
64 | + } |
|
65 | 65 | |
66 | 66 | } |
@@ -25,16 +25,16 @@ |
||
25 | 25 | |
26 | 26 | class AuthenticationFailedException extends HintException { |
27 | 27 | |
28 | - /** |
|
29 | - * BadRequestException constructor. |
|
30 | - * |
|
31 | - * @param array $missingParameters |
|
32 | - */ |
|
33 | - public function __construct() { |
|
34 | - $l = \OC::$server->getL10N('federation'); |
|
35 | - $message = 'Authentication failed, wrong token or provider ID given'; |
|
36 | - $hint = $l->t('Authentication failed, wrong token or provider ID given'); |
|
37 | - parent::__construct($message, $hint); |
|
38 | - } |
|
28 | + /** |
|
29 | + * BadRequestException constructor. |
|
30 | + * |
|
31 | + * @param array $missingParameters |
|
32 | + */ |
|
33 | + public function __construct() { |
|
34 | + $l = \OC::$server->getL10N('federation'); |
|
35 | + $message = 'Authentication failed, wrong token or provider ID given'; |
|
36 | + $hint = $l->t('Authentication failed, wrong token or provider ID given'); |
|
37 | + parent::__construct($message, $hint); |
|
38 | + } |
|
39 | 39 | |
40 | 40 | } |
@@ -39,41 +39,41 @@ |
||
39 | 39 | |
40 | 40 | interface ICloudFederationProvider { |
41 | 41 | |
42 | - /** |
|
43 | - * get the name of the share type, handled by this provider |
|
44 | - * |
|
45 | - * @return string |
|
46 | - * |
|
47 | - * @since 14.0.0 |
|
48 | - */ |
|
49 | - public function getShareType(); |
|
42 | + /** |
|
43 | + * get the name of the share type, handled by this provider |
|
44 | + * |
|
45 | + * @return string |
|
46 | + * |
|
47 | + * @since 14.0.0 |
|
48 | + */ |
|
49 | + public function getShareType(); |
|
50 | 50 | |
51 | - /** |
|
52 | - * share received from another server |
|
53 | - * |
|
54 | - * @param ICloudFederationShare $share |
|
55 | - * @return string provider specific unique ID of the share |
|
56 | - * |
|
57 | - * @throws ProviderCouldNotAddShareException |
|
58 | - * |
|
59 | - * @since 14.0.0 |
|
60 | - */ |
|
61 | - public function shareReceived(ICloudFederationShare $share); |
|
51 | + /** |
|
52 | + * share received from another server |
|
53 | + * |
|
54 | + * @param ICloudFederationShare $share |
|
55 | + * @return string provider specific unique ID of the share |
|
56 | + * |
|
57 | + * @throws ProviderCouldNotAddShareException |
|
58 | + * |
|
59 | + * @since 14.0.0 |
|
60 | + */ |
|
61 | + public function shareReceived(ICloudFederationShare $share); |
|
62 | 62 | |
63 | - /** |
|
64 | - * notification received from another server |
|
65 | - * |
|
66 | - * @param string $notificationType (e.g SHARE_ACCEPTED) |
|
67 | - * @param string $providerId share ID |
|
68 | - * @param array $notification provider specific notification |
|
69 | - * |
|
70 | - * @throws ShareNotFoundException |
|
71 | - * @throws ActionNotSupportedException |
|
72 | - * @throws BadRequestException |
|
73 | - * @throws AuthenticationFailedException |
|
74 | - * |
|
75 | - * @since 14.0.0 |
|
76 | - */ |
|
77 | - public function notificationReceived($notificationType, $providerId, array $notification); |
|
63 | + /** |
|
64 | + * notification received from another server |
|
65 | + * |
|
66 | + * @param string $notificationType (e.g SHARE_ACCEPTED) |
|
67 | + * @param string $providerId share ID |
|
68 | + * @param array $notification provider specific notification |
|
69 | + * |
|
70 | + * @throws ShareNotFoundException |
|
71 | + * @throws ActionNotSupportedException |
|
72 | + * @throws BadRequestException |
|
73 | + * @throws AuthenticationFailedException |
|
74 | + * |
|
75 | + * @since 14.0.0 |
|
76 | + */ |
|
77 | + public function notificationReceived($notificationType, $providerId, array $notification); |
|
78 | 78 | |
79 | 79 | } |
@@ -36,172 +36,172 @@ |
||
36 | 36 | */ |
37 | 37 | interface IShareProvider { |
38 | 38 | |
39 | - /** |
|
40 | - * Return the identifier of this provider. |
|
41 | - * |
|
42 | - * @return string Containing only [a-zA-Z0-9] |
|
43 | - * @since 9.0.0 |
|
44 | - */ |
|
45 | - public function identifier(); |
|
46 | - |
|
47 | - /** |
|
48 | - * Create a share |
|
49 | - * |
|
50 | - * @param \OCP\Share\IShare $share |
|
51 | - * @return \OCP\Share\IShare The share object |
|
52 | - * @since 9.0.0 |
|
53 | - */ |
|
54 | - public function create(\OCP\Share\IShare $share); |
|
55 | - |
|
56 | - /** |
|
57 | - * Update a share |
|
58 | - * |
|
59 | - * @param \OCP\Share\IShare $share |
|
60 | - * @return \OCP\Share\IShare The share object |
|
61 | - * @since 9.0.0 |
|
62 | - */ |
|
63 | - public function update(\OCP\Share\IShare $share); |
|
64 | - |
|
65 | - /** |
|
66 | - * Delete a share |
|
67 | - * |
|
68 | - * @param \OCP\Share\IShare $share |
|
69 | - * @since 9.0.0 |
|
70 | - */ |
|
71 | - public function delete(\OCP\Share\IShare $share); |
|
72 | - |
|
73 | - /** |
|
74 | - * Unshare a file from self as recipient. |
|
75 | - * This may require special handling. If a user unshares a group |
|
76 | - * share from their self then the original group share should still exist. |
|
77 | - * |
|
78 | - * @param \OCP\Share\IShare $share |
|
79 | - * @param string $recipient UserId of the recipient |
|
80 | - * @since 9.0.0 |
|
81 | - */ |
|
82 | - public function deleteFromSelf(\OCP\Share\IShare $share, $recipient); |
|
83 | - |
|
84 | - /** |
|
85 | - * Move a share as a recipient. |
|
86 | - * This is updating the share target. Thus the mount point of the recipient. |
|
87 | - * This may require special handling. If a user moves a group share |
|
88 | - * the target should only be changed for them. |
|
89 | - * |
|
90 | - * @param \OCP\Share\IShare $share |
|
91 | - * @param string $recipient userId of recipient |
|
92 | - * @return \OCP\Share\IShare |
|
93 | - * @since 9.0.0 |
|
94 | - */ |
|
95 | - public function move(\OCP\Share\IShare $share, $recipient); |
|
96 | - |
|
97 | - /** |
|
98 | - * Get all shares by the given user in a folder |
|
99 | - * |
|
100 | - * @param string $userId |
|
101 | - * @param Folder $node |
|
102 | - * @param bool $reshares Also get the shares where $user is the owner instead of just the shares where $user is the initiator |
|
103 | - * @return \OCP\Share\IShare[] |
|
104 | - * @since 11.0.0 |
|
105 | - */ |
|
106 | - public function getSharesInFolder($userId, Folder $node, $reshares); |
|
107 | - |
|
108 | - /** |
|
109 | - * Get all shares by the given user |
|
110 | - * |
|
111 | - * @param string $userId |
|
112 | - * @param int $shareType |
|
113 | - * @param Node|null $node |
|
114 | - * @param bool $reshares Also get the shares where $user is the owner instead of just the shares where $user is the initiator |
|
115 | - * @param int $limit The maximum number of shares to be returned, -1 for all shares |
|
116 | - * @param int $offset |
|
117 | - * @return \OCP\Share\IShare[] |
|
118 | - * @since 9.0.0 |
|
119 | - */ |
|
120 | - public function getSharesBy($userId, $shareType, $node, $reshares, $limit, $offset); |
|
121 | - |
|
122 | - /** |
|
123 | - * Get share by id |
|
124 | - * |
|
125 | - * @param int $id |
|
126 | - * @param string|null $recipientId |
|
127 | - * @return \OCP\Share\IShare |
|
128 | - * @throws ShareNotFoundException |
|
129 | - * @since 9.0.0 |
|
130 | - */ |
|
131 | - public function getShareById($id, $recipientId = null); |
|
132 | - |
|
133 | - /** |
|
134 | - * Get shares for a given path |
|
135 | - * |
|
136 | - * @param Node $path |
|
137 | - * @return \OCP\Share\IShare[] |
|
138 | - * @since 9.0.0 |
|
139 | - */ |
|
140 | - public function getSharesByPath(Node $path); |
|
141 | - |
|
142 | - /** |
|
143 | - * Get shared with the given user |
|
144 | - * |
|
145 | - * @param string $userId get shares where this user is the recipient |
|
146 | - * @param int $shareType |
|
147 | - * @param Node|null $node |
|
148 | - * @param int $limit The max number of entries returned, -1 for all |
|
149 | - * @param int $offset |
|
150 | - * @return \OCP\Share\IShare[] |
|
151 | - * @since 9.0.0 |
|
152 | - */ |
|
153 | - public function getSharedWith($userId, $shareType, $node, $limit, $offset); |
|
154 | - |
|
155 | - /** |
|
156 | - * Get a share by token |
|
157 | - * |
|
158 | - * @param string $token |
|
159 | - * @return \OCP\Share\IShare |
|
160 | - * @throws ShareNotFound |
|
161 | - * @since 9.0.0 |
|
162 | - */ |
|
163 | - public function getShareByToken($token); |
|
164 | - |
|
165 | - /** |
|
166 | - * A user is deleted from the system |
|
167 | - * So clean up the relevant shares. |
|
168 | - * |
|
169 | - * @param string $uid |
|
170 | - * @param int $shareType |
|
171 | - * @since 9.1.0 |
|
172 | - */ |
|
173 | - public function userDeleted($uid, $shareType); |
|
174 | - |
|
175 | - /** |
|
176 | - * A group is deleted from the system. |
|
177 | - * We have to clean up all shares to this group. |
|
178 | - * Providers not handling group shares should just return |
|
179 | - * |
|
180 | - * @param string $gid |
|
181 | - * @since 9.1.0 |
|
182 | - */ |
|
183 | - public function groupDeleted($gid); |
|
184 | - |
|
185 | - /** |
|
186 | - * A user is deleted from a group |
|
187 | - * We have to clean up all the related user specific group shares |
|
188 | - * Providers not handling group shares should just return |
|
189 | - * |
|
190 | - * @param string $uid |
|
191 | - * @param string $gid |
|
192 | - * @since 9.1.0 |
|
193 | - */ |
|
194 | - public function userDeletedFromGroup($uid, $gid); |
|
195 | - |
|
196 | - /** |
|
197 | - * Get the access list to the array of provided nodes. |
|
198 | - * |
|
199 | - * @see IManager::getAccessList() for sample docs |
|
200 | - * |
|
201 | - * @param Node[] $nodes The list of nodes to get access for |
|
202 | - * @param bool $currentAccess If current access is required (like for removed shares that might get revived later) |
|
203 | - * @return array |
|
204 | - * @since 12 |
|
205 | - */ |
|
206 | - public function getAccessList($nodes, $currentAccess); |
|
39 | + /** |
|
40 | + * Return the identifier of this provider. |
|
41 | + * |
|
42 | + * @return string Containing only [a-zA-Z0-9] |
|
43 | + * @since 9.0.0 |
|
44 | + */ |
|
45 | + public function identifier(); |
|
46 | + |
|
47 | + /** |
|
48 | + * Create a share |
|
49 | + * |
|
50 | + * @param \OCP\Share\IShare $share |
|
51 | + * @return \OCP\Share\IShare The share object |
|
52 | + * @since 9.0.0 |
|
53 | + */ |
|
54 | + public function create(\OCP\Share\IShare $share); |
|
55 | + |
|
56 | + /** |
|
57 | + * Update a share |
|
58 | + * |
|
59 | + * @param \OCP\Share\IShare $share |
|
60 | + * @return \OCP\Share\IShare The share object |
|
61 | + * @since 9.0.0 |
|
62 | + */ |
|
63 | + public function update(\OCP\Share\IShare $share); |
|
64 | + |
|
65 | + /** |
|
66 | + * Delete a share |
|
67 | + * |
|
68 | + * @param \OCP\Share\IShare $share |
|
69 | + * @since 9.0.0 |
|
70 | + */ |
|
71 | + public function delete(\OCP\Share\IShare $share); |
|
72 | + |
|
73 | + /** |
|
74 | + * Unshare a file from self as recipient. |
|
75 | + * This may require special handling. If a user unshares a group |
|
76 | + * share from their self then the original group share should still exist. |
|
77 | + * |
|
78 | + * @param \OCP\Share\IShare $share |
|
79 | + * @param string $recipient UserId of the recipient |
|
80 | + * @since 9.0.0 |
|
81 | + */ |
|
82 | + public function deleteFromSelf(\OCP\Share\IShare $share, $recipient); |
|
83 | + |
|
84 | + /** |
|
85 | + * Move a share as a recipient. |
|
86 | + * This is updating the share target. Thus the mount point of the recipient. |
|
87 | + * This may require special handling. If a user moves a group share |
|
88 | + * the target should only be changed for them. |
|
89 | + * |
|
90 | + * @param \OCP\Share\IShare $share |
|
91 | + * @param string $recipient userId of recipient |
|
92 | + * @return \OCP\Share\IShare |
|
93 | + * @since 9.0.0 |
|
94 | + */ |
|
95 | + public function move(\OCP\Share\IShare $share, $recipient); |
|
96 | + |
|
97 | + /** |
|
98 | + * Get all shares by the given user in a folder |
|
99 | + * |
|
100 | + * @param string $userId |
|
101 | + * @param Folder $node |
|
102 | + * @param bool $reshares Also get the shares where $user is the owner instead of just the shares where $user is the initiator |
|
103 | + * @return \OCP\Share\IShare[] |
|
104 | + * @since 11.0.0 |
|
105 | + */ |
|
106 | + public function getSharesInFolder($userId, Folder $node, $reshares); |
|
107 | + |
|
108 | + /** |
|
109 | + * Get all shares by the given user |
|
110 | + * |
|
111 | + * @param string $userId |
|
112 | + * @param int $shareType |
|
113 | + * @param Node|null $node |
|
114 | + * @param bool $reshares Also get the shares where $user is the owner instead of just the shares where $user is the initiator |
|
115 | + * @param int $limit The maximum number of shares to be returned, -1 for all shares |
|
116 | + * @param int $offset |
|
117 | + * @return \OCP\Share\IShare[] |
|
118 | + * @since 9.0.0 |
|
119 | + */ |
|
120 | + public function getSharesBy($userId, $shareType, $node, $reshares, $limit, $offset); |
|
121 | + |
|
122 | + /** |
|
123 | + * Get share by id |
|
124 | + * |
|
125 | + * @param int $id |
|
126 | + * @param string|null $recipientId |
|
127 | + * @return \OCP\Share\IShare |
|
128 | + * @throws ShareNotFoundException |
|
129 | + * @since 9.0.0 |
|
130 | + */ |
|
131 | + public function getShareById($id, $recipientId = null); |
|
132 | + |
|
133 | + /** |
|
134 | + * Get shares for a given path |
|
135 | + * |
|
136 | + * @param Node $path |
|
137 | + * @return \OCP\Share\IShare[] |
|
138 | + * @since 9.0.0 |
|
139 | + */ |
|
140 | + public function getSharesByPath(Node $path); |
|
141 | + |
|
142 | + /** |
|
143 | + * Get shared with the given user |
|
144 | + * |
|
145 | + * @param string $userId get shares where this user is the recipient |
|
146 | + * @param int $shareType |
|
147 | + * @param Node|null $node |
|
148 | + * @param int $limit The max number of entries returned, -1 for all |
|
149 | + * @param int $offset |
|
150 | + * @return \OCP\Share\IShare[] |
|
151 | + * @since 9.0.0 |
|
152 | + */ |
|
153 | + public function getSharedWith($userId, $shareType, $node, $limit, $offset); |
|
154 | + |
|
155 | + /** |
|
156 | + * Get a share by token |
|
157 | + * |
|
158 | + * @param string $token |
|
159 | + * @return \OCP\Share\IShare |
|
160 | + * @throws ShareNotFound |
|
161 | + * @since 9.0.0 |
|
162 | + */ |
|
163 | + public function getShareByToken($token); |
|
164 | + |
|
165 | + /** |
|
166 | + * A user is deleted from the system |
|
167 | + * So clean up the relevant shares. |
|
168 | + * |
|
169 | + * @param string $uid |
|
170 | + * @param int $shareType |
|
171 | + * @since 9.1.0 |
|
172 | + */ |
|
173 | + public function userDeleted($uid, $shareType); |
|
174 | + |
|
175 | + /** |
|
176 | + * A group is deleted from the system. |
|
177 | + * We have to clean up all shares to this group. |
|
178 | + * Providers not handling group shares should just return |
|
179 | + * |
|
180 | + * @param string $gid |
|
181 | + * @since 9.1.0 |
|
182 | + */ |
|
183 | + public function groupDeleted($gid); |
|
184 | + |
|
185 | + /** |
|
186 | + * A user is deleted from a group |
|
187 | + * We have to clean up all the related user specific group shares |
|
188 | + * Providers not handling group shares should just return |
|
189 | + * |
|
190 | + * @param string $uid |
|
191 | + * @param string $gid |
|
192 | + * @since 9.1.0 |
|
193 | + */ |
|
194 | + public function userDeletedFromGroup($uid, $gid); |
|
195 | + |
|
196 | + /** |
|
197 | + * Get the access list to the array of provided nodes. |
|
198 | + * |
|
199 | + * @see IManager::getAccessList() for sample docs |
|
200 | + * |
|
201 | + * @param Node[] $nodes The list of nodes to get access for |
|
202 | + * @param bool $currentAccess If current access is required (like for removed shares that might get revived later) |
|
203 | + * @return array |
|
204 | + * @since 12 |
|
205 | + */ |
|
206 | + public function getAccessList($nodes, $currentAccess); |
|
207 | 207 | } |
@@ -42,190 +42,190 @@ |
||
42 | 42 | */ |
43 | 43 | class CloudFederationProviderManager implements ICloudFederationProviderManager { |
44 | 44 | |
45 | - /** @var array list of available cloud federation providers */ |
|
46 | - private $cloudFederationProvider; |
|
47 | - |
|
48 | - /** @var IAppManager */ |
|
49 | - private $appManager; |
|
50 | - |
|
51 | - /** @var IClientService */ |
|
52 | - private $httpClientService; |
|
53 | - |
|
54 | - /** @var ICloudIdManager */ |
|
55 | - private $cloudIdManager; |
|
56 | - |
|
57 | - /** @var ILogger */ |
|
58 | - private $logger; |
|
59 | - |
|
60 | - private $supportedAPIVersion = '1.0-proposal1'; |
|
61 | - |
|
62 | - /** |
|
63 | - * CloudFederationProviderManager constructor. |
|
64 | - * |
|
65 | - * @param IAppManager $appManager |
|
66 | - * @param IClientService $httpClientService |
|
67 | - * @param ICloudIdManager $cloudIdManager |
|
68 | - * @param ILogger $logger |
|
69 | - */ |
|
70 | - public function __construct(IAppManager $appManager, |
|
71 | - IClientService $httpClientService, |
|
72 | - ICloudIdManager $cloudIdManager, |
|
73 | - ILogger $logger) { |
|
74 | - $this->cloudFederationProvider= []; |
|
75 | - $this->appManager = $appManager; |
|
76 | - $this->httpClientService = $httpClientService; |
|
77 | - $this->cloudIdManager = $cloudIdManager; |
|
78 | - $this->logger = $logger; |
|
79 | - } |
|
80 | - |
|
81 | - |
|
82 | - /** |
|
83 | - * Registers an callback function which must return an cloud federation provider |
|
84 | - * |
|
85 | - * @param string $shareType which share type does the provider handles |
|
86 | - * @param string $displayName user facing name of the federated share provider |
|
87 | - * @param callable $callback |
|
88 | - */ |
|
89 | - public function addCloudFederationProvider($shareType, $displayName, callable $callback) { |
|
90 | - \OC::$server->getRemoteApiFactory(); |
|
91 | - |
|
92 | - $this->cloudFederationProvider[$shareType] = [ |
|
93 | - 'shareType' => $shareType, |
|
94 | - 'displayName' => $displayName, |
|
95 | - 'callback' => $callback, |
|
96 | - ]; |
|
97 | - |
|
98 | - } |
|
99 | - |
|
100 | - /** |
|
101 | - * remove cloud federation provider |
|
102 | - * |
|
103 | - * @param string $providerId |
|
104 | - */ |
|
105 | - public function removeCloudFederationProvider($providerId) { |
|
106 | - unset($this->cloudFederationProvider[$providerId]); |
|
107 | - } |
|
108 | - |
|
109 | - /** |
|
110 | - * get a list of all cloudFederationProviders |
|
111 | - * |
|
112 | - * @return array [id => ['id' => $id, 'displayName' => $displayName, 'callback' => callback]] |
|
113 | - */ |
|
114 | - public function getAllCloudFederationProviders() { |
|
115 | - return $this->cloudFederationProvider; |
|
116 | - } |
|
117 | - |
|
118 | - /** |
|
119 | - * get a specific cloud federation provider |
|
120 | - * |
|
121 | - * @param string $shareType |
|
122 | - * @return ICloudFederationProvider |
|
123 | - * @throws ProviderDoesNotExistsException |
|
124 | - */ |
|
125 | - public function getCloudFederationProvider($shareType) { |
|
126 | - if (isset($this->cloudFederationProvider[$shareType])) { |
|
127 | - return call_user_func($this->cloudFederationProvider[$shareType]['callback']); |
|
128 | - } else { |
|
129 | - throw new ProviderDoesNotExistsException($shareType); |
|
130 | - } |
|
131 | - } |
|
132 | - |
|
133 | - public function sendShare(ICloudFederationShare $share) { |
|
134 | - $cloudID = $this->cloudIdManager->resolveCloudId($share->getShareWith()); |
|
135 | - $ocmEndPoint = $this->getOCMEndPoint($cloudID->getRemote()); |
|
136 | - |
|
137 | - if (empty($ocmEndPoint)) { |
|
138 | - return false; |
|
139 | - } |
|
140 | - |
|
141 | - $client = $this->httpClientService->newClient(); |
|
142 | - try { |
|
143 | - $response = $client->post($ocmEndPoint . '/shares', [ |
|
144 | - 'body' => $share->getShare(), |
|
145 | - 'timeout' => 10, |
|
146 | - 'connect_timeout' => 10, |
|
147 | - ]); |
|
148 | - |
|
149 | - if ($response->getStatusCode() === Http::STATUS_CREATED) { |
|
150 | - return true; |
|
151 | - } |
|
152 | - |
|
153 | - } catch (\Exception $e) { |
|
154 | - // if flat re-sharing is not supported by the remote server |
|
155 | - // we re-throw the exception and fall back to the old behaviour. |
|
156 | - // (flat re-shares has been introduced in Nextcloud 9.1) |
|
157 | - if ($e->getCode() === Http::STATUS_INTERNAL_SERVER_ERROR) { |
|
158 | - throw $e; |
|
159 | - } |
|
160 | - } |
|
161 | - |
|
162 | - return false; |
|
163 | - |
|
164 | - } |
|
165 | - |
|
166 | - /** |
|
167 | - * @param string $url |
|
168 | - * @param ICloudFederationNotification $notification |
|
169 | - * @return bool |
|
170 | - */ |
|
171 | - public function sendNotification($url, ICloudFederationNotification $notification) { |
|
172 | - $ocmEndPoint = $this->getOCMEndPoint($url); |
|
173 | - |
|
174 | - if (empty($ocmEndPoint)) { |
|
175 | - return false; |
|
176 | - } |
|
177 | - |
|
178 | - $client = $this->httpClientService->newClient(); |
|
179 | - try { |
|
180 | - $response = $client->post($ocmEndPoint . '/notifications', [ |
|
181 | - 'body' => $notification->getMessage(), |
|
182 | - 'timeout' => 10, |
|
183 | - 'connect_timeout' => 10, |
|
184 | - ]); |
|
185 | - if ($response->getStatusCode() === Http::STATUS_OK) { |
|
186 | - return true; |
|
187 | - } |
|
188 | - } catch (\Exception $e) { |
|
189 | - // log the error and return false |
|
190 | - $this->logger->error('error while sending notification for federated share: ' . $e->getMessage()); |
|
191 | - } |
|
192 | - |
|
193 | - return false; |
|
194 | - } |
|
195 | - |
|
196 | - /** |
|
197 | - * check if the new cloud federation API is ready to be used |
|
198 | - * |
|
199 | - * @return bool |
|
200 | - */ |
|
201 | - public function isReady() { |
|
202 | - return $this->appManager->isEnabledForUser('cloud_federation_api', false); |
|
203 | - } |
|
204 | - /** |
|
205 | - * check if server supports the new OCM api and ask for the correct end-point |
|
206 | - * |
|
207 | - * @param string $url full base URL of the cloud server |
|
208 | - * @return string |
|
209 | - */ |
|
210 | - protected function getOCMEndPoint($url) { |
|
211 | - $client = $this->httpClientService->newClient(); |
|
212 | - try { |
|
213 | - $response = $client->get($url . '/ocm-provider/', ['timeout' => 10, 'connect_timeout' => 10]); |
|
214 | - } catch (\Exception $e) { |
|
215 | - return ''; |
|
216 | - } |
|
217 | - |
|
218 | - $result = $response->getBody(); |
|
219 | - $result = json_decode($result, true); |
|
220 | - |
|
221 | - $supportedVersion = isset($result['apiVersion']) && $result['apiVersion'] === $this->supportedAPIVersion; |
|
222 | - |
|
223 | - if (isset($result['endPoint']) && $supportedVersion) { |
|
224 | - return $result['endPoint']; |
|
225 | - } |
|
226 | - |
|
227 | - return ''; |
|
228 | - } |
|
45 | + /** @var array list of available cloud federation providers */ |
|
46 | + private $cloudFederationProvider; |
|
47 | + |
|
48 | + /** @var IAppManager */ |
|
49 | + private $appManager; |
|
50 | + |
|
51 | + /** @var IClientService */ |
|
52 | + private $httpClientService; |
|
53 | + |
|
54 | + /** @var ICloudIdManager */ |
|
55 | + private $cloudIdManager; |
|
56 | + |
|
57 | + /** @var ILogger */ |
|
58 | + private $logger; |
|
59 | + |
|
60 | + private $supportedAPIVersion = '1.0-proposal1'; |
|
61 | + |
|
62 | + /** |
|
63 | + * CloudFederationProviderManager constructor. |
|
64 | + * |
|
65 | + * @param IAppManager $appManager |
|
66 | + * @param IClientService $httpClientService |
|
67 | + * @param ICloudIdManager $cloudIdManager |
|
68 | + * @param ILogger $logger |
|
69 | + */ |
|
70 | + public function __construct(IAppManager $appManager, |
|
71 | + IClientService $httpClientService, |
|
72 | + ICloudIdManager $cloudIdManager, |
|
73 | + ILogger $logger) { |
|
74 | + $this->cloudFederationProvider= []; |
|
75 | + $this->appManager = $appManager; |
|
76 | + $this->httpClientService = $httpClientService; |
|
77 | + $this->cloudIdManager = $cloudIdManager; |
|
78 | + $this->logger = $logger; |
|
79 | + } |
|
80 | + |
|
81 | + |
|
82 | + /** |
|
83 | + * Registers an callback function which must return an cloud federation provider |
|
84 | + * |
|
85 | + * @param string $shareType which share type does the provider handles |
|
86 | + * @param string $displayName user facing name of the federated share provider |
|
87 | + * @param callable $callback |
|
88 | + */ |
|
89 | + public function addCloudFederationProvider($shareType, $displayName, callable $callback) { |
|
90 | + \OC::$server->getRemoteApiFactory(); |
|
91 | + |
|
92 | + $this->cloudFederationProvider[$shareType] = [ |
|
93 | + 'shareType' => $shareType, |
|
94 | + 'displayName' => $displayName, |
|
95 | + 'callback' => $callback, |
|
96 | + ]; |
|
97 | + |
|
98 | + } |
|
99 | + |
|
100 | + /** |
|
101 | + * remove cloud federation provider |
|
102 | + * |
|
103 | + * @param string $providerId |
|
104 | + */ |
|
105 | + public function removeCloudFederationProvider($providerId) { |
|
106 | + unset($this->cloudFederationProvider[$providerId]); |
|
107 | + } |
|
108 | + |
|
109 | + /** |
|
110 | + * get a list of all cloudFederationProviders |
|
111 | + * |
|
112 | + * @return array [id => ['id' => $id, 'displayName' => $displayName, 'callback' => callback]] |
|
113 | + */ |
|
114 | + public function getAllCloudFederationProviders() { |
|
115 | + return $this->cloudFederationProvider; |
|
116 | + } |
|
117 | + |
|
118 | + /** |
|
119 | + * get a specific cloud federation provider |
|
120 | + * |
|
121 | + * @param string $shareType |
|
122 | + * @return ICloudFederationProvider |
|
123 | + * @throws ProviderDoesNotExistsException |
|
124 | + */ |
|
125 | + public function getCloudFederationProvider($shareType) { |
|
126 | + if (isset($this->cloudFederationProvider[$shareType])) { |
|
127 | + return call_user_func($this->cloudFederationProvider[$shareType]['callback']); |
|
128 | + } else { |
|
129 | + throw new ProviderDoesNotExistsException($shareType); |
|
130 | + } |
|
131 | + } |
|
132 | + |
|
133 | + public function sendShare(ICloudFederationShare $share) { |
|
134 | + $cloudID = $this->cloudIdManager->resolveCloudId($share->getShareWith()); |
|
135 | + $ocmEndPoint = $this->getOCMEndPoint($cloudID->getRemote()); |
|
136 | + |
|
137 | + if (empty($ocmEndPoint)) { |
|
138 | + return false; |
|
139 | + } |
|
140 | + |
|
141 | + $client = $this->httpClientService->newClient(); |
|
142 | + try { |
|
143 | + $response = $client->post($ocmEndPoint . '/shares', [ |
|
144 | + 'body' => $share->getShare(), |
|
145 | + 'timeout' => 10, |
|
146 | + 'connect_timeout' => 10, |
|
147 | + ]); |
|
148 | + |
|
149 | + if ($response->getStatusCode() === Http::STATUS_CREATED) { |
|
150 | + return true; |
|
151 | + } |
|
152 | + |
|
153 | + } catch (\Exception $e) { |
|
154 | + // if flat re-sharing is not supported by the remote server |
|
155 | + // we re-throw the exception and fall back to the old behaviour. |
|
156 | + // (flat re-shares has been introduced in Nextcloud 9.1) |
|
157 | + if ($e->getCode() === Http::STATUS_INTERNAL_SERVER_ERROR) { |
|
158 | + throw $e; |
|
159 | + } |
|
160 | + } |
|
161 | + |
|
162 | + return false; |
|
163 | + |
|
164 | + } |
|
165 | + |
|
166 | + /** |
|
167 | + * @param string $url |
|
168 | + * @param ICloudFederationNotification $notification |
|
169 | + * @return bool |
|
170 | + */ |
|
171 | + public function sendNotification($url, ICloudFederationNotification $notification) { |
|
172 | + $ocmEndPoint = $this->getOCMEndPoint($url); |
|
173 | + |
|
174 | + if (empty($ocmEndPoint)) { |
|
175 | + return false; |
|
176 | + } |
|
177 | + |
|
178 | + $client = $this->httpClientService->newClient(); |
|
179 | + try { |
|
180 | + $response = $client->post($ocmEndPoint . '/notifications', [ |
|
181 | + 'body' => $notification->getMessage(), |
|
182 | + 'timeout' => 10, |
|
183 | + 'connect_timeout' => 10, |
|
184 | + ]); |
|
185 | + if ($response->getStatusCode() === Http::STATUS_OK) { |
|
186 | + return true; |
|
187 | + } |
|
188 | + } catch (\Exception $e) { |
|
189 | + // log the error and return false |
|
190 | + $this->logger->error('error while sending notification for federated share: ' . $e->getMessage()); |
|
191 | + } |
|
192 | + |
|
193 | + return false; |
|
194 | + } |
|
195 | + |
|
196 | + /** |
|
197 | + * check if the new cloud federation API is ready to be used |
|
198 | + * |
|
199 | + * @return bool |
|
200 | + */ |
|
201 | + public function isReady() { |
|
202 | + return $this->appManager->isEnabledForUser('cloud_federation_api', false); |
|
203 | + } |
|
204 | + /** |
|
205 | + * check if server supports the new OCM api and ask for the correct end-point |
|
206 | + * |
|
207 | + * @param string $url full base URL of the cloud server |
|
208 | + * @return string |
|
209 | + */ |
|
210 | + protected function getOCMEndPoint($url) { |
|
211 | + $client = $this->httpClientService->newClient(); |
|
212 | + try { |
|
213 | + $response = $client->get($url . '/ocm-provider/', ['timeout' => 10, 'connect_timeout' => 10]); |
|
214 | + } catch (\Exception $e) { |
|
215 | + return ''; |
|
216 | + } |
|
217 | + |
|
218 | + $result = $response->getBody(); |
|
219 | + $result = json_decode($result, true); |
|
220 | + |
|
221 | + $supportedVersion = isset($result['apiVersion']) && $result['apiVersion'] === $this->supportedAPIVersion; |
|
222 | + |
|
223 | + if (isset($result['endPoint']) && $supportedVersion) { |
|
224 | + return $result['endPoint']; |
|
225 | + } |
|
226 | + |
|
227 | + return ''; |
|
228 | + } |
|
229 | 229 | |
230 | 230 | |
231 | 231 | } |