Completed
Push — master ( 035890...e48fa1 )
by Lukas
08:38
created

DIContainer   C

Complexity

Total Complexity 16

Size/Duplication

Total Lines 488
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 19

Importance

Changes 0
Metric Value
dl 0
loc 488
rs 6.875
c 0
b 0
f 0
wmc 16
lcom 2
cbo 19

11 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 379 2
A getCoreApi() 0 4 1
A getServer() 0 4 1
A registerMiddleWare() 0 3 1
A getAppName() 0 3 1
A isLoggedIn() 0 3 1
A isAdminUser() 0 4 1
A getUserId() 0 3 1
B log() 0 20 5
A registerCapability() 0 5 1
A query() 0 3 1
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Bernhard Posselt <[email protected]>
6
 * @author Christoph Wurst <[email protected]>
7
 * @author Joas Schilling <[email protected]>
8
 * @author Jörn Friedrich Dreyer <[email protected]>
9
 * @author Lukas Reschke <[email protected]>
10
 * @author Morris Jobke <[email protected]>
11
 * @author Robin McCorkell <[email protected]>
12
 * @author Roeland Jago Douma <[email protected]>
13
 * @author Thomas Müller <[email protected]>
14
 * @author Thomas Tanghus <[email protected]>
15
 *
16
 * @license AGPL-3.0
17
 *
18
 * This code is free software: you can redistribute it and/or modify
19
 * it under the terms of the GNU Affero General Public License, version 3,
20
 * as published by the Free Software Foundation.
21
 *
22
 * This program is distributed in the hope that it will be useful,
23
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25
 * GNU Affero General Public License for more details.
26
 *
27
 * You should have received a copy of the GNU Affero General Public License, version 3,
28
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
29
 *
30
 */
31
32
33
namespace OC\AppFramework\DependencyInjection;
34
35
use OC;
36
use OC\AppFramework\Core\API;
37
use OC\AppFramework\Http;
38
use OC\AppFramework\Http\Dispatcher;
39
use OC\AppFramework\Http\Output;
40
use OC\AppFramework\Middleware\MiddlewareDispatcher;
41
use OC\AppFramework\Middleware\Security\CORSMiddleware;
42
use OC\AppFramework\Middleware\OCSMiddleware;
43
use OC\AppFramework\Middleware\Security\SecurityMiddleware;
44
use OC\AppFramework\Middleware\SessionMiddleware;
45
use OC\AppFramework\Utility\SimpleContainer;
46
use OC\Core\Middleware\TwoFactorMiddleware;
47
use OC\RichObjectStrings\Validator;
48
use OCP\AppFramework\IApi;
49
use OCP\AppFramework\IAppContainer;
50
use OCP\Files\IAppData;
51
use OCP\Files\Mount\IMountManager;
52
use OCP\RichObjectStrings\IValidator;
53
54
class DIContainer extends SimpleContainer implements IAppContainer {
55
56
	/**
57
	 * @var array
58
	 */
59
	private $middleWares = array();
60
61
	/**
62
	 * Put your class dependencies in here
63
	 * @param string $appName the name of the app
64
	 */
65
	public function __construct($appName, $urlParams = array()){
66
		parent::__construct();
67
		$this['AppName'] = $appName;
68
		$this['urlParams'] = $urlParams;
69
70
		/** @var \OC\ServerContainer $server */
71
		$server = $this->getServer();
72
		$server->registerAppContainer($appName, $this);
73
74
		// aliases
75
		$this->registerAlias('appName', 'AppName');
76
		$this->registerAlias('webRoot', 'WebRoot');
77
		$this->registerAlias('userId', 'UserId');
78
79
		/**
80
		 * Core services
81
		 */
82
		$this->registerService('OCP\\IAppConfig', function($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c 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...
83
			return $this->getServer()->getAppConfig();
84
		});
85
86
		$this->registerService('OCP\\App\\IAppManager', function($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c 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...
87
			return $this->getServer()->getAppManager();
88
		});
89
90
		$this->registerService('OCP\\AppFramework\\Http\\IOutput', function($c){
0 ignored issues
show
Unused Code introduced by
The parameter $c 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...
91
			return new Output($this->getServer()->getWebRoot());
92
		});
93
94
		$this->registerService('OCP\\IAvatarManager', function($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c 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...
95
			return $this->getServer()->getAvatarManager();
96
		});
97
98
		$this->registerService('OCP\\Activity\\IManager', function($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c 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...
99
			return $this->getServer()->getActivityManager();
100
		});
101
102
		$this->registerService('OCP\\ICache', function($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c 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...
103
			return $this->getServer()->getCache();
104
		});
105
106
		$this->registerService('OCP\\ICacheFactory', function($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c 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...
107
			return $this->getServer()->getMemCacheFactory();
108
		});
109
110
		$this->registerService('OC\\CapabilitiesManager', function($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c 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...
111
			return $this->getServer()->getCapabilitiesManager();
112
		});
113
114
		$this->registerService('OCP\Comments\ICommentsManager', function($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c 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...
115
			return $this->getServer()->getCommentsManager();
116
		});
117
118
		$this->registerService('OCP\\IConfig', function($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c 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...
119
			return $this->getServer()->getConfig();
120
		});
121
122
		$this->registerService('OCP\\Contacts\\IManager', function($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c 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...
123
			return $this->getServer()->getContactsManager();
124
		});
125
126
		$this->registerService('OCP\\IDateTimeZone', function($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c 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...
127
			return $this->getServer()->getDateTimeZone();
128
		});
129
130
		$this->registerService('OCP\\IDateTimeFormatter', function($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c 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...
131
			return $this->getServer()->getDateTimeFormatter();
132
		});
133
134
		$this->registerService('OCP\\IDb', function($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c 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...
135
			return $this->getServer()->getDb();
0 ignored issues
show
Deprecated Code introduced by
The method OCP\IServerContainer::getDb() has been deprecated with message: 8.1.0 use getDatabaseConnection, will be removed in ownCloud 10

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
136
		});
137
138
		$this->registerService('OCP\\IDBConnection', function($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c 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...
139
			return $this->getServer()->getDatabaseConnection();
140
		});
141
142
		$this->registerService('OCP\\Diagnostics\\IEventLogger', function($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c 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...
143
			return $this->getServer()->getEventLogger();
144
		});
145
146
		$this->registerService('OCP\\Diagnostics\\IQueryLogger', function($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c 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...
147
			return $this->getServer()->getQueryLogger();
148
		});
149
150
		$this->registerService('OCP\\Files\\IMimeTypeDetector', function($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c 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...
151
			return $this->getServer()->getMimeTypeDetector();
152
		});
153
154
		$this->registerService('OCP\\Files\\Config\\IMountProviderCollection', function($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c 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...
155
			return $this->getServer()->getMountProviderCollection();
156
		});
157
158
		$this->registerService('OCP\\Files\\Config\\IUserMountCache', function($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c 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...
159
			return $this->getServer()->getUserMountCache();
160
		});
161
162
		$this->registerService('OCP\\Files\\IRootFolder', function($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c 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...
163
			return $this->getServer()->getRootFolder();
164
		});
165
166
		$this->registerService('OCP\\Files\\Folder', function() {
167
			return $this->getServer()->getUserFolder();
168
		});
169
170
		$this->registerService('OCP\\Http\\Client\\IClientService', function($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c 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...
171
			return $this->getServer()->getHTTPClientService();
172
		});
173
174
		$this->registerService(IAppData::class, function (SimpleContainer $c) {
175
			return $this->getServer()->getAppDataDir($c->query('AppName'));
176
		});
177
178
		$this->registerService('OCP\\IGroupManager', function($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c 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...
179
			return $this->getServer()->getGroupManager();
180
		});
181
182
		$this->registerService('OCP\\Http\\Client\\IClientService', function() {
183
			return $this->getServer()->getHTTPClientService();
184
		});
185
186
		$this->registerService('OCP\\IL10N', function($c) {
187
			return $this->getServer()->getL10N($c->query('AppName'));
188
		});
189
190
		$this->registerService('OCP\\L10N\\IFactory', function($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c 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
			return $this->getServer()->getL10NFactory();
192
		});
193
194
		$this->registerService('OCP\\ILogger', function($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c 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...
195
			return $this->getServer()->getLogger();
196
		});
197
198
		$this->registerService('OCP\\BackgroundJob\\IJobList', function($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c 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...
199
			return $this->getServer()->getJobList();
200
		});
201
202
		$this->registerAlias('OCP\\AppFramework\\Utility\\IControllerMethodReflector', 'OC\AppFramework\Utility\ControllerMethodReflector');
203
		$this->registerAlias('ControllerMethodReflector', 'OCP\\AppFramework\\Utility\\IControllerMethodReflector');
204
205
		$this->registerService('OCP\\Files\\IMimeTypeDetector', function($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c 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...
206
			return $this->getServer()->getMimeTypeDetector();
207
		});
208
209
		$this->registerService('OCP\\Mail\\IMailer', function() {
210
			return $this->getServer()->getMailer();
211
		});
212
213
		$this->registerService('OCP\\INavigationManager', function($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c 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...
214
			return $this->getServer()->getNavigationManager();
215
		});
216
217
		$this->registerService('OCP\\Notification\IManager', function($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c 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...
218
			return $this->getServer()->getNotificationManager();
219
		});
220
221
		$this->registerService('OCP\\IPreview', function($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c 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...
222
			return $this->getServer()->getPreviewManager();
223
		});
224
225
		$this->registerService('OCP\\IRequest', function () {
226
			return $this->getServer()->getRequest();
227
		});
228
		$this->registerAlias('Request', 'OCP\\IRequest');
229
230
		$this->registerService('OCP\\ITagManager', function($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c 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...
231
			return $this->getServer()->getTagManager();
232
		});
233
234
		$this->registerService('OCP\\ITempManager', function($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c 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...
235
			return $this->getServer()->getTempManager();
236
		});
237
238
		$this->registerAlias('OCP\\AppFramework\\Utility\\ITimeFactory', 'OC\AppFramework\Utility\TimeFactory');
239
		$this->registerAlias('TimeFactory', 'OCP\\AppFramework\\Utility\\ITimeFactory');
240
241
242
		$this->registerService('OCP\\Route\\IRouter', function($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c 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...
243
			return $this->getServer()->getRouter();
244
		});
245
246
		$this->registerService('OCP\\ISearch', function($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c 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...
247
			return $this->getServer()->getSearch();
248
		});
249
250
		$this->registerService('OCP\\ISearch', function($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c 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...
251
			return $this->getServer()->getSearch();
252
		});
253
254
		$this->registerService('OCP\\Security\\ICrypto', function($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c 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
			return $this->getServer()->getCrypto();
256
		});
257
258
		$this->registerService('OCP\\Security\\IHasher', function($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c 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...
259
			return $this->getServer()->getHasher();
260
		});
261
262
		$this->registerService('OCP\\Security\\ICredentialsManager', function($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c 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...
263
			return $this->getServer()->getCredentialsManager();
264
		});
265
266
		$this->registerService('OCP\\Security\\ISecureRandom', function($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c 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...
267
			return $this->getServer()->getSecureRandom();
268
		});
269
270
		$this->registerService('OCP\\Share\\IManager', function($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c 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...
271
			return $this->getServer()->getShareManager();
272
		});
273
274
		$this->registerService('OCP\\SystemTag\\ISystemTagManager', function() {
275
			return $this->getServer()->getSystemTagManager();
276
		});
277
278
		$this->registerService('OCP\\SystemTag\\ISystemTagObjectMapper', function() {
279
			return $this->getServer()->getSystemTagObjectMapper();
280
		});
281
282
		$this->registerService('OCP\\IURLGenerator', function($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c 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...
283
			return $this->getServer()->getURLGenerator();
284
		});
285
286
		$this->registerService('OCP\\IUserManager', function($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c 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...
287
			return $this->getServer()->getUserManager();
288
		});
289
290
		$this->registerService('OCP\\IUserSession', function($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c 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...
291
			return $this->getServer()->getUserSession();
292
		});
293
294
		$this->registerService('OCP\\ISession', function($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c 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...
295
			return $this->getServer()->getSession();
296
		});
297
298
		$this->registerService('OCP\\Security\\IContentSecurityPolicyManager', function($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c 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...
299
			return $this->getServer()->getContentSecurityPolicyManager();
300
		});
301
302
		$this->registerService('ServerContainer', function ($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c 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...
303
			return $this->getServer();
304
		});
305
		$this->registerAlias('OCP\\IServerContainer', 'ServerContainer');
306
307
		$this->registerService('Symfony\Component\EventDispatcher\EventDispatcherInterface', function ($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c 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...
308
			return $this->getServer()->getEventDispatcher();
309
		});
310
311
		$this->registerService('OCP\WorkflowEngine\IManager', function ($c) {
312
			return $c->query('OCA\WorkflowEngine\Manager');
313
		});
314
315
		$this->registerService('OCP\\AppFramework\\IAppContainer', function ($c) {
316
			return $c;
317
		});
318
		$this->registerService(IMountManager::class, function () {
319
			return $this->getServer()->getMountManager();
320
		});
321
322
		// commonly used attributes
323
		$this->registerService('UserId', function ($c) {
324
			return $c->query('OCP\\IUserSession')->getSession()->get('user_id');
325
		});
326
327
		$this->registerService('WebRoot', function ($c) {
328
			return $c->query('ServerContainer')->getWebRoot();
329
		});
330
331
		$this->registerService('OCP\Encryption\IManager', function ($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c 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...
332
			return $this->getServer()->getEncryptionManager();
333
		});
334
335
		$this->registerService(IValidator::class, function($c) {
336
			return $c->query(Validator::class);
337
		});
338
339
340
		/**
341
		 * App Framework APIs
342
		 */
343
		$this->registerService('API', function($c){
344
			$c->query('OCP\\ILogger')->debug(
345
				'Accessing the API class is deprecated! Use the appropriate ' .
346
				'services instead!'
347
			);
348
			return new API($c['AppName']);
0 ignored issues
show
Deprecated Code introduced by
The class OC\AppFramework\Core\API has been deprecated.

This class, trait or interface has been deprecated.

Loading history...
349
		});
350
351
		$this->registerService('Protocol', function($c){
352
			/** @var \OC\Server $server */
353
			$server = $c->query('ServerContainer');
354
			$protocol = $server->getRequest()->getHttpProtocol();
355
			return new Http($_SERVER, $protocol);
356
		});
357
358
		$this->registerService('Dispatcher', function($c) {
359
			return new Dispatcher(
360
				$c['Protocol'],
361
				$c['MiddlewareDispatcher'],
362
				$c['ControllerMethodReflector'],
363
				$c['Request']
364
			);
365
		});
366
367
		/**
368
		 * App Framework default arguments
369
		 */
370
		$this->registerParameter('corsMethods', 'PUT, POST, GET, DELETE, PATCH');
371
		$this->registerParameter('corsAllowedHeaders', 'Authorization, Content-Type, Accept');
372
		$this->registerParameter('corsMaxAge', 1728000);
373
374
		/**
375
		 * Middleware
376
		 */
377
		$app = $this;
378
		$this->registerService('SecurityMiddleware', function($c) use ($app){
379
			return new SecurityMiddleware(
380
				$c['Request'],
381
				$c['ControllerMethodReflector'],
382
				$app->getServer()->getNavigationManager(),
383
				$app->getServer()->getURLGenerator(),
384
				$app->getServer()->getLogger(),
385
				$c['AppName'],
386
				$app->isLoggedIn(),
0 ignored issues
show
Deprecated Code introduced by
The method OC\AppFramework\Dependen...Container::isLoggedIn() has been deprecated with message: use IUserSession->isLoggedIn()

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
387
				$app->isAdminUser(),
0 ignored issues
show
Deprecated Code introduced by
The method OC\AppFramework\Dependen...ontainer::isAdminUser() has been deprecated with message: use IGroupManager->isAdmin($userId)

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
388
				$app->getServer()->getContentSecurityPolicyManager(),
0 ignored issues
show
Compatibility introduced by
$app->getServer()->getCo...SecurityPolicyManager() of type object<OCP\Security\ICon...tSecurityPolicyManager> is not a sub-type of object<OC\Security\CSP\C...tSecurityPolicyManager>. It seems like you assume a concrete implementation of the interface OCP\Security\IContentSecurityPolicyManager to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
389
				$app->getServer()->getCsrfTokenManager(),
390
				$app->getServer()->getContentSecurityPolicyNonceManager()
0 ignored issues
show
Bug introduced by
The method getContentSecurityPolicyNonceManager() does not exist on OCP\IServerContainer. Did you maybe mean getContentSecurityPolicyManager()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
391
			);
392
		});
393
394
		$this->registerService('CORSMiddleware', function($c) {
395
			return new CORSMiddleware(
396
				$c['Request'],
397
				$c['ControllerMethodReflector'],
398
				$c['OCP\IUserSession'],
399
				$c->getServer()->getBruteForceThrottler()
400
			);
401
		});
402
403
		$this->registerService('SessionMiddleware', function($c) use ($app) {
404
			return new SessionMiddleware(
405
				$c['Request'],
406
				$c['ControllerMethodReflector'],
407
				$app->getServer()->getSession()
408
			);
409
		});
410
411
		$this->registerService('TwoFactorMiddleware', function (SimpleContainer $c) use ($app) {
412
			$twoFactorManager = $c->getServer()->getTwoFactorAuthManager();
413
			$userSession = $app->getServer()->getUserSession();
414
			$session = $app->getServer()->getSession();
415
			$urlGenerator = $app->getServer()->getURLGenerator();
416
			$reflector = $c['ControllerMethodReflector'];
417
			$request = $app->getServer()->getRequest();
418
			return new TwoFactorMiddleware($twoFactorManager, $userSession, $session, $urlGenerator, $reflector, $request);
0 ignored issues
show
Compatibility introduced by
$userSession of type object<OCP\IUserSession> is not a sub-type of object<OC\User\Session>. It seems like you assume a concrete implementation of the interface OCP\IUserSession to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
419
		});
420
421
		$this->registerService('OCSMiddleware', function (SimpleContainer $c) {
422
			return new OCSMiddleware(
423
				$c['Request']
424
			);
425
		});
426
427
		$middleWares = &$this->middleWares;
428
		$this->registerService('MiddlewareDispatcher', function($c) use (&$middleWares) {
429
			$dispatcher = new MiddlewareDispatcher();
430
			$dispatcher->registerMiddleware($c['CORSMiddleware']);
431
			$dispatcher->registerMiddleware($c['OCSMiddleware']);
432
			$dispatcher->registerMiddleware($c['SecurityMiddleware']);
433
			$dispatcher->registerMiddleWare($c['TwoFactorMiddleware']);
434
435
			foreach($middleWares as $middleWare) {
436
				$dispatcher->registerMiddleware($c[$middleWare]);
437
			}
438
439
			$dispatcher->registerMiddleware($c['SessionMiddleware']);
440
			return $dispatcher;
441
		});
442
443
	}
444
445
446
	/**
447
	 * @deprecated implements only deprecated methods
448
	 * @return IApi
449
	 */
450
	function getCoreApi()
451
	{
452
		return $this->query('API');
453
	}
454
455
	/**
456
	 * @return \OCP\IServerContainer
457
	 */
458
	function getServer()
459
	{
460
		return OC::$server;
461
	}
462
463
	/**
464
	 * @param string $middleWare
465
	 * @return boolean|null
466
	 */
467
	function registerMiddleWare($middleWare) {
468
		array_push($this->middleWares, $middleWare);
469
	}
470
471
	/**
472
	 * used to return the appname of the set application
473
	 * @return string the name of your application
474
	 */
475
	function getAppName() {
476
		return $this->query('AppName');
477
	}
478
479
	/**
480
	 * @deprecated use IUserSession->isLoggedIn()
481
	 * @return boolean
482
	 */
483
	function isLoggedIn() {
484
		return \OC_User::isLoggedIn();
0 ignored issues
show
Deprecated Code introduced by
The method OC_User::isLoggedIn() has been deprecated with message: use \OC::$server->getUserSession()->isLoggedIn()

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
485
	}
486
487
	/**
488
	 * @deprecated use IGroupManager->isAdmin($userId)
489
	 * @return boolean
490
	 */
491
	function isAdminUser() {
492
		$uid = $this->getUserId();
493
		return \OC_User::isAdminUser($uid);
494
	}
495
496
	private function getUserId() {
497
		return $this->getServer()->getSession()->get('user_id');
498
	}
499
500
	/**
501
	 * @deprecated use the ILogger instead
502
	 * @param string $message
503
	 * @param string $level
504
	 * @return mixed
505
	 */
506
	function log($message, $level) {
507
		switch($level){
508
			case 'debug':
509
				$level = \OCP\Util::DEBUG;
510
				break;
511
			case 'info':
512
				$level = \OCP\Util::INFO;
513
				break;
514
			case 'warn':
515
				$level = \OCP\Util::WARN;
516
				break;
517
			case 'fatal':
518
				$level = \OCP\Util::FATAL;
519
				break;
520
			default:
521
				$level = \OCP\Util::ERROR;
522
				break;
523
		}
524
		\OCP\Util::writeLog($this->getAppName(), $message, $level);
525
	}
526
527
	/**
528
	 * Register a capability
529
	 *
530
	 * @param string $serviceName e.g. 'OCA\Files\Capabilities'
531
	 */
532
	public function registerCapability($serviceName) {
533
		$this->query('OC\CapabilitiesManager')->registerCapability(function() use ($serviceName) {
534
			return $this->query($serviceName);
535
		});
536
	}
537
538
	public function query($name) {
539
		return parent::query($name);
540
	}
541
}
542