Completed
Pull Request — master (#32303)
by Victor
11:29
created

RequestHandlerController::assertIncomingSharingEnabled()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 2
nop 0
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\IShare;
43
44
/**
45
 * Class RequestHandlerController
46
 *
47
 * Handles OCS Request to the federated share API
48
 *
49
 * @package OCA\FederatedFileSharing\API
50
 */
51
class RequestHandlerController extends OCSController {
52
53
	/** @var OcmMiddleware */
54
	private $ocmMiddleware;
55
56
	/** @var IUserManager */
57
	private $userManager;
58
59
	/** @var AddressHandler */
60
	private $addressHandler;
61
62
	/** @var  FedShareManager */
63
	private $fedShareManager;
64
65
	/**
66
	 * Server2Server constructor.
67
	 *
68
	 * @param string $appName
69
	 * @param IRequest $request
70
	 * @param OcmMiddleware $ocmMiddleware
71
	 * @param IUserManager $userManager
72
	 * @param AddressHandler $addressHandler
73
	 * @param FedShareManager $fedShareManager
74
	 */
75
	public function __construct($appName,
76
								IRequest $request,
77
								OcmMiddleware $ocmMiddleware,
78
								IUserManager $userManager,
79
								AddressHandler $addressHandler,
80
								FedShareManager $fedShareManager
81
	) {
82
		parent::__construct($appName, $request);
83
84
		$this->ocmMiddleware = $ocmMiddleware;
85
		$this->userManager = $userManager;
86
		$this->addressHandler = $addressHandler;
87
		$this->fedShareManager = $fedShareManager;
88
	}
89
90
	/**
91
	 * @NoCSRFRequired
92
	 * @PublicPage
93
	 *
94
	 * create a new share
95
	 *
96
	 * @return Result
97
	 */
98
	public function createShare() {
99
		try {
100
			$this->ocmMiddleware->assertIncomingSharingEnabled();
101
			$remote = $this->request->getParam('remote', null);
102
			$token = $this->request->getParam('token', null);
103
			$name = $this->request->getParam('name', null);
104
			$owner = $this->request->getParam('owner', null);
105
			$sharedBy = $this->request->getParam('sharedBy', null);
106
			$shareWith = $this->request->getParam('shareWith', null);
107
			$remoteId = $this->request->getParam('remoteId', null);
108
			$sharedByFederatedId = $this->request->getParam(
109
				'sharedByFederatedId',
110
				null
111
			);
112
			$ownerFederatedId = $this->request->getParam('ownerFederatedId', null);
113
			$this->ocmMiddleware->assertNotNull(
114
				[
115
					'remote' => $remote,
116
					'token' => $token,
117
					'name' => $name,
118
					'owner' => $owner,
119
					'remoteId' => $remoteId,
120
					'shareWith' => $shareWith
121
				]
122
			);
123
124
			if (!\OCP\Util::isValidFileName($name)) {
125
				throw new BadRequestException(
126
					'The mountpoint name contains invalid characters.'
127
				);
128
			}
129
			// FIXME this should be a method in the user management instead
130
			\OCP\Util::writeLog('files_sharing', 'shareWith before, ' . $shareWith, \OCP\Util::DEBUG);
131
			\OCP\Util::emitHook(
132
				'\OCA\Files_Sharing\API\Server2Server',
133
				'preLoginNameUsedAsUserName',
134
				['uid' => &$shareWith]
135
			);
136
			\OCP\Util::writeLog('files_sharing', 'shareWith after, ' . $shareWith, \OCP\Util::DEBUG);
137
			if (!$this->userManager->userExists($shareWith)) {
138
				throw new BadRequestException('User does not exist');
139
			}
140
141
			if ($ownerFederatedId === null) {
142
				$ownerFederatedId = $owner . '@' . $this->addressHandler->normalizeRemote($remote);
143
			}
144
			// if the owner of the share and the initiator are the same user
145
			// we also complete the federated share ID for the initiator
146
			if ($sharedByFederatedId === null && $owner === $sharedBy) {
147
				$sharedByFederatedId = $ownerFederatedId;
148
			}
149
150
			$ownerAddress = new Address($ownerFederatedId);
151
			$sharedByAddress = new Address($sharedByFederatedId);
152
153
			$this->fedShareManager->createShare(
154
				$ownerAddress,
155
				$sharedByAddress,
156
				$shareWith,
157
				$remoteId,
158
				$name,
159
				$token
160
			);
161
		} catch (OcmException $e) {
162
			return new Result(
163
				null,
164
				$e->getHttpStatusCode(),
165
				$e->getMessage()
166
			);
167
		} catch (\Exception $e) {
168
			\OCP\Util::writeLog(
169
				'files_sharing',
170
				'server can not add remote share, ' . $e->getMessage(),
171
				\OCP\Util::ERROR
172
			);
173
			return new Result(
174
				null,
175
				Http::STATUS_INTERNAL_SERVER_ERROR,
176
				'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...
177
			);
178
		}
179
		return new Result();
180
	}
181
182
	/**
183
	 * @NoCSRFRequired
184
	 * @PublicPage
185
	 *
186
	 * create re-share on behalf of another user
187
	 *
188
	 * @param int $id
189
	 *
190
	 * @return Result
191
	 */
192
	public function reShare($id) {
193
		$token = $this->request->getParam('token', null);
194
		$shareWith = $this->request->getParam('shareWith', null);
195
		$permission = $this->request->getParam('permission', null);
196
		$remoteId = $this->request->getParam('remoteId', null);
197
198
		try {
199
			$this->ocmMiddleware->assertNotNull(
200
				[
201
					'id' => $id,
202
					'token' => $token,
203
					'shareWith' => $shareWith,
204
					'permission'  => $permission,
205
					'remoteId' => $remoteId
206
				]
207
			);
208
			$permission = (int) $permission;
209
			$remoteId = (int) $remoteId;
210
			$share = $this->ocmMiddleware->getValidShare($id, $token);
211
212
			// don't allow to share a file back to the owner
213
			list($user, $remote) = $this->addressHandler->splitUserRemote($shareWith);
214
			$owner = $share->getShareOwner();
215
			$currentServer = $this->addressHandler->generateRemoteURL();
216
			if ($this->addressHandler->compareAddresses($user, $remote, $owner, $currentServer)) {
217
				return new Result(null, Http::STATUS_FORBIDDEN);
218
			}
219
220
			$reSharingAllowed = $share->getPermissions() & Constants::PERMISSION_SHARE;
221
			if (!$reSharingAllowed) {
222
				return new Result(null, Http::STATUS_BAD_REQUEST);
223
			}
224
			$result = $this->fedShareManager->reShare(
225
				$share,
226
				$remoteId,
227
				$shareWith,
228
				$permission
229
			);
230
		} catch (BadRequestException $e) {
231
			return new Result(null, Http::STATUS_FORBIDDEN);
232
		} catch (\Exception $e) {
233
			return new Result(null, Http::STATUS_BAD_REQUEST);
234
		}
235
236
		return new Result(
237
			[
238
				'token' => $result->getToken(),
239
				'remoteId' => $result->getId()
240
			]
241
		);
242
	}
243
244
	/**
245
	 * @NoCSRFRequired
246
	 * @PublicPage
247
	 *
248
	 * accept server-to-server share
249
	 *
250
	 * @param int $id
251
	 *
252
	 * @return Result
253
	 */
254 View Code Duplication
	public function acceptShare($id) {
255
		try {
256
			$this->ocmMiddleware->assertOutgoingSharingEnabled();
257
			$token = $this->request->getParam('token', null);
258
			$share = $this->ocmMiddleware->getValidShare($id, $token);
259
			$this->fedShareManager->acceptShare($share);
260
		} catch (NotImplementedException $e) {
261
			return new Result(
262
				null,
263
				Http::STATUS_SERVICE_UNAVAILABLE,
264
				'Server does not support federated cloud sharing'
265
			);
266
		}
267
		return new Result();
268
	}
269
270
	/**
271
	 * @NoCSRFRequired
272
	 * @PublicPage
273
	 *
274
	 * decline server-to-server share
275
	 *
276
	 * @param int $id
277
	 *
278
	 * @return Result
279
	 */
280 View Code Duplication
	public function declineShare($id) {
281
		try {
282
			$token = $this->request->getParam('token', null);
283
			$this->ocmMiddleware->assertOutgoingSharingEnabled();
284
			$share = $this->ocmMiddleware->getValidShare($id, $token);
285
			$this->fedShareManager->declineShare($share);
286
		} catch (NotImplementedException $e) {
287
			return new Result(
288
				null,
289
				Http::STATUS_SERVICE_UNAVAILABLE,
290
				'Server does not support federated cloud sharing'
291
			);
292
		}
293
294
		return new Result();
295
	}
296
297
	/**
298
	 * @NoCSRFRequired
299
	 * @PublicPage
300
	 *
301
	 * remove server-to-server share if it was unshared by the owner
302
	 *
303
	 * @param int $id
304
	 *
305
	 * @return Result
306
	 */
307 View Code Duplication
	public function unshare($id) {
308
		try {
309
			$this->ocmMiddleware->assertOutgoingSharingEnabled();
310
			$token = $this->request->getParam('token', null);
311
			if ($token && $id) {
312
				$this->fedShareManager->unshare($id, $token);
313
			}
314
		} catch (NotImplementedException $e) {
315
			return new Result(
316
				null,
317
				Http::STATUS_SERVICE_UNAVAILABLE,
318
				'Server does not support federated cloud sharing'
319
			);
320
		} catch (\Exception $e) {
321
			// pass
322
		}
323
		return new Result();
324
	}
325
326
	/**
327
	 * @NoCSRFRequired
328
	 * @PublicPage
329
	 *
330
	 * federated share was revoked, either by the owner or the re-sharer
331
	 *
332
	 * @param int $id
333
	 *
334
	 * @return Result
335
	 */
336 View Code Duplication
	public function revoke($id) {
337
		try {
338
			$token = $this->request->getParam('token', null);
339
			$share = $this->getValidShare($id, $token);
340
			$this->fedShareManager->revoke($share);
341
		} catch (\Exception $e) {
342
			return new Result(null, Http::STATUS_BAD_REQUEST);
343
		}
344
345
		return new Result();
346
	}
347
348
	/**
349
	 * @NoCSRFRequired
350
	 * @PublicPage
351
	 *
352
	 * update share information to keep federated re-shares in sync
353
	 *
354
	 * @param int $id
355
	 *
356
	 * @return Result
357
	 */
358
	public function updatePermissions($id) {
359
		try {
360
			$permissions = $this->request->getParam('permissions', null);
361
			$token = $this->request->getParam('token', null);
362
			$share = $this->ocmMiddleware->getValidShare($id, $token);
363
			$validPermission = \ctype_digit((string)$permissions);
364
			if (!$validPermission) {
365
				throw new \Exception();
366
			}
367
			$this->fedShareManager->updatePermissions($share, (int)$permissions);
368
		} catch (\Exception $e) {
369
			return new Result(null, Http::STATUS_BAD_REQUEST);
370
		}
371
372
		return new Result();
373
	}
374
}
375