Completed
Pull Request — master (#32767)
by
unknown
10:15
created

DIContainer::log()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
nc 5
nop 2
dl 0
loc 20
rs 9.2888
c 0
b 0
f 0
1
<?php
2
/**
3
 * @author Bernhard Posselt <[email protected]>
4
 * @author Christoph Wurst <[email protected]>
5
 * @author Joas Schilling <[email protected]>
6
 * @author Jörn Friedrich Dreyer <[email protected]>
7
 * @author Lukas Reschke <[email protected]>
8
 * @author Morris Jobke <[email protected]>
9
 * @author Robin McCorkell <[email protected]>
10
 * @author Roeland Jago Douma <[email protected]>
11
 * @author Thomas Müller <[email protected]>
12
 * @author Thomas Tanghus <[email protected]>
13
 *
14
 * @copyright Copyright (c) 2018, ownCloud GmbH
15
 * @license AGPL-3.0
16
 *
17
 * This code is free software: you can redistribute it and/or modify
18
 * it under the terms of the GNU Affero General Public License, version 3,
19
 * as published by the Free Software Foundation.
20
 *
21
 * This program is distributed in the hope that it will be useful,
22
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24
 * GNU Affero General Public License for more details.
25
 *
26
 * You should have received a copy of the GNU Affero General Public License, version 3,
27
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
28
 *
29
 */
30
31
namespace OC\AppFramework\DependencyInjection;
32
33
use OC;
34
use OC\AppFramework\Core\API;
35
use OC\AppFramework\Http;
36
use OC\AppFramework\Http\Dispatcher;
37
use OC\AppFramework\Http\Output;
38
use OC\AppFramework\Middleware\MiddlewareDispatcher;
39
use OC\AppFramework\Middleware\Security\CORSMiddleware;
40
use OC\AppFramework\Middleware\Security\SecurityMiddleware;
41
use OC\AppFramework\Middleware\SessionMiddleware;
42
use OC\AppFramework\Utility\SimpleContainer;
43
use OC\Core\Middleware\AccountModuleMiddleware;
44
use OC\Core\Middleware\TwoFactorMiddleware;
45
use OCP\App\IServiceLoader;
46
use OCP\AppFramework\IAppContainer;
47
use OCP\Files\Mount\IMountManager;
48
use OCP\IDateTimeFormatter;
49
50
class DIContainer extends SimpleContainer implements IAppContainer {
51
52
	/**
53
	 * @var array
54
	 */
55
	private $middleWares = [];
56
57
	/**
58
	 * Put your class dependencies in here
59
	 * @param string $appName the name of the app
60
	 */
61
	public function __construct($appName, $urlParams = []) {
62
		parent::__construct();
63
		$this['AppName'] = $appName;
64
		$this['urlParams'] = $urlParams;
65
66
		/** @var \OC\ServerContainer $server */
67
		$server = $this->getServer();
68
		$server->registerAppContainer($appName, $this);
69
70
		// aliases
71
		$this->registerAlias('appName', 'AppName');
72
		$this->registerAlias('webRoot', 'WebRoot');
73
		$this->registerAlias('userId', 'UserId');
74
75
		/**
76
		 * Core services
77
		 */
78
		$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...
79
			return $this->getServer()->getAppConfig();
80
		});
81
82
		$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...
83
			return $this->getServer()->getAppManager();
84
		});
85
86
		$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...
87
			return new Output($this->getServer()->getWebRoot());
88
		});
89
90
		$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...
91
			return $this->getServer()->getAvatarManager();
92
		});
93
94
		$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...
95
			return $this->getServer()->getActivityManager();
96
		});
97
98
		$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...
99
			return $this->getServer()->getCache();
100
		});
101
102
		$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...
103
			return $this->getServer()->getMemCacheFactory();
104
		});
105
106
		$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...
107
			return $this->getServer()->getCapabilitiesManager();
108
		});
109
110
		$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...
111
			return $this->getServer()->getCommentsManager();
112
		});
113
114
		$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...
115
			return $this->getServer()->getConfig();
116
		});
117
118
		$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...
119
			return $this->getServer()->getContactsManager();
120
		});
121
122
		$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...
123
			return $this->getServer()->getDateTimeZone();
124
		});
125
126
		$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...
127
			return $this->getServer()->getDb();
128
		});
129
130
		$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...
131
			return $this->getServer()->getDatabaseConnection();
132
		});
133
134
		$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...
135
			return $this->getServer()->getEventLogger();
136
		});
137
138
		$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...
139
			return $this->getServer()->getQueryLogger();
140
		});
141
142
		$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...
143
			return $this->getServer()->getMimeTypeDetector();
144
		});
145
146
		$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...
147
			return $this->getServer()->getMountProviderCollection();
148
		});
149
150
		$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...
151
			return $this->getServer()->getRootFolder();
152
		});
153
154
		$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...
155
			return $this->getServer()->getHTTPClientService();
156
		});
157
158
		$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...
159
			return $this->getServer()->getGroupManager();
160
		});
161
162
		$this->registerService('OCP\\Http\\Client\\IClientService', function () {
163
			return $this->getServer()->getHTTPClientService();
164
		});
165
166
		$this->registerService('OCP\\IL10N', function ($c) {
167
			return $this->getServer()->getL10N($c->query('AppName'));
168
		});
169
170
		$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...
171
			return $this->getServer()->getL10NFactory();
172
		});
173
174
		$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...
175
			return $this->getServer()->getLogger();
176
		});
177
178
		$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...
179
			return $this->getServer()->getJobList();
180
		});
181
182
		$this->registerAlias('OCP\\AppFramework\\Utility\\IControllerMethodReflector', 'OC\AppFramework\Utility\ControllerMethodReflector');
183
		$this->registerAlias('ControllerMethodReflector', 'OCP\\AppFramework\\Utility\\IControllerMethodReflector');
184
185
		$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...
186
			return $this->getServer()->getMimeTypeDetector();
187
		});
188
189
		$this->registerService('OCP\\Mail\\IMailer', function () {
190
			return $this->getServer()->getMailer();
191
		});
192
193
		$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...
194
			return $this->getServer()->getNavigationManager();
195
		});
196
197
		$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...
198
			return $this->getServer()->getNotificationManager();
199
		});
200
201
		$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...
202
			return $this->getServer()->getPreviewManager();
203
		});
204
205
		$this->registerService('OCP\\IRequest', function () {
206
			return $this->getServer()->getRequest();
207
		});
208
		$this->registerAlias('Request', 'OCP\\IRequest');
209
210
		$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...
211
			return $this->getServer()->getTagManager();
212
		});
213
214
		$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...
215
			return $this->getServer()->getTempManager();
216
		});
217
218
		$this->registerAlias('OCP\\AppFramework\\Utility\\ITimeFactory', 'OC\AppFramework\Utility\TimeFactory');
219
		$this->registerAlias('TimeFactory', 'OCP\\AppFramework\\Utility\\ITimeFactory');
220
221
		$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...
222
			return $this->getServer()->getRouter();
223
		});
224
225
		$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...
226
			return $this->getServer()->getSearch();
227
		});
228
229
		$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...
230
			return $this->getServer()->getSearch();
231
		});
232
233
		$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...
234
			return $this->getServer()->getCrypto();
235
		});
236
237
		$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...
238
			return $this->getServer()->getHasher();
239
		});
240
241
		$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...
242
			return $this->getServer()->getCredentialsManager();
243
		});
244
245
		$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...
246
			return $this->getServer()->getSecureRandom();
247
		});
248
249
		$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...
250
			return $this->getServer()->getShareManager();
251
		});
252
253
		$this->registerService('OCP\\SystemTag\\ISystemTagManager', function () {
254
			return $this->getServer()->getSystemTagManager();
255
		});
256
257
		$this->registerService('OCP\\SystemTag\\ISystemTagObjectMapper', function () {
258
			return $this->getServer()->getSystemTagObjectMapper();
259
		});
260
261
		$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...
262
			return $this->getServer()->getURLGenerator();
263
		});
264
265
		$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...
266
			return $this->getServer()->getUserManager();
267
		});
268
269
		$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...
270
			return $this->getServer()->getUserSession();
271
		});
272
273
		$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...
274
			return $this->getServer()->getSession();
275
		});
276
277
		$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...
278
			return $this->getServer()->getContentSecurityPolicyManager();
279
		});
280
281
		$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...
282
			return $this->getServer();
283
		});
284
		$this->registerAlias('OCP\\IServerContainer', 'ServerContainer');
285
		$this->registerAlias(IServiceLoader::class, 'ServerContainer');
286
287
		$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...
288
			return $this->getServer()->getEventDispatcher();
289
		});
290
291
		$this->registerService('OCP\\AppFramework\\IAppContainer', function ($c) {
292
			return $c;
293
		});
294
295
		$this->registerService(IDateTimeFormatter::class, function () {
296
			return $this->getServer()->getDateTimeFormatter();
297
		});
298
		$this->registerService(IMountManager::class, function () {
299
			return $this->getServer()->getMountManager();
300
		});
301
302
		// commonly used attributes
303
		$this->registerService('UserId', function ($c) {
304
			return $c->query('OCP\\IUserSession')->getSession()->get('user_id');
305
		});
306
307
		$this->registerService('WebRoot', function ($c) {
308
			return $c->query('ServerContainer')->getWebRoot();
309
		});
310
311
		$this->registerService('Protocol', function ($c) {
312
			/** @var \OC\Server $server */
313
			$server = $c->query('ServerContainer');
314
			$protocol = $server->getRequest()->getHttpProtocol();
315
			return new Http($_SERVER, $protocol);
316
		});
317
318
		$this->registerService('Dispatcher', function ($c) {
319
			return new Dispatcher(
320
				$c['Protocol'],
321
				$c['MiddlewareDispatcher'],
322
				$c['ControllerMethodReflector'],
323
				$c['Request']
324
			);
325
		});
326
327
		$this->registerService('OCP\Theme\IThemeService', 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...
328
			return $this->getServer()->getThemeService();
329
		});
330
331
		/**
332
		 * Middleware
333
		 */
334
		$app = $this;
335
		$this->registerService('SecurityMiddleware', function ($c) use ($app) {
336
			return new SecurityMiddleware(
337
				$c['Request'],
338
				$c['ControllerMethodReflector'],
339
				$app->getServer()->getNavigationManager(),
340
				$app->getServer()->getURLGenerator(),
341
				$app->getServer()->getLogger(),
342
				$app->getServer()->getUserSession(),
343
				$c['AppName'],
344
				$app->isAdminUser(),
345
				$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...
346
			);
347
		});
348
349
		$this->registerService('CORSMiddleware', function ($c) {
350
			return new CORSMiddleware(
351
				$c['Request'],
352
				$c['ControllerMethodReflector'],
353
				$c['OCP\IUserSession'],
354
				$c['OCP\IConfig']
355
			);
356
		});
357
358
		$this->registerService('SessionMiddleware', function ($c) use ($app) {
359
			return new SessionMiddleware(
360
				$c['Request'],
361
				$c['ControllerMethodReflector'],
362
				$app->getServer()->getSession()
363
			);
364
		});
365
366
		$this->registerService('TwoFactorMiddleware', function (SimpleContainer $c) use ($app) {
367
			$twoFactorManager = $c->getServer()->getTwoFactorAuthManager();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class OC\AppFramework\Utility\SimpleContainer as the method getServer() does only exist in the following sub-classes of OC\AppFramework\Utility\SimpleContainer: OC\AppFramework\DependencyInjection\DIContainer. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
368
			$userSession = $app->getServer()->getUserSession();
369
			$urlGenerator = $app->getServer()->getURLGenerator();
370
			$reflector = $c['ControllerMethodReflector'];
371
			$request = $app->getServer()->getRequest();
372
			return new TwoFactorMiddleware($twoFactorManager, $userSession, $urlGenerator, $reflector, $request);
373
		});
374
375
		$middleWares = &$this->middleWares;
376
		$this->registerService('MiddlewareDispatcher', function ($c) use (&$middleWares) {
377
			$dispatcher = new MiddlewareDispatcher();
378
			$dispatcher->registerMiddleware($c['CORSMiddleware']);
379
			$dispatcher->registerMiddleware($c['SecurityMiddleware']);
380
			$dispatcher->registerMiddleware($c['TwoFactorMiddleware']);
381
			$dispatcher->registerMiddleware($c->query(AccountModuleMiddleware::class));
382
383
			foreach ($middleWares as $middleWare) {
384
				$dispatcher->registerMiddleware($c[$middleWare]);
385
			}
386
387
			$dispatcher->registerMiddleware($c['SessionMiddleware']);
388
			return $dispatcher;
389
		});
390
	}
391
392
	/**
393
	 * @return \OCP\IServerContainer
394
	 */
395
	public function getServer() {
396
		return OC::$server;
397
	}
398
399
	/**
400
	 * @param string $middleWare
401
	 * @return boolean|null
402
	 */
403
	public function registerMiddleWare($middleWare) {
404
		\array_push($this->middleWares, $middleWare);
405
	}
406
407
	/**
408
	 * used to return the appname of the set application
409
	 * @return string the name of your application
410
	 */
411
	public function getAppName() {
412
		return $this->query('AppName');
413
	}
414
415
	/**
416
	 * @deprecated use IUserSession->isLoggedIn()
417
	 * @return boolean
418
	 */
419
	public function isLoggedIn() {
420
		return $this->getServer()->getUserSession()->isLoggedIn();
421
	}
422
423
	/**
424
	 * @deprecated use IGroupManager->isAdmin($userId)
425
	 * @return boolean
426
	 */
427
	public function isAdminUser() {
428
		$uid = $this->getUserId();
429
		return \OC_User::isAdminUser($uid);
430
	}
431
432
	private function getUserId() {
433
		return $this->getServer()->getSession()->get('user_id');
434
	}
435
436
	/**
437
	 * @deprecated use the ILogger instead
438
	 * @param string $message
439
	 * @param string $level
440
	 * @return mixed
441
	 */
442
	public function log($message, $level) {
443
		switch ($level) {
444
			case 'debug':
445
				$level = \OCP\Util::DEBUG;
446
				break;
447
			case 'info':
448
				$level = \OCP\Util::INFO;
449
				break;
450
			case 'warn':
451
				$level = \OCP\Util::WARN;
452
				break;
453
			case 'fatal':
454
				$level = \OCP\Util::FATAL;
455
				break;
456
			default:
457
				$level = \OCP\Util::ERROR;
458
				break;
459
		}
460
		\OCP\Util::writeLog($this->getAppName(), $message, $level);
461
	}
462
463
	/**
464
	 * Register a capability
465
	 *
466
	 * @param string $serviceName e.g. 'OCA\Files\Capabilities'
467
	 */
468
	public function registerCapability($serviceName) {
469
		$this->query('OC\CapabilitiesManager')->registerCapability(function () use ($serviceName) {
470
			return $this->query($serviceName);
471
		});
472
	}
473
}
474