Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
45 | class OcmController extends Controller { |
||
46 | const API_VERSION = '1.0-proposal1'; |
||
47 | |||
48 | /** |
||
49 | * @var OcmMiddleware |
||
50 | */ |
||
51 | private $ocmMiddleware; |
||
52 | |||
53 | /** |
||
54 | * @var IURLGenerator |
||
55 | */ |
||
56 | protected $urlGenerator; |
||
57 | |||
58 | /** |
||
59 | * @var IUserManager |
||
60 | */ |
||
61 | protected $userManager; |
||
62 | |||
63 | /** |
||
64 | * @var AddressHandler |
||
65 | */ |
||
66 | protected $addressHandler; |
||
67 | |||
68 | /** |
||
69 | * @var FedShareManager |
||
70 | */ |
||
71 | protected $fedShareManager; |
||
72 | |||
73 | /** |
||
74 | * @var ILogger |
||
75 | */ |
||
76 | protected $logger; |
||
77 | |||
78 | /** |
||
79 | * OcmController constructor. |
||
80 | * |
||
81 | * @param string $appName |
||
82 | * @param IRequest $request |
||
83 | * @param OcmMiddleware $ocmMiddleware |
||
84 | * @param IURLGenerator $urlGenerator |
||
85 | * @param IUserManager $userManager |
||
86 | * @param AddressHandler $addressHandler |
||
87 | * @param FedShareManager $fedShareManager |
||
88 | * @param ILogger $logger |
||
89 | */ |
||
90 | View Code Duplication | public function __construct($appName, |
|
91 | IRequest $request, |
||
92 | OcmMiddleware $ocmMiddleware, |
||
93 | IURLGenerator $urlGenerator, |
||
94 | IUserManager $userManager, |
||
95 | AddressHandler $addressHandler, |
||
96 | FedShareManager $fedShareManager, |
||
97 | ILogger $logger |
||
98 | ) { |
||
99 | parent::__construct($appName, $request); |
||
100 | |||
101 | $this->ocmMiddleware = $ocmMiddleware; |
||
102 | $this->urlGenerator = $urlGenerator; |
||
103 | $this->userManager = $userManager; |
||
104 | $this->addressHandler = $addressHandler; |
||
105 | $this->fedShareManager = $fedShareManager; |
||
106 | $this->logger = $logger; |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * @NoCSRFRequired |
||
111 | * @PublicPage |
||
112 | * |
||
113 | * EndPoint discovery |
||
114 | * Responds to /ocm-provider/ requests |
||
115 | * |
||
116 | * @return array |
||
117 | */ |
||
118 | public function discovery() { |
||
119 | $endPoint = $this->urlGenerator->linkToRouteAbsolute( |
||
120 | "{$this->appName}.ocm.index" |
||
121 | ); |
||
122 | return [ |
||
123 | 'enabled' => true, |
||
124 | 'apiVersion' => self::API_VERSION, |
||
125 | 'endPoint' => \rtrim($endPoint, '/'), |
||
126 | 'shareTypes' => [ |
||
127 | [ |
||
128 | 'name' => FileNotification::RESOURCE_TYPE_FILE, |
||
129 | 'protocols' => $this->getProtocols() |
||
130 | ] |
||
131 | ] |
||
132 | ]; |
||
133 | } |
||
134 | |||
135 | /** |
||
136 | * @NoCSRFRequired |
||
137 | * @PublicPage |
||
138 | * |
||
139 | * @param string $shareWith identifier of the user or group |
||
140 | * to share the resource with |
||
141 | * @param string $name name of the shared resource |
||
142 | * @param string $description share description (optional) |
||
143 | * @param string $providerId Identifier of the resource at the provider side |
||
144 | * @param string $owner identifier of the user that owns the resource |
||
145 | * @param string $ownerDisplayName display name of the owner |
||
146 | * @param string $sender Provider specific identifier of the user that wants |
||
147 | * to share the resource |
||
148 | * @param string $senderDisplayName Display name of the user that wants |
||
149 | * to share the resource |
||
150 | * @param string $shareType Share type ('user' is supported atm) |
||
151 | * @param string $resourceType only 'file' is supported atm |
||
152 | * @param array $protocol |
||
153 | * [ |
||
154 | * 'name' => (string) protocol name. Only 'webdav' is supported atm, |
||
155 | * 'options' => [ |
||
156 | * protocol specific options |
||
157 | * only `webdav` options are supported atm |
||
158 | * e.g. `uri`, `access_token`, `password`, `permissions` etc. |
||
159 | * |
||
160 | * For backward compatibility the webdav protocol will use |
||
161 | * the 'sharedSecret" as username and password |
||
162 | * ] |
||
163 | * |
||
164 | * @return JSONResponse |
||
165 | */ |
||
166 | public function createShare($shareWith, |
||
167 | $name, |
||
168 | $description, |
||
|
|||
169 | $providerId, |
||
170 | $owner, |
||
171 | $ownerDisplayName, |
||
172 | $sender, |
||
173 | $senderDisplayName, |
||
174 | $shareType, |
||
175 | $resourceType, |
||
176 | $protocol |
||
177 | |||
178 | ) { |
||
179 | try { |
||
180 | $this->ocmMiddleware->assertIncomingSharingEnabled(); |
||
181 | $this->ocmMiddleware->assertNotNull( |
||
182 | [ |
||
183 | 'shareWith' => $shareWith, |
||
184 | 'name' => $name, |
||
185 | 'providerId' => $providerId, |
||
186 | 'owner' => $owner, |
||
187 | 'shareType' => $shareType, |
||
188 | 'resourceType' => $resourceType |
||
189 | ] |
||
190 | ); |
||
191 | if (!\is_array($protocol) |
||
192 | || !isset($protocol['name']) |
||
193 | || !isset($protocol['options']) |
||
194 | || !\is_array($protocol['options']) |
||
195 | || !isset($protocol['options']['sharedSecret']) |
||
196 | ) { |
||
197 | throw new BadRequestException( |
||
198 | 'server can not add remote share, missing parameter' |
||
199 | ); |
||
200 | } |
||
201 | if (!\OCP\Util::isValidFileName($name)) { |
||
202 | throw new BadRequestException( |
||
203 | 'The mountpoint name contains invalid characters.' |
||
204 | ); |
||
205 | } |
||
206 | |||
207 | $shareWithAddress = new Address($shareWith); |
||
208 | $localShareWith = $shareWithAddress->getUserId(); |
||
209 | |||
210 | // FIXME this should be a method in the user management instead |
||
211 | $this->logger->debug( |
||
212 | "shareWith before, $localShareWith", |
||
213 | ['app' => $this->appName] |
||
214 | ); |
||
215 | \OCP\Util::emitHook( |
||
216 | '\OCA\Files_Sharing\API\Server2Server', |
||
217 | 'preLoginNameUsedAsUserName', |
||
218 | ['uid' => &$localShareWith] |
||
219 | ); |
||
220 | $this->logger->debug( |
||
221 | "shareWith after, $localShareWith", |
||
222 | ['app' => $this->appName] |
||
223 | ); |
||
224 | |||
225 | if ($this->isSupportedProtocol($protocol['name']) === false) { |
||
226 | throw new NotImplementedException( |
||
227 | "Protocol {$protocol['name']} is not supported" |
||
228 | ); |
||
229 | } |
||
230 | |||
231 | if ($this->isSupportedShareType($shareType) === false) { |
||
232 | throw new NotImplementedException( |
||
233 | "ShareType {$shareType} is not supported" |
||
234 | ); |
||
235 | } |
||
236 | |||
237 | if ($this->isSupportedResourceType($resourceType) === false) { |
||
238 | throw new NotImplementedException( |
||
239 | "ResourceType {$resourceType} is not supported" |
||
240 | ); |
||
241 | } |
||
242 | |||
243 | if (!$this->userManager->userExists($localShareWith)) { |
||
244 | throw new BadRequestException("User $localShareWith does not exist"); |
||
245 | } |
||
246 | |||
247 | $ownerAddress = new Address($owner, $ownerDisplayName); |
||
248 | $sharedByAddress = new Address($sender, $senderDisplayName); |
||
249 | |||
250 | $this->fedShareManager->createShare( |
||
251 | $ownerAddress, |
||
252 | $sharedByAddress, |
||
253 | $localShareWith, |
||
254 | $providerId, |
||
255 | $name, |
||
256 | $protocol['options']['sharedSecret'] |
||
257 | ); |
||
258 | } catch (OcmException $e) { |
||
259 | return new JSONResponse( |
||
260 | ['message' => $e->getMessage()], |
||
261 | $e->getHttpStatusCode() |
||
262 | ); |
||
263 | } catch (\Exception $e) { |
||
264 | $this->logger->error( |
||
265 | "server can not add remote share, {$e->getMessage()}", |
||
266 | ['app' => 'federatefilesharing'] |
||
267 | ); |
||
268 | return new JSONResponse( |
||
269 | [ |
||
270 | 'message' => "internal server error, was not able to add share from {$ownerAddress->getHostName()}" |
||
271 | ], |
||
272 | Http::STATUS_INTERNAL_SERVER_ERROR |
||
273 | ); |
||
274 | } |
||
275 | return new JSONResponse( |
||
276 | [], |
||
277 | Http::STATUS_CREATED |
||
278 | ); |
||
279 | } |
||
280 | |||
281 | /** |
||
282 | * @NoCSRFRequired |
||
283 | * @PublicPage |
||
284 | * |
||
285 | * @param string $notificationType notification type (SHARE_REMOVED, etc) |
||
286 | * @param string $resourceType only 'file' is supported atm |
||
287 | * @param string $providerId Identifier of the resource at the provider side |
||
288 | * @param array $notification |
||
289 | * [ |
||
290 | * optional additional parameters, depending on the notification |
||
291 | * and the resource type |
||
292 | * ] |
||
293 | * |
||
294 | * @return JSONResponse |
||
295 | */ |
||
296 | public function processNotification($notificationType, |
||
297 | $resourceType, |
||
298 | $providerId, |
||
299 | $notification |
||
300 | ) { |
||
301 | try { |
||
302 | $this->ocmMiddleware->assertNotNull( |
||
303 | [ |
||
304 | 'notificationType' => $notificationType, |
||
305 | 'resourceType' => $resourceType, |
||
306 | 'providerId' => $providerId |
||
307 | ] |
||
308 | ); |
||
309 | if (!\is_array($notification)) { |
||
310 | throw new BadRequestException( |
||
311 | 'server can not add remote share, missing parameter' |
||
312 | ); |
||
313 | } |
||
314 | |||
315 | if ($this->isSupportedResourceType($resourceType) === false) { |
||
316 | throw new NotImplementedException( |
||
317 | "ResourceType {$resourceType} is not supported" |
||
318 | ); |
||
319 | } |
||
320 | // TODO: check permissions/preconditions in all cases |
||
321 | switch ($notificationType) { |
||
322 | View Code Duplication | case FileNotification::NOTIFICATION_TYPE_SHARE_ACCEPTED: |
|
323 | $this->ocmMiddleware->assertOutgoingSharingEnabled(); |
||
324 | $share = $this->ocmMiddleware->getValidShare( |
||
325 | $providerId, $notification['sharedSecret'] |
||
326 | ); |
||
327 | $this->fedShareManager->acceptShare($share); |
||
328 | break; |
||
329 | View Code Duplication | case FileNotification::NOTIFICATION_TYPE_SHARE_DECLINED: |
|
330 | $this->ocmMiddleware->assertOutgoingSharingEnabled(); |
||
331 | $share = $this->ocmMiddleware->getValidShare( |
||
332 | $providerId, $notification['sharedSecret'] |
||
333 | ); |
||
334 | $this->fedShareManager->declineShare($share); |
||
335 | break; |
||
336 | case FileNotification::NOTIFICATION_TYPE_REQUEST_RESHARE: |
||
337 | $this->ocmMiddleware->assertOutgoingSharingEnabled(); |
||
338 | $this->ocmMiddleware->assertNotNull( |
||
339 | [ |
||
340 | 'shareWith' => $notification['shareWith'], |
||
341 | 'senderId' => $notification['senderId'], |
||
342 | ] |
||
343 | ); |
||
344 | $share = $this->ocmMiddleware->getValidShare( |
||
345 | $providerId, $notification['sharedSecret'] |
||
346 | ); |
||
347 | |||
348 | // don't allow to share a file back to the owner |
||
349 | $owner = $share->getShareOwner(); |
||
350 | $ownerAddress = $this->addressHandler->getLocalUserFederatedAddress($owner); |
||
351 | $shareWithAddress = new Address($notification['shareWith']); |
||
352 | $this->ocmMiddleware->assertNotSameUser($ownerAddress, $shareWithAddress); |
||
353 | $this->ocmMiddleware->assertSharingPermissionSet($share); |
||
354 | |||
355 | // TODO: permissions not needed ??? |
||
356 | $reShare = $this->fedShareManager->reShare( |
||
357 | $share, $notification['senderId'], |
||
358 | $notification['shareWith'], |
||
359 | 0 |
||
360 | ); |
||
361 | return new JSONResponse( |
||
362 | [ |
||
363 | 'sharedSecret' => $reShare->getToken(), |
||
364 | 'providerId' => $reShare->getId() |
||
365 | ], |
||
366 | Http::STATUS_CREATED |
||
367 | ); |
||
368 | break; |
||
369 | case FileNotification::NOTIFICATION_TYPE_RESHARE_CHANGE_PERMISSION: |
||
370 | $permissions = $notification['permission']; |
||
371 | // TODO: Map OCM permissions to numeric |
||
372 | $share = $this->ocmMiddleware->getValidShare( |
||
373 | $providerId, $notification['sharedSecret'] |
||
374 | ); |
||
375 | $this->fedShareManager->updatePermissions($share, $permissions); |
||
376 | break; |
||
377 | case FileNotification::NOTIFICATION_TYPE_SHARE_UNSHARED: |
||
378 | $this->fedShareManager->unshare( |
||
379 | $providerId, $notification['sharedSecret'] |
||
380 | ); |
||
381 | break; |
||
382 | case FileNotification::NOTIFICATION_TYPE_RESHARE_UNDO: |
||
383 | $share = $this->ocmMiddleware->getValidShare( |
||
384 | $providerId, $notification['sharedSecret'] |
||
385 | ); |
||
386 | $this->fedShareManager->revoke($share); |
||
387 | break; |
||
388 | default: |
||
389 | return new JSONResponse( |
||
390 | ['message' => "Notification of type {$notificationType} is not supported"], |
||
391 | Http::STATUS_NOT_IMPLEMENTED |
||
392 | ); |
||
393 | } |
||
394 | } catch (OcmException $e) { |
||
395 | return new JSONResponse( |
||
396 | ['message' => $e->getMessage()], |
||
397 | $e->getHttpStatusCode() |
||
398 | ); |
||
399 | } catch (\Exception $e) { |
||
400 | $this->logger->error( |
||
401 | "server can not process notification, {$e->getMessage()}", |
||
402 | ['app' => 'federatefilesharing'] |
||
403 | ); |
||
404 | return new JSONResponse( |
||
405 | [ |
||
406 | 'message' => "internal server error, was not able to process notification" |
||
407 | ], |
||
408 | Http::STATUS_INTERNAL_SERVER_ERROR |
||
409 | ); |
||
410 | } |
||
411 | return new JSONResponse( |
||
412 | [], |
||
413 | Http::STATUS_CREATED |
||
414 | ); |
||
415 | } |
||
416 | |||
417 | /** |
||
418 | * Get list of supported protocols |
||
419 | * |
||
420 | * @return array |
||
421 | */ |
||
422 | protected function getProtocols() { |
||
423 | return [ |
||
424 | 'webdav' => '/public.php/webdav/' |
||
425 | ]; |
||
426 | } |
||
427 | |||
428 | /** |
||
429 | * @param string $resourceType |
||
430 | * |
||
431 | * @return bool |
||
432 | */ |
||
433 | protected function isSupportedResourceType($resourceType) { |
||
436 | |||
437 | /** |
||
438 | * @param string $shareType |
||
439 | * @return bool |
||
440 | */ |
||
441 | protected function isSupportedShareType($shareType) { |
||
445 | |||
446 | /** |
||
447 | * @param string $protocolName |
||
448 | * @return bool |
||
449 | */ |
||
450 | protected function isSupportedProtocol($protocolName) { |
||
455 | } |
||
456 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.