Completed
Push — master ( 50e4ab...3ade5c )
by Morris
27:11 queued 12:48
created
lib/public/AppFramework/IAppContainer.php 1 patch
Indentation   +24 added lines, -24 removed lines patch added patch discarded remove patch
@@ -37,31 +37,31 @@
 block discarded – undo
37 37
  */
38 38
 interface IAppContainer extends IContainer {
39 39
 
40
-	/**
41
-	 * used to return the appname of the set application
42
-	 * @return string the name of your application
43
-	 * @since 6.0.0
44
-	 */
45
-	public function getAppName();
40
+    /**
41
+     * used to return the appname of the set application
42
+     * @return string the name of your application
43
+     * @since 6.0.0
44
+     */
45
+    public function getAppName();
46 46
 
47
-	/**
48
-	 * @return \OCP\IServerContainer
49
-	 * @since 6.0.0
50
-	 */
51
-	public function getServer();
47
+    /**
48
+     * @return \OCP\IServerContainer
49
+     * @since 6.0.0
50
+     */
51
+    public function getServer();
52 52
 
53
-	/**
54
-	 * @param string $middleWare
55
-	 * @return boolean
56
-	 * @since 6.0.0
57
-	 */
58
-	public function registerMiddleWare($middleWare);
53
+    /**
54
+     * @param string $middleWare
55
+     * @return boolean
56
+     * @since 6.0.0
57
+     */
58
+    public function registerMiddleWare($middleWare);
59 59
 
60
-	/**
61
-	 * Register a capability
62
-	 *
63
-	 * @param string $serviceName e.g. 'OCA\Files\Capabilities'
64
-	 * @since 8.2.0
65
-	 */
66
-	 public function registerCapability($serviceName);
60
+    /**
61
+     * Register a capability
62
+     *
63
+     * @param string $serviceName e.g. 'OCA\Files\Capabilities'
64
+     * @since 8.2.0
65
+     */
66
+        public function registerCapability($serviceName);
67 67
 }
Please login to merge, or discard this patch.
lib/private/AppFramework/DependencyInjection/DIContainer.php 1 patch
Indentation   +404 added lines, -404 removed lines patch added patch discarded remove patch
@@ -67,408 +67,408 @@
 block discarded – undo
67 67
 
68 68
 class DIContainer extends SimpleContainer implements IAppContainer {
69 69
 
70
-	/**
71
-	 * @var array
72
-	 */
73
-	private $middleWares = array();
74
-
75
-	/** @var ServerContainer */
76
-	private $server;
77
-
78
-	/**
79
-	 * Put your class dependencies in here
80
-	 * @param string $appName the name of the app
81
-	 * @param array $urlParams
82
-	 * @param ServerContainer|null $server
83
-	 */
84
-	public function __construct($appName, $urlParams = array(), ServerContainer $server = null){
85
-		parent::__construct();
86
-		$this['AppName'] = $appName;
87
-		$this['urlParams'] = $urlParams;
88
-
89
-		/** @var \OC\ServerContainer $server */
90
-		if ($server === null) {
91
-			$server = \OC::$server;
92
-		}
93
-		$this->server = $server;
94
-		$this->server->registerAppContainer($appName, $this);
95
-
96
-		// aliases
97
-		$this->registerAlias('appName', 'AppName');
98
-		$this->registerAlias('webRoot', 'WebRoot');
99
-		$this->registerAlias('userId', 'UserId');
100
-
101
-		/**
102
-		 * Core services
103
-		 */
104
-		$this->registerService(IOutput::class, function($c){
105
-			return new Output($this->getServer()->getWebRoot());
106
-		});
107
-
108
-		$this->registerService(Folder::class, function() {
109
-			return $this->getServer()->getUserFolder();
110
-		});
111
-
112
-		$this->registerService(IAppData::class, function (SimpleContainer $c) {
113
-			return $this->getServer()->getAppDataDir($c->query('AppName'));
114
-		});
115
-
116
-		$this->registerService(IL10N::class, function($c) {
117
-			return $this->getServer()->getL10N($c->query('AppName'));
118
-		});
119
-
120
-		$this->registerAlias(\OCP\AppFramework\Utility\IControllerMethodReflector::class, \OC\AppFramework\Utility\ControllerMethodReflector::class);
121
-		$this->registerAlias('ControllerMethodReflector', \OCP\AppFramework\Utility\IControllerMethodReflector::class);
122
-
123
-		$this->registerService(IRequest::class, function() {
124
-			return $this->getServer()->query(IRequest::class);
125
-		});
126
-		$this->registerAlias('Request', IRequest::class);
127
-
128
-		$this->registerAlias(\OCP\AppFramework\Utility\ITimeFactory::class, \OC\AppFramework\Utility\TimeFactory::class);
129
-		$this->registerAlias('TimeFactory', \OCP\AppFramework\Utility\ITimeFactory::class);
130
-
131
-		$this->registerAlias(\OC\User\Session::class, \OCP\IUserSession::class);
132
-
133
-		$this->registerService(IServerContainer::class, function ($c) {
134
-			return $this->getServer();
135
-		});
136
-		$this->registerAlias('ServerContainer', IServerContainer::class);
137
-
138
-		$this->registerService(\OCP\WorkflowEngine\IManager::class, function ($c) {
139
-			return $c->query('OCA\WorkflowEngine\Manager');
140
-		});
141
-
142
-		$this->registerService(\OCP\AppFramework\IAppContainer::class, function ($c) {
143
-			return $c;
144
-		});
145
-
146
-		// commonly used attributes
147
-		$this->registerService('UserId', function ($c) {
148
-			return $c->query('OCP\\IUserSession')->getSession()->get('user_id');
149
-		});
150
-
151
-		$this->registerService('WebRoot', function ($c) {
152
-			return $c->query('ServerContainer')->getWebRoot();
153
-		});
154
-
155
-		$this->registerService('fromMailAddress', function() {
156
-			return Util::getDefaultEmailAddress('no-reply');
157
-		});
158
-
159
-		$this->registerService('OC_Defaults', function ($c) {
160
-			return $c->getServer()->getThemingDefaults();
161
-		});
162
-
163
-		$this->registerService('OCP\Encryption\IManager', function ($c) {
164
-			return $this->getServer()->getEncryptionManager();
165
-		});
166
-
167
-		$this->registerService(IConfig::class, function ($c) {
168
-			return $c->query(OC\GlobalScale\Config::class);
169
-		});
170
-
171
-		$this->registerService(IValidator::class, function($c) {
172
-			return $c->query(Validator::class);
173
-		});
174
-
175
-		$this->registerService(\OC\Security\IdentityProof\Manager::class, function ($c) {
176
-			return new \OC\Security\IdentityProof\Manager(
177
-				$this->getServer()->query(\OC\Files\AppData\Factory::class),
178
-				$this->getServer()->getCrypto(),
179
-				$this->getServer()->getConfig()
180
-			);
181
-		});
182
-
183
-		/**
184
-		 * App Framework APIs
185
-		 */
186
-		$this->registerService('API', function($c){
187
-			$c->query('OCP\\ILogger')->debug(
188
-				'Accessing the API class is deprecated! Use the appropriate ' .
189
-				'services instead!'
190
-			);
191
-			return new API($c['AppName']);
192
-		});
193
-
194
-		$this->registerService('Protocol', function($c){
195
-			/** @var \OC\Server $server */
196
-			$server = $c->query('ServerContainer');
197
-			$protocol = $server->getRequest()->getHttpProtocol();
198
-			return new Http($_SERVER, $protocol);
199
-		});
200
-
201
-		$this->registerService('Dispatcher', function($c) {
202
-			return new Dispatcher(
203
-				$c['Protocol'],
204
-				$c['MiddlewareDispatcher'],
205
-				$c['ControllerMethodReflector'],
206
-				$c['Request']
207
-			);
208
-		});
209
-
210
-		/**
211
-		 * App Framework default arguments
212
-		 */
213
-		$this->registerParameter('corsMethods', 'PUT, POST, GET, DELETE, PATCH');
214
-		$this->registerParameter('corsAllowedHeaders', 'Authorization, Content-Type, Accept');
215
-		$this->registerParameter('corsMaxAge', 1728000);
216
-
217
-		/**
218
-		 * Middleware
219
-		 */
220
-		$app = $this;
221
-		$this->registerService('SecurityMiddleware', function($c) use ($app){
222
-			/** @var \OC\Server $server */
223
-			$server = $app->getServer();
224
-
225
-			return new SecurityMiddleware(
226
-				$c['Request'],
227
-				$c['ControllerMethodReflector'],
228
-				$server->getNavigationManager(),
229
-				$server->getURLGenerator(),
230
-				$server->getLogger(),
231
-				$c['AppName'],
232
-				$server->getUserSession()->isLoggedIn(),
233
-				$server->getGroupManager()->isAdmin($this->getUserId()),
234
-				$server->getContentSecurityPolicyManager(),
235
-				$server->getCsrfTokenManager(),
236
-				$server->getContentSecurityPolicyNonceManager(),
237
-				$server->getAppManager()
238
-			);
239
-		});
240
-
241
-		$this->registerService(OC\AppFramework\Middleware\Security\PasswordConfirmationMiddleware::class, function ($c) use ($app) {
242
-			/** @var \OC\Server $server */
243
-			$server = $app->getServer();
244
-
245
-			return new OC\AppFramework\Middleware\Security\PasswordConfirmationMiddleware(
246
-				$c['ControllerMethodReflector'],
247
-				$server->getSession(),
248
-				$server->getUserSession(),
249
-				$server->query(ITimeFactory::class)
250
-			);
251
-		});
252
-
253
-		$this->registerService('BruteForceMiddleware', function($c) use ($app) {
254
-			/** @var \OC\Server $server */
255
-			$server = $app->getServer();
256
-
257
-			return new OC\AppFramework\Middleware\Security\BruteForceMiddleware(
258
-				$c['ControllerMethodReflector'],
259
-				$server->getBruteForceThrottler(),
260
-				$server->getRequest()
261
-			);
262
-		});
263
-
264
-		$this->registerService('RateLimitingMiddleware', function($c) use ($app) {
265
-			/** @var \OC\Server $server */
266
-			$server = $app->getServer();
267
-
268
-			return new RateLimitingMiddleware(
269
-				$server->getRequest(),
270
-				$server->getUserSession(),
271
-				$c['ControllerMethodReflector'],
272
-				$c->query(OC\Security\RateLimiting\Limiter::class)
273
-			);
274
-		});
275
-
276
-		$this->registerService('CORSMiddleware', function($c) {
277
-			return new CORSMiddleware(
278
-				$c['Request'],
279
-				$c['ControllerMethodReflector'],
280
-				$c->query(IUserSession::class),
281
-				$c->getServer()->getBruteForceThrottler()
282
-			);
283
-		});
284
-
285
-		$this->registerService('SessionMiddleware', function($c) use ($app) {
286
-			return new SessionMiddleware(
287
-				$c['Request'],
288
-				$c['ControllerMethodReflector'],
289
-				$app->getServer()->getSession()
290
-			);
291
-		});
292
-
293
-		$this->registerService('TwoFactorMiddleware', function (SimpleContainer $c) use ($app) {
294
-			$twoFactorManager = $c->getServer()->getTwoFactorAuthManager();
295
-			$userSession = $app->getServer()->getUserSession();
296
-			$session = $app->getServer()->getSession();
297
-			$urlGenerator = $app->getServer()->getURLGenerator();
298
-			$reflector = $c['ControllerMethodReflector'];
299
-			$request = $app->getServer()->getRequest();
300
-			return new TwoFactorMiddleware($twoFactorManager, $userSession, $session, $urlGenerator, $reflector, $request);
301
-		});
302
-
303
-		$this->registerService('OCSMiddleware', function (SimpleContainer $c) {
304
-			return new OCSMiddleware(
305
-				$c['Request']
306
-			);
307
-		});
308
-
309
-		$this->registerService(OC\AppFramework\Middleware\Security\SameSiteCookieMiddleware::class, function (SimpleContainer $c) {
310
-			return new OC\AppFramework\Middleware\Security\SameSiteCookieMiddleware(
311
-				$c['Request'],
312
-				$c['ControllerMethodReflector']
313
-			);
314
-		});
315
-
316
-		$middleWares = &$this->middleWares;
317
-		$this->registerService('MiddlewareDispatcher', function($c) use (&$middleWares) {
318
-			$dispatcher = new MiddlewareDispatcher();
319
-			$dispatcher->registerMiddleware($c[OC\AppFramework\Middleware\Security\SameSiteCookieMiddleware::class]);
320
-			$dispatcher->registerMiddleware($c['CORSMiddleware']);
321
-			$dispatcher->registerMiddleware($c['OCSMiddleware']);
322
-			$dispatcher->registerMiddleware($c['SecurityMiddleware']);
323
-			$dispatcher->registerMiddleware($c[OC\AppFramework\Middleware\Security\PasswordConfirmationMiddleware::class]);
324
-			$dispatcher->registerMiddleware($c['TwoFactorMiddleware']);
325
-			$dispatcher->registerMiddleware($c['BruteForceMiddleware']);
326
-			$dispatcher->registerMiddleware($c['RateLimitingMiddleware']);
327
-
328
-			foreach($middleWares as $middleWare) {
329
-				$dispatcher->registerMiddleware($c[$middleWare]);
330
-			}
331
-
332
-			$dispatcher->registerMiddleware($c['SessionMiddleware']);
333
-			return $dispatcher;
334
-		});
335
-
336
-	}
337
-
338
-
339
-	/**
340
-	 * @deprecated implements only deprecated methods
341
-	 * @return IApi
342
-	 */
343
-	public function getCoreApi()
344
-	{
345
-		return $this->query('API');
346
-	}
347
-
348
-	/**
349
-	 * @return \OCP\IServerContainer
350
-	 */
351
-	public function getServer()
352
-	{
353
-		return $this->server;
354
-	}
355
-
356
-	/**
357
-	 * @param string $middleWare
358
-	 * @return boolean|null
359
-	 */
360
-	public function registerMiddleWare($middleWare) {
361
-		array_push($this->middleWares, $middleWare);
362
-	}
363
-
364
-	/**
365
-	 * used to return the appname of the set application
366
-	 * @return string the name of your application
367
-	 */
368
-	public function getAppName() {
369
-		return $this->query('AppName');
370
-	}
371
-
372
-	/**
373
-	 * @deprecated use IUserSession->isLoggedIn()
374
-	 * @return boolean
375
-	 */
376
-	public function isLoggedIn() {
377
-		return \OC::$server->getUserSession()->isLoggedIn();
378
-	}
379
-
380
-	/**
381
-	 * @deprecated use IGroupManager->isAdmin($userId)
382
-	 * @return boolean
383
-	 */
384
-	public function isAdminUser() {
385
-		$uid = $this->getUserId();
386
-		return \OC_User::isAdminUser($uid);
387
-	}
388
-
389
-	private function getUserId() {
390
-		return $this->getServer()->getSession()->get('user_id');
391
-	}
392
-
393
-	/**
394
-	 * @deprecated use the ILogger instead
395
-	 * @param string $message
396
-	 * @param string $level
397
-	 * @return mixed
398
-	 */
399
-	public function log($message, $level) {
400
-		switch($level){
401
-			case 'debug':
402
-				$level = \OCP\Util::DEBUG;
403
-				break;
404
-			case 'info':
405
-				$level = \OCP\Util::INFO;
406
-				break;
407
-			case 'warn':
408
-				$level = \OCP\Util::WARN;
409
-				break;
410
-			case 'fatal':
411
-				$level = \OCP\Util::FATAL;
412
-				break;
413
-			default:
414
-				$level = \OCP\Util::ERROR;
415
-				break;
416
-		}
417
-		\OCP\Util::writeLog($this->getAppName(), $message, $level);
418
-	}
419
-
420
-	/**
421
-	 * Register a capability
422
-	 *
423
-	 * @param string $serviceName e.g. 'OCA\Files\Capabilities'
424
-	 */
425
-	public function registerCapability($serviceName) {
426
-		$this->query('OC\CapabilitiesManager')->registerCapability(function() use ($serviceName) {
427
-			return $this->query($serviceName);
428
-		});
429
-	}
430
-
431
-	/**
432
-	 * @param string $name
433
-	 * @return mixed
434
-	 * @throws QueryException if the query could not be resolved
435
-	 */
436
-	public function query($name) {
437
-		try {
438
-			return $this->queryNoFallback($name);
439
-		} catch (QueryException $firstException) {
440
-			try {
441
-				return $this->getServer()->query($name);
442
-			} catch (QueryException $secondException) {
443
-				if ($firstException->getCode() === 1) {
444
-					throw $secondException;
445
-				}
446
-				throw $firstException;
447
-			}
448
-		}
449
-	}
450
-
451
-	/**
452
-	 * @param string $name
453
-	 * @return mixed
454
-	 * @throws QueryException if the query could not be resolved
455
-	 */
456
-	public function queryNoFallback($name) {
457
-		$name = $this->sanitizeName($name);
458
-
459
-		if ($this->offsetExists($name)) {
460
-			return parent::query($name);
461
-		} else {
462
-			if ($this['AppName'] === 'settings' && strpos($name, 'OC\\Settings\\') === 0) {
463
-				return parent::query($name);
464
-			} else if ($this['AppName'] === 'core' && strpos($name, 'OC\\Core\\') === 0) {
465
-				return parent::query($name);
466
-			} else if (strpos($name, \OC\AppFramework\App::buildAppNamespace($this['AppName']) . '\\') === 0) {
467
-				return parent::query($name);
468
-			}
469
-		}
470
-
471
-		throw new QueryException('Could not resolve ' . $name . '!' .
472
-			' Class can not be instantiated', 1);
473
-	}
70
+    /**
71
+     * @var array
72
+     */
73
+    private $middleWares = array();
74
+
75
+    /** @var ServerContainer */
76
+    private $server;
77
+
78
+    /**
79
+     * Put your class dependencies in here
80
+     * @param string $appName the name of the app
81
+     * @param array $urlParams
82
+     * @param ServerContainer|null $server
83
+     */
84
+    public function __construct($appName, $urlParams = array(), ServerContainer $server = null){
85
+        parent::__construct();
86
+        $this['AppName'] = $appName;
87
+        $this['urlParams'] = $urlParams;
88
+
89
+        /** @var \OC\ServerContainer $server */
90
+        if ($server === null) {
91
+            $server = \OC::$server;
92
+        }
93
+        $this->server = $server;
94
+        $this->server->registerAppContainer($appName, $this);
95
+
96
+        // aliases
97
+        $this->registerAlias('appName', 'AppName');
98
+        $this->registerAlias('webRoot', 'WebRoot');
99
+        $this->registerAlias('userId', 'UserId');
100
+
101
+        /**
102
+         * Core services
103
+         */
104
+        $this->registerService(IOutput::class, function($c){
105
+            return new Output($this->getServer()->getWebRoot());
106
+        });
107
+
108
+        $this->registerService(Folder::class, function() {
109
+            return $this->getServer()->getUserFolder();
110
+        });
111
+
112
+        $this->registerService(IAppData::class, function (SimpleContainer $c) {
113
+            return $this->getServer()->getAppDataDir($c->query('AppName'));
114
+        });
115
+
116
+        $this->registerService(IL10N::class, function($c) {
117
+            return $this->getServer()->getL10N($c->query('AppName'));
118
+        });
119
+
120
+        $this->registerAlias(\OCP\AppFramework\Utility\IControllerMethodReflector::class, \OC\AppFramework\Utility\ControllerMethodReflector::class);
121
+        $this->registerAlias('ControllerMethodReflector', \OCP\AppFramework\Utility\IControllerMethodReflector::class);
122
+
123
+        $this->registerService(IRequest::class, function() {
124
+            return $this->getServer()->query(IRequest::class);
125
+        });
126
+        $this->registerAlias('Request', IRequest::class);
127
+
128
+        $this->registerAlias(\OCP\AppFramework\Utility\ITimeFactory::class, \OC\AppFramework\Utility\TimeFactory::class);
129
+        $this->registerAlias('TimeFactory', \OCP\AppFramework\Utility\ITimeFactory::class);
130
+
131
+        $this->registerAlias(\OC\User\Session::class, \OCP\IUserSession::class);
132
+
133
+        $this->registerService(IServerContainer::class, function ($c) {
134
+            return $this->getServer();
135
+        });
136
+        $this->registerAlias('ServerContainer', IServerContainer::class);
137
+
138
+        $this->registerService(\OCP\WorkflowEngine\IManager::class, function ($c) {
139
+            return $c->query('OCA\WorkflowEngine\Manager');
140
+        });
141
+
142
+        $this->registerService(\OCP\AppFramework\IAppContainer::class, function ($c) {
143
+            return $c;
144
+        });
145
+
146
+        // commonly used attributes
147
+        $this->registerService('UserId', function ($c) {
148
+            return $c->query('OCP\\IUserSession')->getSession()->get('user_id');
149
+        });
150
+
151
+        $this->registerService('WebRoot', function ($c) {
152
+            return $c->query('ServerContainer')->getWebRoot();
153
+        });
154
+
155
+        $this->registerService('fromMailAddress', function() {
156
+            return Util::getDefaultEmailAddress('no-reply');
157
+        });
158
+
159
+        $this->registerService('OC_Defaults', function ($c) {
160
+            return $c->getServer()->getThemingDefaults();
161
+        });
162
+
163
+        $this->registerService('OCP\Encryption\IManager', function ($c) {
164
+            return $this->getServer()->getEncryptionManager();
165
+        });
166
+
167
+        $this->registerService(IConfig::class, function ($c) {
168
+            return $c->query(OC\GlobalScale\Config::class);
169
+        });
170
+
171
+        $this->registerService(IValidator::class, function($c) {
172
+            return $c->query(Validator::class);
173
+        });
174
+
175
+        $this->registerService(\OC\Security\IdentityProof\Manager::class, function ($c) {
176
+            return new \OC\Security\IdentityProof\Manager(
177
+                $this->getServer()->query(\OC\Files\AppData\Factory::class),
178
+                $this->getServer()->getCrypto(),
179
+                $this->getServer()->getConfig()
180
+            );
181
+        });
182
+
183
+        /**
184
+         * App Framework APIs
185
+         */
186
+        $this->registerService('API', function($c){
187
+            $c->query('OCP\\ILogger')->debug(
188
+                'Accessing the API class is deprecated! Use the appropriate ' .
189
+                'services instead!'
190
+            );
191
+            return new API($c['AppName']);
192
+        });
193
+
194
+        $this->registerService('Protocol', function($c){
195
+            /** @var \OC\Server $server */
196
+            $server = $c->query('ServerContainer');
197
+            $protocol = $server->getRequest()->getHttpProtocol();
198
+            return new Http($_SERVER, $protocol);
199
+        });
200
+
201
+        $this->registerService('Dispatcher', function($c) {
202
+            return new Dispatcher(
203
+                $c['Protocol'],
204
+                $c['MiddlewareDispatcher'],
205
+                $c['ControllerMethodReflector'],
206
+                $c['Request']
207
+            );
208
+        });
209
+
210
+        /**
211
+         * App Framework default arguments
212
+         */
213
+        $this->registerParameter('corsMethods', 'PUT, POST, GET, DELETE, PATCH');
214
+        $this->registerParameter('corsAllowedHeaders', 'Authorization, Content-Type, Accept');
215
+        $this->registerParameter('corsMaxAge', 1728000);
216
+
217
+        /**
218
+         * Middleware
219
+         */
220
+        $app = $this;
221
+        $this->registerService('SecurityMiddleware', function($c) use ($app){
222
+            /** @var \OC\Server $server */
223
+            $server = $app->getServer();
224
+
225
+            return new SecurityMiddleware(
226
+                $c['Request'],
227
+                $c['ControllerMethodReflector'],
228
+                $server->getNavigationManager(),
229
+                $server->getURLGenerator(),
230
+                $server->getLogger(),
231
+                $c['AppName'],
232
+                $server->getUserSession()->isLoggedIn(),
233
+                $server->getGroupManager()->isAdmin($this->getUserId()),
234
+                $server->getContentSecurityPolicyManager(),
235
+                $server->getCsrfTokenManager(),
236
+                $server->getContentSecurityPolicyNonceManager(),
237
+                $server->getAppManager()
238
+            );
239
+        });
240
+
241
+        $this->registerService(OC\AppFramework\Middleware\Security\PasswordConfirmationMiddleware::class, function ($c) use ($app) {
242
+            /** @var \OC\Server $server */
243
+            $server = $app->getServer();
244
+
245
+            return new OC\AppFramework\Middleware\Security\PasswordConfirmationMiddleware(
246
+                $c['ControllerMethodReflector'],
247
+                $server->getSession(),
248
+                $server->getUserSession(),
249
+                $server->query(ITimeFactory::class)
250
+            );
251
+        });
252
+
253
+        $this->registerService('BruteForceMiddleware', function($c) use ($app) {
254
+            /** @var \OC\Server $server */
255
+            $server = $app->getServer();
256
+
257
+            return new OC\AppFramework\Middleware\Security\BruteForceMiddleware(
258
+                $c['ControllerMethodReflector'],
259
+                $server->getBruteForceThrottler(),
260
+                $server->getRequest()
261
+            );
262
+        });
263
+
264
+        $this->registerService('RateLimitingMiddleware', function($c) use ($app) {
265
+            /** @var \OC\Server $server */
266
+            $server = $app->getServer();
267
+
268
+            return new RateLimitingMiddleware(
269
+                $server->getRequest(),
270
+                $server->getUserSession(),
271
+                $c['ControllerMethodReflector'],
272
+                $c->query(OC\Security\RateLimiting\Limiter::class)
273
+            );
274
+        });
275
+
276
+        $this->registerService('CORSMiddleware', function($c) {
277
+            return new CORSMiddleware(
278
+                $c['Request'],
279
+                $c['ControllerMethodReflector'],
280
+                $c->query(IUserSession::class),
281
+                $c->getServer()->getBruteForceThrottler()
282
+            );
283
+        });
284
+
285
+        $this->registerService('SessionMiddleware', function($c) use ($app) {
286
+            return new SessionMiddleware(
287
+                $c['Request'],
288
+                $c['ControllerMethodReflector'],
289
+                $app->getServer()->getSession()
290
+            );
291
+        });
292
+
293
+        $this->registerService('TwoFactorMiddleware', function (SimpleContainer $c) use ($app) {
294
+            $twoFactorManager = $c->getServer()->getTwoFactorAuthManager();
295
+            $userSession = $app->getServer()->getUserSession();
296
+            $session = $app->getServer()->getSession();
297
+            $urlGenerator = $app->getServer()->getURLGenerator();
298
+            $reflector = $c['ControllerMethodReflector'];
299
+            $request = $app->getServer()->getRequest();
300
+            return new TwoFactorMiddleware($twoFactorManager, $userSession, $session, $urlGenerator, $reflector, $request);
301
+        });
302
+
303
+        $this->registerService('OCSMiddleware', function (SimpleContainer $c) {
304
+            return new OCSMiddleware(
305
+                $c['Request']
306
+            );
307
+        });
308
+
309
+        $this->registerService(OC\AppFramework\Middleware\Security\SameSiteCookieMiddleware::class, function (SimpleContainer $c) {
310
+            return new OC\AppFramework\Middleware\Security\SameSiteCookieMiddleware(
311
+                $c['Request'],
312
+                $c['ControllerMethodReflector']
313
+            );
314
+        });
315
+
316
+        $middleWares = &$this->middleWares;
317
+        $this->registerService('MiddlewareDispatcher', function($c) use (&$middleWares) {
318
+            $dispatcher = new MiddlewareDispatcher();
319
+            $dispatcher->registerMiddleware($c[OC\AppFramework\Middleware\Security\SameSiteCookieMiddleware::class]);
320
+            $dispatcher->registerMiddleware($c['CORSMiddleware']);
321
+            $dispatcher->registerMiddleware($c['OCSMiddleware']);
322
+            $dispatcher->registerMiddleware($c['SecurityMiddleware']);
323
+            $dispatcher->registerMiddleware($c[OC\AppFramework\Middleware\Security\PasswordConfirmationMiddleware::class]);
324
+            $dispatcher->registerMiddleware($c['TwoFactorMiddleware']);
325
+            $dispatcher->registerMiddleware($c['BruteForceMiddleware']);
326
+            $dispatcher->registerMiddleware($c['RateLimitingMiddleware']);
327
+
328
+            foreach($middleWares as $middleWare) {
329
+                $dispatcher->registerMiddleware($c[$middleWare]);
330
+            }
331
+
332
+            $dispatcher->registerMiddleware($c['SessionMiddleware']);
333
+            return $dispatcher;
334
+        });
335
+
336
+    }
337
+
338
+
339
+    /**
340
+     * @deprecated implements only deprecated methods
341
+     * @return IApi
342
+     */
343
+    public function getCoreApi()
344
+    {
345
+        return $this->query('API');
346
+    }
347
+
348
+    /**
349
+     * @return \OCP\IServerContainer
350
+     */
351
+    public function getServer()
352
+    {
353
+        return $this->server;
354
+    }
355
+
356
+    /**
357
+     * @param string $middleWare
358
+     * @return boolean|null
359
+     */
360
+    public function registerMiddleWare($middleWare) {
361
+        array_push($this->middleWares, $middleWare);
362
+    }
363
+
364
+    /**
365
+     * used to return the appname of the set application
366
+     * @return string the name of your application
367
+     */
368
+    public function getAppName() {
369
+        return $this->query('AppName');
370
+    }
371
+
372
+    /**
373
+     * @deprecated use IUserSession->isLoggedIn()
374
+     * @return boolean
375
+     */
376
+    public function isLoggedIn() {
377
+        return \OC::$server->getUserSession()->isLoggedIn();
378
+    }
379
+
380
+    /**
381
+     * @deprecated use IGroupManager->isAdmin($userId)
382
+     * @return boolean
383
+     */
384
+    public function isAdminUser() {
385
+        $uid = $this->getUserId();
386
+        return \OC_User::isAdminUser($uid);
387
+    }
388
+
389
+    private function getUserId() {
390
+        return $this->getServer()->getSession()->get('user_id');
391
+    }
392
+
393
+    /**
394
+     * @deprecated use the ILogger instead
395
+     * @param string $message
396
+     * @param string $level
397
+     * @return mixed
398
+     */
399
+    public function log($message, $level) {
400
+        switch($level){
401
+            case 'debug':
402
+                $level = \OCP\Util::DEBUG;
403
+                break;
404
+            case 'info':
405
+                $level = \OCP\Util::INFO;
406
+                break;
407
+            case 'warn':
408
+                $level = \OCP\Util::WARN;
409
+                break;
410
+            case 'fatal':
411
+                $level = \OCP\Util::FATAL;
412
+                break;
413
+            default:
414
+                $level = \OCP\Util::ERROR;
415
+                break;
416
+        }
417
+        \OCP\Util::writeLog($this->getAppName(), $message, $level);
418
+    }
419
+
420
+    /**
421
+     * Register a capability
422
+     *
423
+     * @param string $serviceName e.g. 'OCA\Files\Capabilities'
424
+     */
425
+    public function registerCapability($serviceName) {
426
+        $this->query('OC\CapabilitiesManager')->registerCapability(function() use ($serviceName) {
427
+            return $this->query($serviceName);
428
+        });
429
+    }
430
+
431
+    /**
432
+     * @param string $name
433
+     * @return mixed
434
+     * @throws QueryException if the query could not be resolved
435
+     */
436
+    public function query($name) {
437
+        try {
438
+            return $this->queryNoFallback($name);
439
+        } catch (QueryException $firstException) {
440
+            try {
441
+                return $this->getServer()->query($name);
442
+            } catch (QueryException $secondException) {
443
+                if ($firstException->getCode() === 1) {
444
+                    throw $secondException;
445
+                }
446
+                throw $firstException;
447
+            }
448
+        }
449
+    }
450
+
451
+    /**
452
+     * @param string $name
453
+     * @return mixed
454
+     * @throws QueryException if the query could not be resolved
455
+     */
456
+    public function queryNoFallback($name) {
457
+        $name = $this->sanitizeName($name);
458
+
459
+        if ($this->offsetExists($name)) {
460
+            return parent::query($name);
461
+        } else {
462
+            if ($this['AppName'] === 'settings' && strpos($name, 'OC\\Settings\\') === 0) {
463
+                return parent::query($name);
464
+            } else if ($this['AppName'] === 'core' && strpos($name, 'OC\\Core\\') === 0) {
465
+                return parent::query($name);
466
+            } else if (strpos($name, \OC\AppFramework\App::buildAppNamespace($this['AppName']) . '\\') === 0) {
467
+                return parent::query($name);
468
+            }
469
+        }
470
+
471
+        throw new QueryException('Could not resolve ' . $name . '!' .
472
+            ' Class can not be instantiated', 1);
473
+    }
474 474
 }
Please login to merge, or discard this patch.