Completed
Push — master ( 45895d...58599e )
by Morris
99:32 queued 74:23
created

MountPublicLinkController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 19
nc 1
nop 10
dl 0
loc 22
rs 9.2
c 0
b 0
f 0

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 * @copyright Copyright (c) 2016, Björn Schießle <[email protected]>
5
 *
6
 * @author Allan Nordhøy <[email protected]>
7
 * @author Bjoern Schiessle <[email protected]>
8
 * @author Björn Schießle <[email protected]>
9
 * @author Lukas Reschke <[email protected]>
10
 * @author Robin Appelman <[email protected]>
11
 * @author Roeland Jago Douma <[email protected]>
12
 *
13
 * @license AGPL-3.0
14
 *
15
 * This code is free software: you can redistribute it and/or modify
16
 * it under the terms of the GNU Affero General Public License, version 3,
17
 * as published by the Free Software Foundation.
18
 *
19
 * This program is distributed in the hope that it will be useful,
20
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22
 * GNU Affero General Public License for more details.
23
 *
24
 * You should have received a copy of the GNU Affero General Public License, version 3,
25
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
26
 *
27
 */
28
29
30
namespace OCA\FederatedFileSharing\Controller;
31
32
use OC\Files\Filesystem;
33
use OC\HintException;
34
use OC\Share\Helper;
35
use OCA\FederatedFileSharing\AddressHandler;
36
use OCA\FederatedFileSharing\FederatedShareProvider;
37
use OCA\Files_Sharing\External\Manager;
38
use OCP\AppFramework\Controller;
39
use OCP\AppFramework\Http;
40
use OCP\AppFramework\Http\JSONResponse;
41
use OCP\Federation\ICloudIdManager;
42
use OCP\Files\StorageInvalidException;
43
use OCP\Http\Client\IClientService;
44
use OCP\IL10N;
45
use OCP\IRequest;
46
use OCP\ISession;
47
use OCP\IUserSession;
48
use OCP\Share\IManager;
49
use OCP\Util;
50
51
/**
52
 * Class MountPublicLinkController
53
 *
54
 * convert public links to federated shares
55
 *
56
 * @package OCA\FederatedFileSharing\Controller
57
 */
58
class MountPublicLinkController extends Controller {
59
60
	/** @var FederatedShareProvider */
61
	private $federatedShareProvider;
62
63
	/** @var AddressHandler */
64
	private $addressHandler;
65
66
	/** @var IManager  */
67
	private $shareManager;
68
69
	/** @var  ISession */
70
	private $session;
71
72
	/** @var IL10N */
73
	private $l;
74
75
	/** @var IUserSession */
76
	private $userSession;
77
78
	/** @var IClientService */
79
	private $clientService;
80
81
	/** @var ICloudIdManager  */
82
	private $cloudIdManager;
83
84
	/**
85
	 * MountPublicLinkController constructor.
86
	 *
87
	 * @param string $appName
88
	 * @param IRequest $request
89
	 * @param FederatedShareProvider $federatedShareProvider
90
	 * @param IManager $shareManager
91
	 * @param AddressHandler $addressHandler
92
	 * @param ISession $session
93
	 * @param IL10N $l
94
	 * @param IUserSession $userSession
95
	 * @param IClientService $clientService
96
	 * @param ICloudIdManager $cloudIdManager
97
	 */
98
	public function __construct($appName,
99
								IRequest $request,
100
								FederatedShareProvider $federatedShareProvider,
101
								IManager $shareManager,
102
								AddressHandler $addressHandler,
103
								ISession $session,
104
								IL10N $l,
105
								IUserSession $userSession,
106
								IClientService $clientService,
107
								ICloudIdManager $cloudIdManager
108
	) {
109
		parent::__construct($appName, $request);
110
111
		$this->federatedShareProvider = $federatedShareProvider;
112
		$this->shareManager = $shareManager;
113
		$this->addressHandler = $addressHandler;
114
		$this->session = $session;
115
		$this->l = $l;
116
		$this->userSession = $userSession;
117
		$this->clientService = $clientService;
118
		$this->cloudIdManager = $cloudIdManager;
119
	}
120
121
	/**
122
	 * send federated share to a user of a public link
123
	 *
124
	 * @NoCSRFRequired
125
	 * @PublicPage
126
	 * @BruteForceProtection(action=publicLink2FederatedShare)
127
	 *
128
	 * @param string $shareWith
129
	 * @param string $token
130
	 * @param string $password
131
	 * @return JSONResponse
132
	 */
133
	public function createFederatedShare($shareWith, $token, $password = '') {
134
135
		if (!$this->federatedShareProvider->isOutgoingServer2serverShareEnabled()) {
136
			return new JSONResponse(
137
				['message' => 'This server doesn\'t support outgoing federated shares'],
138
				Http::STATUS_BAD_REQUEST
139
			);
140
		}
141
142
		try {
143
			list(, $server) = $this->addressHandler->splitUserRemote($shareWith);
144
			$share = $this->shareManager->getShareByToken($token);
145
		} catch (HintException $e) {
146
			return new JSONResponse(['message' => $e->getHint()], Http::STATUS_BAD_REQUEST);
147
		}
148
149
		// make sure that user is authenticated in case of a password protected link
150
		$storedPassword = $share->getPassword();
151
		$authenticated = $this->session->get('public_link_authenticated') === $share->getId() ||
152
			$this->shareManager->checkPassword($share, $password);
153
		if (!empty($storedPassword) && !$authenticated ) {
154
			$response = new JSONResponse(
155
				['message' => 'No permission to access the share'],
156
				Http::STATUS_BAD_REQUEST
157
			);
158
			$response->throttle();
159
			return $response;
160
		}
161
162
		$share->setSharedWith($shareWith);
163
164
		try {
165
			$this->federatedShareProvider->create($share);
166
		} catch (\Exception $e) {
167
			\OC::$server->getLogger()->logException($e, [
0 ignored issues
show
Documentation introduced by
$e is of type object<Exception>, but the function expects a object<Throwable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
168
				'level' => \OCP\Util::WARN,
169
				'app' => 'federatedfilesharing',
170
			]);
171
			return new JSONResponse(['message' => $e->getMessage()], Http::STATUS_BAD_REQUEST);
172
		}
173
174
		return new JSONResponse(['remoteUrl' => $server]);
175
	}
176
177
	/**
178
	 * ask other server to get a federated share
179
	 *
180
	 * @NoAdminRequired
181
	 *
182
	 * @param string $token
183
	 * @param string $remote
184
	 * @param string $password
185
	 * @param string $owner (only for legacy reasons, can be removed with legacyMountPublicLink())
186
	 * @param string $ownerDisplayName (only for legacy reasons, can be removed with legacyMountPublicLink())
187
	 * @param string $name (only for legacy reasons, can be removed with legacyMountPublicLink())
188
	 * @return JSONResponse
189
	 */
190
	public function askForFederatedShare($token, $remote, $password = '', $owner = '', $ownerDisplayName = '', $name = '') {
0 ignored issues
show
Unused Code introduced by
The parameter $owner is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $ownerDisplayName is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $name is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
191
		// check if server admin allows to mount public links from other servers
192
		if ($this->federatedShareProvider->isIncomingServer2serverShareEnabled() === false) {
193
			return new JSONResponse(['message' => $this->l->t('Server to server sharing is not enabled on this server')], Http::STATUS_BAD_REQUEST);
194
		}
195
196
		$cloudId = $this->cloudIdManager->getCloudId($this->userSession->getUser()->getUID(), $this->addressHandler->generateRemoteURL());
197
198
		$httpClient = $this->clientService->newClient();
199
200
		try {
201
			$response = $httpClient->post($remote . '/index.php/apps/federatedfilesharing/createFederatedShare',
202
				[
203
					'body' =>
204
						[
205
							'token' => $token,
206
							'shareWith' => rtrim($cloudId->getId(), '/'),
207
							'password' => $password
208
						],
209
					'connect_timeout' => 10,
210
				]
211
			);
212
		} catch (\Exception $e) {
213
			if (empty($password)) {
214
				$message = $this->l->t("Couldn't establish a federated share.");
215
			} else {
216
				$message = $this->l->t("Couldn't establish a federated share, maybe the password was wrong.");
217
			}
218
			return new JSONResponse(['message' => $message], Http::STATUS_BAD_REQUEST);
219
		}
220
221
		$body = $response->getBody();
222
		$result = json_decode($body, true);
223
224
		if (is_array($result) && isset($result['remoteUrl'])) {
225
			return new JSONResponse(['message' => $this->l->t('Federated Share request sent, you will receive an invitation. Check your notifications.')]);
226
		}
227
228
		// if we doesn't get the expected response we assume that we try to add
229
		// a federated share from a Nextcloud <= 9 server
230
		$message = $this->l->t("Couldn't establish a federated share, it looks like the server to federate with is too old (Nextcloud <= 9).");
231
		return new JSONResponse(['message' => $message], Http::STATUS_BAD_REQUEST);
232
	}
233
}
234