Completed
Pull Request — master (#32303)
by Victor
12:27
created

OcmController::discovery()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
1
<?php
2
/**
3
 * @author Viktar Dubiniuk <[email protected]>
4
 *
5
 * @copyright Copyright (c) 2018, ownCloud GmbH
6
 * @license AGPL-3.0
7
 *
8
 * This code is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License, version 3,
10
 * as published by the Free Software Foundation.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License, version 3,
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
19
 *
20
 */
21
22
namespace OCA\FederatedFileSharing\Controller;
23
24
use OCA\FederatedFileSharing\Address;
25
use OCA\FederatedFileSharing\AddressHandler;
26
use OCP\AppFramework\Http\JSONResponse;
27
use OCA\FederatedFileSharing\Exception\InvalidShareException;
28
use OCA\FederatedFileSharing\Exception\NotSupportedException;
29
use OCA\FederatedFileSharing\FedShareManager;
30
use OCP\AppFramework\Controller;
31
use OCP\AppFramework\Http;
32
use OCP\ILogger;
33
use OCP\IRequest;
34
use OCP\IURLGenerator;
35
use OCP\IUserManager;
36
37
/**
38
 * Class OcmController
39
 *
40
 * @package OCA\FederatedFileSharing\Controller
41
 */
42
class OcmController extends Controller {
43
	const API_VERSION = '1.0-proposal1';
44
45
	/**
46
	 * @var IURLGenerator
47
	 */
48
	protected $urlGenerator;
49
50
	/**
51
	 * @var IUserManager
52
	 */
53
	protected $userManager;
54
55
	/**
56
	 * @var AddressHandler
57
	 */
58
	protected $addressHandler;
59
60
	/**
61
	 * @var FedShareManager
62
	 */
63
	protected $fedShareManager;
64
65
	/**
66
	 * @var ILogger
67
	 */
68
	protected $logger;
69
70
	/**
71
	 * OcmController constructor.
72
	 *
73
	 * @param string $appName
74
	 * @param IRequest $request
75
	 * @param IURLGenerator $urlGenerator
76
	 * @param IUserManager $userManager
77
	 * @param AddressHandler $addressHandler
78
	 * @param FedShareManager $fedShareManager
79
	 * @param ILogger $logger
80
	 */
81 View Code Duplication
	public function __construct($appName,
82
									IRequest $request,
83
									IURLGenerator $urlGenerator,
84
									IUserManager $userManager,
85
									AddressHandler $addressHandler,
86
									FedShareManager $fedShareManager,
87
									ILogger $logger
88
	) {
89
		parent::__construct($appName, $request);
90
91
		$this->urlGenerator = $urlGenerator;
92
		$this->userManager = $userManager;
93
		$this->addressHandler = $addressHandler;
94
		$this->fedShareManager = $fedShareManager;
95
		$this->logger = $logger;
96
	}
97
98
	/**
99
	 * @NoCSRFRequired
100
	 * @PublicPage
101
	 *
102
	 * EndPoint discovery
103
	 * Responds to /ocm-provider/ requests
104
	 *
105
	 * @return array
106
	 */
107
	public function discovery() {
108
		return [
109
			'enabled' => true,
110
			'apiVersion' => self::API_VERSION,
111
			'endPoint' => $this->urlGenerator->linkToRouteAbsolute(
112
				"{$this->appName}.ocm.index"
113
			),
114
			'shareTypes' => [
115
				'name' => 'file',
116
				'protocols' => $this->getProtocols()
117
			]
118
		];
119
	}
120
121
	/**
122
	 * @NoCSRFRequired
123
	 * @PublicPage
124
	 *
125
	 *
126
	 *
127
	 * @param string $shareWith identifier of the user or group
128
	 * 							to share the resource with
129
	 * @param string $name name of the shared resource
130
	 * @param string $description share description (optional)
131
	 * @param string $providerId Identifier of the resource at the provider side
132
	 * @param string $owner identifier of the user that owns the resource
133
	 * @param string $ownerDisplayName display name of the owner
134
	 * @param string $sender Provider specific identifier of the user that wants
135
	 * 							to share the resource
136
	 * @param string $senderDisplayName Display name of the user that wants
137
	 * 									to share the resource
138
	 * @param string $shareType Share type (user or group share)
139
	 * @param string $resourceType only 'file' is supported atm
140
	 * @param array $protocol
141
	 * 		[
142
	 * 			'name' => (string) protocol name. Only 'webdav' is supported atm,
143
	 * 			'options' => [
144
	 * 				protocol specific options
145
	 * 				only `webdav` options are supported atm
146
	 * 				e.g. `uri`,	`access_token`, `password`, `permissions` etc.
147
	 *
148
	 * 				For backward compatibility the webdav protocol will use
149
	 * 				the 'sharedSecret" as username and password
150
	 * 			]
151
	 *
152
	 */
153
	public function createShare($shareWith,
154
								$name,
155
								$description,
0 ignored issues
show
Unused Code introduced by
The parameter $description 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...
156
								$providerId,
157
								$owner,
158
								$ownerDisplayName,
159
								$sender,
160
								$senderDisplayName,
161
								$shareType,
162
								$resourceType,
163
								$protocol
164
165
	) {
166
		try {
167
			$hasMissingParams = $this->hasNull(
168
				[$shareWith, $name, $providerId, $owner, $shareType, $resourceType]
169
			);
170
			if ($hasMissingParams
171
				|| !is_array($protocol)
172
				|| !isset($protocol['name'])
173
				|| !isset($protocol['options'])
174
				|| !is_array($protocol['options'])
175
				|| !isset($protocol['options']['sharedSecret'])
176
			) {
177
				throw new InvalidShareException(
178
					'server can not add remote share, missing parameter'
179
				);
180
			}
181
			if (!\OCP\Util::isValidFileName($name)) {
182
				throw new InvalidShareException(
183
					'The mountpoint name contains invalid characters.'
184
				);
185
			}
186
			// FIXME this should be a method in the user management instead
187
			$this->logger->debug(
188
				"shareWith before, $shareWith",
189
				['app' => $this->appName]
190
			);
191
			\OCP\Util::emitHook(
192
				'\OCA\Files_Sharing\API\Server2Server',
193
				'preLoginNameUsedAsUserName',
194
				['uid' => &$shareWith]
195
			);
196
			$this->logger->debug(
197
				"shareWith after, $shareWith",
198
				['app' => $this->appName]
199
			);
200
201
			if (!$this->userManager->userExists($shareWith)) {
202
				throw new InvalidShareException('User does not exist');
203
			}
204
205
			$ownerAddress = new Address($owner, $ownerDisplayName);
206
			$sharedByAddress = new Address($sender, $senderDisplayName);
207
208
			$this->fedShareManager->createShare(
209
				$ownerAddress,
210
				$sharedByAddress,
211
				$shareWith,
212
				$providerId,
213
				$name,
214
				$protocol['options']['sharedSecret']
215
			);
216
		} catch (InvalidShareException $e) {
217
			return new JSONResponse(
218
				null,
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a array|object.

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...
219
				Http::STATUS_BAD_REQUEST,
220
				$e->getMessage()
0 ignored issues
show
Unused Code introduced by
The call to JSONResponse::__construct() has too many arguments starting with $e->getMessage().

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
221
			);
222
		} catch (NotSupportedException $e) {
223
			return new JSONResponse(
224
				null,
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a array|object.

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...
225
				Http::STATUS_SERVICE_UNAVAILABLE,
226
				'Server does not support federated cloud sharing'
0 ignored issues
show
Unused Code introduced by
The call to JSONResponse::__construct() has too many arguments starting with 'Server does not support federated cloud sharing'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
227
			);
228
		} catch (\Exception $e) {
229
			\OCP\Util::writeLog(
230
				'files_sharing',
231
				'server can not add remote share, ' . $e->getMessage(),
232
				\OCP\Util::ERROR
233
			);
234
			return new JSONResponse(
235
				null,
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a array|object.

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...
236
				Http::STATUS_INTERNAL_SERVER_ERROR,
237
				'internal server error, was not able to add share from ' . $remote
0 ignored issues
show
Bug introduced by
The variable $remote does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Unused Code introduced by
The call to JSONResponse::__construct() has too many arguments starting with 'internal server error, ... share from ' . $remote.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
238
			);
239
		}
240
		return new JSONResponse();
241
	}
242
243
	/**
244
	 * @param string $notificationType notification type (SHARE_REMOVED, etc)
245
	 * @param string $resourceType only 'file' is supported atm
246
	 * @param string $providerId Identifier of the resource at the provider side
247
	 * @param array $notification
248
	 * 		[
249
	 * 			optional additional parameters, depending on the notification
250
	 * 				and the resource type
251
	 * 		]
252
	 */
253
	public function processNotification($notificationType,
0 ignored issues
show
Unused Code introduced by
The parameter $notificationType 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...
254
										$resourceType,
0 ignored issues
show
Unused Code introduced by
The parameter $resourceType 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...
255
										$providerId,
0 ignored issues
show
Unused Code introduced by
The parameter $providerId 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...
256
										$notification
0 ignored issues
show
Unused Code introduced by
The parameter $notification 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...
257
	) {
258
		// TODO: implement
259
	}
260
261
	protected function getProtocols() {
262
		return [
263
			'webdav' => '/public.php/webdav/'
264
		];
265
	}
266
267
	/**
268
	 * Check if value is null or an array has any null item
269
	 *
270
	 * @param mixed $param
271
	 *
272
	 * @return bool
273
	 */
274 View Code Duplication
	protected function hasNull($param) {
275
		if (\is_array($param)) {
276
			return \in_array(null, $param, true);
277
		} else {
278
			return $param === null;
279
		}
280
	}
281
}
282