|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @copyright Copyright (c) 2018 Bjoern Schiessle <[email protected]> |
|
4
|
|
|
* |
|
5
|
|
|
* @author Bjoern Schiessle <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* @license GNU AGPL version 3 or any later version |
|
8
|
|
|
* |
|
9
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
10
|
|
|
* it under the terms of the GNU Affero General Public License as |
|
11
|
|
|
* published by the Free Software Foundation, either version 3 of the |
|
12
|
|
|
* License, or (at your option) any later version. |
|
13
|
|
|
* |
|
14
|
|
|
* This program is distributed in the hope that it will be useful, |
|
15
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
16
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
17
|
|
|
* GNU Affero General Public License for more details. |
|
18
|
|
|
* |
|
19
|
|
|
* You should have received a copy of the GNU Affero General Public License |
|
20
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
21
|
|
|
* |
|
22
|
|
|
*/ |
|
23
|
|
|
|
|
24
|
|
|
namespace OCA\CloudFederationAPI\Controller; |
|
25
|
|
|
|
|
26
|
|
|
use OCA\CloudFederationAPI\Config; |
|
27
|
|
|
use OCP\AppFramework\Controller; |
|
28
|
|
|
use OCP\AppFramework\Http; |
|
29
|
|
|
use OCP\AppFramework\Http\JSONResponse; |
|
30
|
|
|
use OCP\Federation\Exceptions\ActionNotSupportedException; |
|
31
|
|
|
use OCP\Federation\Exceptions\AuthenticationFailedException; |
|
32
|
|
|
use OCP\Federation\Exceptions\BadRequestException; |
|
33
|
|
|
use OCP\Federation\Exceptions\ProviderCouldNotAddShareException; |
|
34
|
|
|
use OCP\Federation\ICloudFederationFactory; |
|
35
|
|
|
use OCP\Federation\ICloudFederationProviderManager; |
|
36
|
|
|
use OCP\Federation\Exceptions\ProviderDoesNotExistsException; |
|
37
|
|
|
use OCP\Federation\ICloudIdManager; |
|
38
|
|
|
use OCP\ILogger; |
|
39
|
|
|
use OCP\IRequest; |
|
40
|
|
|
use OCP\IURLGenerator; |
|
41
|
|
|
use OCP\IUserManager; |
|
42
|
|
|
use OCP\Share\Exceptions\ShareNotFound; |
|
43
|
|
|
|
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Class RequestHandlerController |
|
47
|
|
|
* |
|
48
|
|
|
* handle API between different Cloud instances |
|
49
|
|
|
* |
|
50
|
|
|
* @package OCA\CloudFederationAPI\Controller |
|
51
|
|
|
*/ |
|
52
|
|
|
class RequestHandlerController extends Controller { |
|
53
|
|
|
|
|
54
|
|
|
/** @var ILogger */ |
|
55
|
|
|
private $logger; |
|
56
|
|
|
|
|
57
|
|
|
/** @var IUserManager */ |
|
58
|
|
|
private $userManager; |
|
59
|
|
|
|
|
60
|
|
|
/** @var IURLGenerator */ |
|
61
|
|
|
private $urlGenerator; |
|
62
|
|
|
|
|
63
|
|
|
/** @var ICloudFederationProviderManager */ |
|
64
|
|
|
private $cloudFederationProviderManager; |
|
65
|
|
|
|
|
66
|
|
|
/** @var Config */ |
|
67
|
|
|
private $config; |
|
68
|
|
|
|
|
69
|
|
|
/** @var ICloudFederationFactory */ |
|
70
|
|
|
private $factory; |
|
71
|
|
|
|
|
72
|
|
|
/** @var ICloudIdManager */ |
|
73
|
|
|
private $cloudIdManager; |
|
74
|
|
|
|
|
75
|
|
|
public function __construct($appName, |
|
76
|
|
|
IRequest $request, |
|
77
|
|
|
ILogger $logger, |
|
78
|
|
|
IUserManager $userManager, |
|
79
|
|
|
IURLGenerator $urlGenerator, |
|
80
|
|
|
ICloudFederationProviderManager $cloudFederationProviderManager, |
|
81
|
|
|
Config $config, |
|
82
|
|
|
ICloudFederationFactory $factory, |
|
83
|
|
|
ICloudIdManager $cloudIdManager |
|
84
|
|
|
) { |
|
85
|
|
|
parent::__construct($appName, $request); |
|
86
|
|
|
|
|
87
|
|
|
$this->logger = $logger; |
|
88
|
|
|
$this->userManager = $userManager; |
|
89
|
|
|
$this->urlGenerator = $urlGenerator; |
|
90
|
|
|
$this->cloudFederationProviderManager = $cloudFederationProviderManager; |
|
91
|
|
|
$this->config = $config; |
|
92
|
|
|
$this->factory = $factory; |
|
93
|
|
|
$this->cloudIdManager = $cloudIdManager; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* add share |
|
98
|
|
|
* |
|
99
|
|
|
* @NoCSRFRequired |
|
100
|
|
|
* @PublicPage |
|
101
|
|
|
* @BruteForceProtection(action=receiveFederatedShare) |
|
102
|
|
|
* |
|
103
|
|
|
* @param string $shareWith |
|
104
|
|
|
* @param string $name resource name (e.g. document.odt) |
|
105
|
|
|
* @param string $description share description (optional) |
|
106
|
|
|
* @param string $providerId resource UID on the provider side |
|
107
|
|
|
* @param string $owner provider specific UID of the user who owns the resource |
|
108
|
|
|
* @param string $ownerDisplayName display name of the user who shared the item |
|
109
|
|
|
* @param string $sharedBy provider specific UID of the user who shared the resource |
|
110
|
|
|
* @param string $sharedByDisplayName display name of the user who shared the resource |
|
111
|
|
|
* @param array $protocol (e,.g. ['name' => 'webdav', 'options' => ['username' => 'john', 'permissions' => 31]]) |
|
112
|
|
|
* @param string $shareType ('group' or 'user' share) |
|
113
|
|
|
* @param $resourceType ('file', 'calendar',...) |
|
114
|
|
|
* @return Http\DataResponse|JSONResponse |
|
115
|
|
|
* |
|
116
|
|
|
* Example: curl -H "Content-Type: application/json" -X POST -d '{"shareWith":"admin1@serve1","name":"welcome server2.txt","description":"desc","providerId":"2","owner":"admin2@http://localhost/server2","ownerDisplayName":"admin2 display","shareType":"user","resourceType":"file","protocol":{"name":"webdav","options":{"sharedSecret":"secret","permissions":"webdav-property"}}}' http://localhost/server/index.php/ocm/shares |
|
117
|
|
|
*/ |
|
118
|
|
|
public function addShare($shareWith, $name, $description, $providerId, $owner, $ownerDisplayName, $sharedBy, $sharedByDisplayName, $protocol, $shareType, $resourceType) { |
|
119
|
|
|
|
|
120
|
|
|
// check if all required parameters are set |
|
121
|
|
|
if ($shareWith === null || |
|
122
|
|
|
$name === null || |
|
123
|
|
|
$providerId === null || |
|
124
|
|
|
$owner === null || |
|
125
|
|
|
$resourceType === null || |
|
126
|
|
|
$shareType === null || |
|
127
|
|
|
!is_array($protocol) || |
|
128
|
|
|
!isset($protocol['name']) || |
|
129
|
|
|
!isset ($protocol['options']) || |
|
130
|
|
|
!is_array($protocol['options']) || |
|
131
|
|
|
!isset($protocol['options']['sharedSecret']) |
|
132
|
|
|
) { |
|
133
|
|
|
return new JSONResponse( |
|
134
|
|
|
['message' => 'Missing arguments'], |
|
135
|
|
|
Http::STATUS_BAD_REQUEST |
|
136
|
|
|
); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
$cloudId = $this->cloudIdManager->resolveCloudId($shareWith); |
|
140
|
|
|
$shareWithLocalId = $cloudId->getUser(); |
|
141
|
|
|
$shareWith = $this->mapUid($shareWithLocalId); |
|
142
|
|
|
|
|
143
|
|
|
if (!$this->userManager->userExists($shareWith)) { |
|
144
|
|
|
return new JSONResponse( |
|
145
|
|
|
['message' => 'User "' . $shareWith . '" does not exists at ' . $this->urlGenerator->getBaseUrl()], |
|
146
|
|
|
Http::STATUS_BAD_REQUEST |
|
147
|
|
|
); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
// if no explicit display name is given, we use the uid as display name |
|
151
|
|
|
$ownerDisplayName = $ownerDisplayName === null ? $owner : $ownerDisplayName; |
|
152
|
|
|
$sharedByDisplayName = $sharedByDisplayName === null ? $sharedBy : $sharedByDisplayName; |
|
153
|
|
|
|
|
154
|
|
|
// sharedBy* parameter is optional, if nothing is set we assume that it is the same user as the owner |
|
155
|
|
|
if ($sharedBy === null) { |
|
156
|
|
|
$sharedBy = $owner; |
|
157
|
|
|
$sharedByDisplayName = $ownerDisplayName; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
try { |
|
161
|
|
|
$provider = $this->cloudFederationProviderManager->getCloudFederationProvider($resourceType); |
|
162
|
|
|
$share = $this->factory->getCloudFederationShare($shareWith, $name, $description, $providerId, $owner, $ownerDisplayName, $sharedBy, $sharedByDisplayName, '', $shareType, $resourceType); |
|
163
|
|
|
$share->setProtocol($protocol); |
|
164
|
|
|
$id = $provider->shareReceived($share); |
|
|
|
|
|
|
165
|
|
|
} catch (ProviderDoesNotExistsException $e) { |
|
166
|
|
|
return new JSONResponse( |
|
167
|
|
|
['message' => $e->getMessage()], |
|
168
|
|
|
Http::STATUS_NOT_IMPLEMENTED |
|
169
|
|
|
); |
|
170
|
|
|
} catch (ProviderCouldNotAddShareException $e) { |
|
171
|
|
|
return new JSONResponse( |
|
172
|
|
|
['message' => $e->getMessage()], |
|
173
|
|
|
$e->getCode() |
|
174
|
|
|
); |
|
175
|
|
|
} catch (\Exception $e) { |
|
176
|
|
|
return new JSONResponse( |
|
177
|
|
|
['message' => 'Internal error at ' . $this->urlGenerator->getBaseUrl()], |
|
178
|
|
|
Http::STATUS_BAD_REQUEST |
|
179
|
|
|
); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
$user = $this->userManager->get($shareWithLocalId); |
|
183
|
|
|
$recipientDisplayName = ''; |
|
184
|
|
|
if($user) { |
|
185
|
|
|
$recipientDisplayName = $user->getDisplayName(); |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
return new JSONResponse( |
|
189
|
|
|
['recipientDisplayName' => $recipientDisplayName], |
|
190
|
|
|
Http::STATUS_CREATED); |
|
191
|
|
|
|
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* receive notification about existing share |
|
196
|
|
|
* |
|
197
|
|
|
* @NoCSRFRequired |
|
198
|
|
|
* @PublicPage |
|
199
|
|
|
* @BruteForceProtection(action=receiveFederatedShareNotification) |
|
200
|
|
|
* |
|
201
|
|
|
* @param string $notificationType (notification type, e.g. SHARE_ACCEPTED) |
|
202
|
|
|
* @param string $resourceType (calendar, file, contact,...) |
|
203
|
|
|
* @param string $providerId id of the share |
|
204
|
|
|
* @param array $notification the actual payload of the notification |
|
205
|
|
|
* @return JSONResponse |
|
206
|
|
|
*/ |
|
207
|
|
|
public function receiveNotification($notificationType, $resourceType, $providerId, array $notification) { |
|
208
|
|
|
|
|
209
|
|
|
// check if all required parameters are set |
|
210
|
|
|
if ($notificationType === null || |
|
211
|
|
|
$resourceType === null || |
|
212
|
|
|
$providerId === null || |
|
213
|
|
|
!is_array($notification) |
|
214
|
|
|
) { |
|
215
|
|
|
return new JSONResponse( |
|
216
|
|
|
['message' => 'Missing arguments'], |
|
217
|
|
|
Http::STATUS_BAD_REQUEST |
|
218
|
|
|
); |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
try { |
|
222
|
|
|
$provider = $this->cloudFederationProviderManager->getCloudFederationProvider($resourceType); |
|
223
|
|
|
$result = $provider->notificationReceived($notificationType, $providerId, $notification); |
|
224
|
|
|
} catch (ProviderDoesNotExistsException $e) { |
|
225
|
|
|
return new JSONResponse( |
|
226
|
|
|
['message' => $e->getMessage()], |
|
227
|
|
|
Http::STATUS_BAD_REQUEST |
|
228
|
|
|
); |
|
229
|
|
|
} catch (ShareNotFound $e) { |
|
230
|
|
|
return new JSONResponse( |
|
231
|
|
|
['message' => $e->getMessage()], |
|
232
|
|
|
Http::STATUS_BAD_REQUEST |
|
233
|
|
|
); |
|
234
|
|
|
} catch (ActionNotSupportedException $e) { |
|
235
|
|
|
return new JSONResponse( |
|
236
|
|
|
['message' => $e->getMessage()], |
|
237
|
|
|
Http::STATUS_NOT_IMPLEMENTED |
|
238
|
|
|
); |
|
239
|
|
|
} catch (BadRequestException $e) { |
|
240
|
|
|
return new JSONResponse($e->getReturnMessage(), Http::STATUS_BAD_REQUEST); |
|
241
|
|
|
} catch (AuthenticationFailedException $e) { |
|
242
|
|
|
return new JSONResponse(["message" => "RESOURCE_NOT_FOUND"], Http::STATUS_FORBIDDEN); |
|
243
|
|
|
} |
|
244
|
|
|
catch (\Exception $e) { |
|
245
|
|
|
return new JSONResponse( |
|
246
|
|
|
['message' => 'Internal error at ' . $this->urlGenerator->getBaseUrl()], |
|
247
|
|
|
Http::STATUS_BAD_REQUEST |
|
248
|
|
|
); |
|
249
|
|
|
} |
|
250
|
|
|
|
|
251
|
|
|
return new JSONResponse($result,Http::STATUS_CREATED); |
|
252
|
|
|
|
|
253
|
|
|
} |
|
254
|
|
|
|
|
255
|
|
|
/** |
|
256
|
|
|
* map login name to internal LDAP UID if a LDAP backend is in use |
|
257
|
|
|
* |
|
258
|
|
|
* @param string $uid |
|
259
|
|
|
* @return string mixed |
|
260
|
|
|
*/ |
|
261
|
|
|
private function mapUid($uid) { |
|
262
|
|
|
\OC::$server->getURLGenerator()->linkToDocs('key'); |
|
263
|
|
|
// FIXME this should be a method in the user management instead |
|
264
|
|
|
$this->logger->debug('shareWith before, ' . $uid, ['app' => $this->appName]); |
|
265
|
|
|
\OCP\Util::emitHook( |
|
266
|
|
|
'\OCA\Files_Sharing\API\Server2Server', |
|
267
|
|
|
'preLoginNameUsedAsUserName', |
|
268
|
|
|
array('uid' => &$uid) |
|
269
|
|
|
); |
|
270
|
|
|
$this->logger->debug('shareWith after, ' . $uid, ['app' => $this->appName]); |
|
271
|
|
|
|
|
272
|
|
|
return $uid; |
|
273
|
|
|
} |
|
274
|
|
|
|
|
275
|
|
|
} |
|
276
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.