Completed
Pull Request — master (#32767)
by
unknown
17:39 queued 06:53
created

DIContainer::getAppName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 3
rs 10
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
		/**
312
		 * App Framework APIs
313
		 */
314
		$this->registerService('API', function ($c) {
315
			$c->query('OCP\\ILogger')->debug(
316
				'Accessing the API class is deprecated! Use the appropriate ' .
317
				'services instead!'
318
			);
319
			return new API($c['AppName']);
320
		});
321
322
		$this->registerService('Protocol', function ($c) {
323
			/** @var \OC\Server $server */
324
			$server = $c->query('ServerContainer');
325
			$protocol = $server->getRequest()->getHttpProtocol();
326
			return new Http($_SERVER, $protocol);
327
		});
328
329
		$this->registerService('Dispatcher', function ($c) {
330
			return new Dispatcher(
331
				$c['Protocol'],
332
				$c['MiddlewareDispatcher'],
333
				$c['ControllerMethodReflector'],
334
				$c['Request']
335
			);
336
		});
337
338
		$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...
339
			return $this->getServer()->getThemeService();
340
		});
341
342
		/**
343
		 * Middleware
344
		 */
345
		$app = $this;
346
		$this->registerService('SecurityMiddleware', function ($c) use ($app) {
347
			return new SecurityMiddleware(
348
				$c['Request'],
349
				$c['ControllerMethodReflector'],
350
				$app->getServer()->getNavigationManager(),
351
				$app->getServer()->getURLGenerator(),
352
				$app->getServer()->getLogger(),
353
				$app->getServer()->getUserSession(),
354
				$c['AppName'],
355
				$app->isAdminUser(),
356
				$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...
357
			);
358
		});
359
360
		$this->registerService('CORSMiddleware', function ($c) {
361
			return new CORSMiddleware(
362
				$c['Request'],
363
				$c['ControllerMethodReflector'],
364
				$c['OCP\IUserSession'],
365
				$c['OCP\IConfig']
366
			);
367
		});
368
369
		$this->registerService('SessionMiddleware', function ($c) use ($app) {
370
			return new SessionMiddleware(
371
				$c['Request'],
372
				$c['ControllerMethodReflector'],
373
				$app->getServer()->getSession()
374
			);
375
		});
376
377
		$this->registerService('TwoFactorMiddleware', function (SimpleContainer $c) use ($app) {
378
			$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...
379
			$userSession = $app->getServer()->getUserSession();
380
			$urlGenerator = $app->getServer()->getURLGenerator();
381
			$reflector = $c['ControllerMethodReflector'];
382
			$request = $app->getServer()->getRequest();
383
			return new TwoFactorMiddleware($twoFactorManager, $userSession, $urlGenerator, $reflector, $request);
384
		});
385
386
		$middleWares = &$this->middleWares;
387
		$this->registerService('MiddlewareDispatcher', function ($c) use (&$middleWares) {
388
			$dispatcher = new MiddlewareDispatcher();
389
			$dispatcher->registerMiddleware($c['CORSMiddleware']);
390
			$dispatcher->registerMiddleware($c['SecurityMiddleware']);
391
			$dispatcher->registerMiddleware($c['TwoFactorMiddleware']);
392
			$dispatcher->registerMiddleware($c->query(AccountModuleMiddleware::class));
393
394
			foreach ($middleWares as $middleWare) {
395
				$dispatcher->registerMiddleware($c[$middleWare]);
396
			}
397
398
			$dispatcher->registerMiddleware($c['SessionMiddleware']);
399
			return $dispatcher;
400
		});
401
	}
402
403
	/**
404
	 * @return \OCP\IServerContainer
405
	 */
406
	public function getServer() {
407
		return OC::$server;
408
	}
409
410
	/**
411
	 * @param string $middleWare
412
	 * @return boolean|null
413
	 */
414
	public function registerMiddleWare($middleWare) {
415
		\array_push($this->middleWares, $middleWare);
416
	}
417
418
	/**
419
	 * used to return the appname of the set application
420
	 * @return string the name of your application
421
	 */
422
	public function getAppName() {
423
		return $this->query('AppName');
424
	}
425
426
	/**
427
	 * @deprecated use IUserSession->isLoggedIn()
428
	 * @return boolean
429
	 */
430
	public function isLoggedIn() {
431
		return $this->getServer()->getUserSession()->isLoggedIn();
432
	}
433
434
	/**
435
	 * @deprecated use IGroupManager->isAdmin($userId)
436
	 * @return boolean
437
	 */
438
	public function isAdminUser() {
439
		$uid = $this->getUserId();
440
		return \OC_User::isAdminUser($uid);
441
	}
442
443
	private function getUserId() {
444
		return $this->getServer()->getSession()->get('user_id');
445
	}
446
447
	/**
448
	 * @deprecated use the ILogger instead
449
	 * @param string $message
450
	 * @param string $level
451
	 * @return mixed
452
	 */
453
	public function log($message, $level) {
454
		switch ($level) {
455
			case 'debug':
456
				$level = \OCP\Util::DEBUG;
457
				break;
458
			case 'info':
459
				$level = \OCP\Util::INFO;
460
				break;
461
			case 'warn':
462
				$level = \OCP\Util::WARN;
463
				break;
464
			case 'fatal':
465
				$level = \OCP\Util::FATAL;
466
				break;
467
			default:
468
				$level = \OCP\Util::ERROR;
469
				break;
470
		}
471
		\OCP\Util::writeLog($this->getAppName(), $message, $level);
472
	}
473
474
	/**
475
	 * Register a capability
476
	 *
477
	 * @param string $serviceName e.g. 'OCA\Files\Capabilities'
478
	 */
479
	public function registerCapability($serviceName) {
480
		$this->query('OC\CapabilitiesManager')->registerCapability(function () use ($serviceName) {
481
			return $this->query($serviceName);
482
		});
483
	}
484
}
485