Completed
Pull Request — master (#32303)
by Victor
13:39
created

RequestHandlerController::hasNull()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @author Arthur Schiwon <[email protected]>
4
 * @author Björn Schießle <[email protected]>
5
 * @author Joas Schilling <[email protected]>
6
 * @author Lukas Reschke <[email protected]>
7
 * @author Morris Jobke <[email protected]>
8
 * @author Thomas Müller <[email protected]>
9
 *
10
 * @copyright Copyright (c) 2018, ownCloud GmbH
11
 * @license AGPL-3.0
12
 *
13
 * This code is free software: you can redistribute it and/or modify
14
 * it under the terms of the GNU Affero General Public License, version 3,
15
 * as published by the Free Software Foundation.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20
 * GNU Affero General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU Affero General Public License, version 3,
23
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
24
 *
25
 */
26
27
namespace OCA\FederatedFileSharing\Controller;
28
29
use OC\OCS\Result;
30
use OCA\FederatedFileSharing\Address;
31
use OCA\FederatedFileSharing\AddressHandler;
32
use OCA\FederatedFileSharing\FedShareManager;
33
use OCA\FederatedFileSharing\Middleware\OcmMiddleware;
34
use OCA\FederatedFileSharing\Ocm\Exception\BadRequestException;
35
use OCA\FederatedFileSharing\Ocm\Exception\NotImplementedException;
36
use OCA\FederatedFileSharing\Ocm\Exception\OcmException;
37
use OCP\AppFramework\Http;
38
use OCP\AppFramework\OCSController;
39
use OCP\Constants;
40
use OCP\IRequest;
41
use OCP\IUserManager;
42
use OCP\Share;
43
use OCP\Share\IShare;
44
45
/**
46
 * Class RequestHandlerController
47
 *
48
 * Handles OCS Request to the federated share API
49
 *
50
 * @package OCA\FederatedFileSharing\API
51
 */
52
class RequestHandlerController extends OCSController {
53
54
	/** @var OcmMiddleware */
55
	private $ocmMiddleware;
56
57
	/** @var IUserManager */
58
	private $userManager;
59
60
	/** @var AddressHandler */
61
	private $addressHandler;
62
63
	/** @var  FedShareManager */
64
	private $fedShareManager;
65
66
	/**
67
	 * Server2Server constructor.
68
	 *
69
	 * @param string $appName
70
	 * @param IRequest $request
71
	 * @param OcmMiddleware $ocmMiddleware
72
	 * @param IUserManager $userManager
73
	 * @param AddressHandler $addressHandler
74
	 * @param FedShareManager $fedShareManager
75
	 */
76
	public function __construct($appName,
77
								IRequest $request,
78
								OcmMiddleware $ocmMiddleware,
79
								IUserManager $userManager,
80
								AddressHandler $addressHandler,
81
								FedShareManager $fedShareManager
82
	) {
83
		parent::__construct($appName, $request);
84
85
		$this->ocmMiddleware = $ocmMiddleware;
86
		$this->userManager = $userManager;
87
		$this->addressHandler = $addressHandler;
88
		$this->fedShareManager = $fedShareManager;
89
	}
90
91
	/**
92
	 * @NoCSRFRequired
93
	 * @PublicPage
94
	 *
95
	 * create a new share
96
	 *
97
	 * @return Result
98
	 */
99
	public function createShare() {
100
		try {
101
			$this->ocmMiddleware->assertIncomingSharingEnabled();
102
			$remote = $this->request->getParam('remote', null);
103
			$token = $this->request->getParam('token', null);
104
			$name = $this->request->getParam('name', null);
105
			$owner = $this->request->getParam('owner', null);
106
			$sharedBy = $this->request->getParam('sharedBy', null);
107
			$shareWith = $this->request->getParam('shareWith', null);
108
			$remoteId = $this->request->getParam('remoteId', null);
109
			$sharedByFederatedId = $this->request->getParam(
110
				'sharedByFederatedId',
111
				null
112
			);
113
			$ownerFederatedId = $this->request->getParam('ownerFederatedId', null);
114
			$this->ocmMiddleware->assertNotNull(
115
				[
116
					'remote' => $remote,
117
					'token' => $token,
118
					'name' => $name,
119
					'owner' => $owner,
120
					'remoteId' => $remoteId,
121
					'shareWith' => $shareWith
122
				]
123
			);
124
125
			if (!\OCP\Util::isValidFileName($name)) {
126
				throw new BadRequestException(
127
					'The mountpoint name contains invalid characters.'
128
				);
129
			}
130
			// FIXME this should be a method in the user management instead
131
			\OCP\Util::writeLog('files_sharing', 'shareWith before, ' . $shareWith, \OCP\Util::DEBUG);
132
			\OCP\Util::emitHook(
133
				'\OCA\Files_Sharing\API\Server2Server',
134
				'preLoginNameUsedAsUserName',
135
				['uid' => &$shareWith]
136
			);
137
			\OCP\Util::writeLog('files_sharing', 'shareWith after, ' . $shareWith, \OCP\Util::DEBUG);
138
			if (!$this->userManager->userExists($shareWith)) {
139
				throw new BadRequestException('User does not exist');
140
			}
141
142
			if ($ownerFederatedId === null) {
143
				$ownerFederatedId = $owner . '@' . $this->addressHandler->normalizeRemote($remote);
144
			}
145
			// if the owner of the share and the initiator are the same user
146
			// we also complete the federated share ID for the initiator
147
			if ($sharedByFederatedId === null && $owner === $sharedBy) {
148
				$sharedByFederatedId = $ownerFederatedId;
149
			}
150
151
			$ownerAddress = new Address($ownerFederatedId);
152
			$sharedByAddress = new Address($sharedByFederatedId);
153
154
			$this->fedShareManager->createShare(
155
				$ownerAddress,
156
				$sharedByAddress,
157
				$shareWith,
158
				$remoteId,
159
				$name,
160
				$token
161
			);
162
		} catch (OcmException $e) {
163
			return new Result(
164
				null,
165
				$e->getHttpStatusCode(),
166
				$e->getMessage()
167
			);
168
		} catch (\Exception $e) {
169
			\OCP\Util::writeLog(
170
				'files_sharing',
171
				'server can not add remote share, ' . $e->getMessage(),
172
				\OCP\Util::ERROR
173
			);
174
			return new Result(
175
				null,
176
				Http::STATUS_INTERNAL_SERVER_ERROR,
177
				'internal server error, was not able to add share from ' . $remote
0 ignored issues
show
Bug introduced by
The variable $remote does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
178
			);
179
		}
180
		return new Result();
181
	}
182
183
	/**
184
	 * @NoCSRFRequired
185
	 * @PublicPage
186
	 *
187
	 * create re-share on behalf of another user
188
	 *
189
	 * @param int $id
190
	 *
191
	 * @return Result
192
	 */
193
	public function reShare($id) {
194
		$token = $this->request->getParam('token', null);
195
		$shareWith = $this->request->getParam('shareWith', null);
196
		$permission = $this->request->getParam('permission', null);
197
		$remoteId = $this->request->getParam('remoteId', null);
198
199
		try {
200
			$this->ocmMiddleware->assertNotNull(
201
				[
202
					'id' => $id,
203
					'token' => $token,
204
					'shareWith' => $shareWith,
205
					'permission'  => $permission,
206
					'remoteId' => $remoteId
207
				]
208
			);
209
			$permission = (int) $permission;
210
			$remoteId = (int) $remoteId;
211
			$share = $this->ocmMiddleware->getValidShare($id, $token);
212
213
			// don't allow to share a file back to the owner
214
			list($user, $remote) = $this->addressHandler->splitUserRemote($shareWith);
215
			$owner = $share->getShareOwner();
216
			$currentServer = $this->addressHandler->generateRemoteURL();
217
			if ($this->addressHandler->compareAddresses($user, $remote, $owner, $currentServer)) {
218
				return new Result(null, Http::STATUS_FORBIDDEN);
219
			}
220
221
			$reSharingAllowed = $share->getPermissions() & Constants::PERMISSION_SHARE;
222
			if (!$reSharingAllowed) {
223
				return new Result(null, Http::STATUS_BAD_REQUEST);
224
			}
225
			$result = $this->fedShareManager->reShare(
226
				$share,
227
				$remoteId,
228
				$shareWith,
229
				$permission
230
			);
231
		} catch (Share\Exceptions\ShareNotFound $e) {
232
			return new Result(null, Http::STATUS_NOT_FOUND);
233
		} catch (BadRequestException $e) {
234
			return new Result(null, Http::STATUS_FORBIDDEN);
235
		} catch (\Exception $e) {
236
			return new Result(null, Http::STATUS_BAD_REQUEST);
237
		}
238
239
		return new Result(
240
			[
241
				'token' => $result->getToken(),
242
				'remoteId' => $result->getId()
243
			]
244
		);
245
	}
246
247
	/**
248
	 * @NoCSRFRequired
249
	 * @PublicPage
250
	 *
251
	 * accept server-to-server share
252
	 *
253
	 * @param int $id
254
	 *
255
	 * @return Result
256
	 */
257 View Code Duplication
	public function acceptShare($id) {
258
		try {
259
			$this->ocmMiddleware->assertOutgoingSharingEnabled();
260
			$token = $this->request->getParam('token', null);
261
			$share = $this->ocmMiddleware->getValidShare($id, $token);
262
			$this->fedShareManager->acceptShare($share);
263
		} catch (NotImplementedException $e) {
264
			return new Result(
265
				null,
266
				Http::STATUS_SERVICE_UNAVAILABLE,
267
				'Server does not support federated cloud sharing'
268
			);
269
		} catch (Share\Exceptions\ShareNotFound $e) {
270
			// pass
271
		}
272
		return new Result();
273
	}
274
275
	/**
276
	 * @NoCSRFRequired
277
	 * @PublicPage
278
	 *
279
	 * decline server-to-server share
280
	 *
281
	 * @param int $id
282
	 *
283
	 * @return Result
284
	 */
285 View Code Duplication
	public function declineShare($id) {
286
		try {
287
			$token = $this->request->getParam('token', null);
288
			$this->ocmMiddleware->assertOutgoingSharingEnabled();
289
			$share = $this->ocmMiddleware->getValidShare($id, $token);
290
			$this->fedShareManager->declineShare($share);
291
		} catch (NotImplementedException $e) {
292
			return new Result(
293
				null,
294
				Http::STATUS_SERVICE_UNAVAILABLE,
295
				'Server does not support federated cloud sharing'
296
			);
297
		} catch (Share\Exceptions\ShareNotFound $e) {
298
			// pass
299
		}
300
301
		return new Result();
302
	}
303
304
	/**
305
	 * @NoCSRFRequired
306
	 * @PublicPage
307
	 *
308
	 * remove server-to-server share if it was unshared by the owner
309
	 *
310
	 * @param int $id
311
	 *
312
	 * @return Result
313
	 */
314 View Code Duplication
	public function unshare($id) {
315
		try {
316
			$this->ocmMiddleware->assertOutgoingSharingEnabled();
317
			$token = $this->request->getParam('token', null);
318
			if ($token && $id) {
319
				$this->fedShareManager->unshare($id, $token);
320
			}
321
		} catch (NotImplementedException $e) {
322
			return new Result(
323
				null,
324
				Http::STATUS_SERVICE_UNAVAILABLE,
325
				'Server does not support federated cloud sharing'
326
			);
327
		} catch (\Exception $e) {
328
			// pass
329
		}
330
		return new Result();
331
	}
332
333
	/**
334
	 * @NoCSRFRequired
335
	 * @PublicPage
336
	 *
337
	 * federated share was revoked, either by the owner or the re-sharer
338
	 *
339
	 * @param int $id
340
	 *
341
	 * @return Result
342
	 */
343 View Code Duplication
	public function revoke($id) {
344
		try {
345
			$token = $this->request->getParam('token', null);
346
			$share = $this->getValidShare($id, $token);
347
			$this->fedShareManager->revoke($share);
348
		} catch (\Exception $e) {
349
			return new Result(null, Http::STATUS_BAD_REQUEST);
350
		}
351
352
		return new Result();
353
	}
354
355
	/**
356
	 * @NoCSRFRequired
357
	 * @PublicPage
358
	 *
359
	 * update share information to keep federated re-shares in sync
360
	 *
361
	 * @param int $id
362
	 *
363
	 * @return Result
364
	 */
365
	public function updatePermissions($id) {
366
		try {
367
			$permissions = $this->request->getParam('permissions', null);
368
			$token = $this->request->getParam('token', null);
369
			$share = $this->ocmMiddleware->getValidShare($id, $token);
370
			$validPermission = \ctype_digit((string)$permissions);
371
			if (!$validPermission) {
372
				throw new \Exception();
373
			}
374
			$this->fedShareManager->updatePermissions($share, (int)$permissions);
375
		} catch (\Exception $e) {
376
			return new Result(null, Http::STATUS_BAD_REQUEST);
377
		}
378
379
		return new Result();
380
	}
381
}
382