@@ -68,351 +68,351 @@ |
||
68 | 68 | |
69 | 69 | class DIContainer extends SimpleContainer implements IAppContainer { |
70 | 70 | |
71 | - /** |
|
72 | - * @var array |
|
73 | - */ |
|
74 | - private $middleWares = []; |
|
75 | - |
|
76 | - /** @var ServerContainer */ |
|
77 | - private $server; |
|
78 | - |
|
79 | - /** |
|
80 | - * Put your class dependencies in here |
|
81 | - * @param string $appName the name of the app |
|
82 | - * @param array $urlParams |
|
83 | - * @param ServerContainer|null $server |
|
84 | - */ |
|
85 | - public function __construct($appName, $urlParams = array(), ServerContainer $server = null){ |
|
86 | - parent::__construct(); |
|
87 | - $this['AppName'] = $appName; |
|
88 | - $this['urlParams'] = $urlParams; |
|
89 | - |
|
90 | - $this->registerAlias('Request', IRequest::class); |
|
91 | - |
|
92 | - /** @var \OC\ServerContainer $server */ |
|
93 | - if ($server === null) { |
|
94 | - $server = \OC::$server; |
|
95 | - } |
|
96 | - $this->server = $server; |
|
97 | - $this->server->registerAppContainer($appName, $this); |
|
98 | - |
|
99 | - // aliases |
|
100 | - $this->registerAlias('appName', 'AppName'); |
|
101 | - $this->registerAlias('webRoot', 'WebRoot'); |
|
102 | - $this->registerAlias('userId', 'UserId'); |
|
103 | - |
|
104 | - /** |
|
105 | - * Core services |
|
106 | - */ |
|
107 | - $this->registerService(IOutput::class, function(){ |
|
108 | - return new Output($this->getServer()->getWebRoot()); |
|
109 | - }); |
|
110 | - |
|
111 | - $this->registerService(Folder::class, function() { |
|
112 | - return $this->getServer()->getUserFolder(); |
|
113 | - }); |
|
114 | - |
|
115 | - $this->registerService(IAppData::class, function (SimpleContainer $c) { |
|
116 | - return $this->getServer()->getAppDataDir($c->query('AppName')); |
|
117 | - }); |
|
118 | - |
|
119 | - $this->registerService(IL10N::class, function($c) { |
|
120 | - return $this->getServer()->getL10N($c->query('AppName')); |
|
121 | - }); |
|
122 | - |
|
123 | - // Log wrapper |
|
124 | - $this->registerService(ILogger::class, function ($c) { |
|
125 | - return new OC\AppFramework\Logger($this->server->query(ILogger::class), $c->query('AppName')); |
|
126 | - }); |
|
127 | - |
|
128 | - $this->registerService(IServerContainer::class, function () { |
|
129 | - return $this->getServer(); |
|
130 | - }); |
|
131 | - $this->registerAlias('ServerContainer', IServerContainer::class); |
|
132 | - |
|
133 | - $this->registerService(\OCP\WorkflowEngine\IManager::class, function ($c) { |
|
134 | - return $c->query(Manager::class); |
|
135 | - }); |
|
136 | - |
|
137 | - $this->registerService(\OCP\AppFramework\IAppContainer::class, function ($c) { |
|
138 | - return $c; |
|
139 | - }); |
|
140 | - |
|
141 | - // commonly used attributes |
|
142 | - $this->registerService('UserId', function ($c) { |
|
143 | - return $c->query(IUserSession::class)->getSession()->get('user_id'); |
|
144 | - }); |
|
145 | - |
|
146 | - $this->registerService('WebRoot', function ($c) { |
|
147 | - return $c->query('ServerContainer')->getWebRoot(); |
|
148 | - }); |
|
149 | - |
|
150 | - $this->registerService('OC_Defaults', function ($c) { |
|
151 | - return $c->getServer()->getThemingDefaults(); |
|
152 | - }); |
|
153 | - |
|
154 | - $this->registerService(IConfig::class, function ($c) { |
|
155 | - return $c->query(OC\GlobalScale\Config::class); |
|
156 | - }); |
|
157 | - |
|
158 | - $this->registerService('Protocol', function($c){ |
|
159 | - /** @var \OC\Server $server */ |
|
160 | - $server = $c->query('ServerContainer'); |
|
161 | - $protocol = $server->getRequest()->getHttpProtocol(); |
|
162 | - return new Http($_SERVER, $protocol); |
|
163 | - }); |
|
164 | - |
|
165 | - $this->registerService('Dispatcher', function($c) { |
|
166 | - return new Dispatcher( |
|
167 | - $c['Protocol'], |
|
168 | - $c['MiddlewareDispatcher'], |
|
169 | - $c->query(IControllerMethodReflector::class), |
|
170 | - $c['Request'] |
|
171 | - ); |
|
172 | - }); |
|
173 | - |
|
174 | - /** |
|
175 | - * App Framework default arguments |
|
176 | - */ |
|
177 | - $this->registerParameter('corsMethods', 'PUT, POST, GET, DELETE, PATCH'); |
|
178 | - $this->registerParameter('corsAllowedHeaders', 'Authorization, Content-Type, Accept'); |
|
179 | - $this->registerParameter('corsMaxAge', 1728000); |
|
180 | - |
|
181 | - /** |
|
182 | - * Middleware |
|
183 | - */ |
|
184 | - $this->registerService('MiddlewareDispatcher', function(SimpleContainer $c) { |
|
185 | - $server = $this->getServer(); |
|
186 | - |
|
187 | - $dispatcher = new MiddlewareDispatcher(); |
|
188 | - $dispatcher->registerMiddleware( |
|
189 | - $c->query(OC\AppFramework\Middleware\Security\ReloadExecutionMiddleware::class) |
|
190 | - ); |
|
191 | - |
|
192 | - $dispatcher->registerMiddleware( |
|
193 | - new OC\AppFramework\Middleware\Security\SameSiteCookieMiddleware( |
|
194 | - $c->query(IRequest::class), |
|
195 | - $c->query(IControllerMethodReflector::class) |
|
196 | - ) |
|
197 | - ); |
|
198 | - $dispatcher->registerMiddleware( |
|
199 | - new CORSMiddleware( |
|
200 | - $c->query(IRequest::class), |
|
201 | - $c->query(IControllerMethodReflector::class), |
|
202 | - $c->query(IUserSession::class), |
|
203 | - $c->query(OC\Security\Bruteforce\Throttler::class) |
|
204 | - ) |
|
205 | - ); |
|
206 | - $dispatcher->registerMiddleware( |
|
207 | - new OCSMiddleware( |
|
208 | - $c->query(IRequest::class) |
|
209 | - ) |
|
210 | - ); |
|
211 | - |
|
212 | - $securityMiddleware = new SecurityMiddleware( |
|
213 | - $c->query(IRequest::class), |
|
214 | - $c->query(IControllerMethodReflector::class), |
|
215 | - $c->query(INavigationManager::class), |
|
216 | - $c->query(IURLGenerator::class), |
|
217 | - $server->getLogger(), |
|
218 | - $c['AppName'], |
|
219 | - $server->getUserSession()->isLoggedIn(), |
|
220 | - $server->getGroupManager()->isAdmin($this->getUserId()), |
|
221 | - $server->getContentSecurityPolicyManager(), |
|
222 | - $server->getCsrfTokenManager(), |
|
223 | - $server->getContentSecurityPolicyNonceManager(), |
|
224 | - $server->getAppManager(), |
|
225 | - $server->getL10N('lib') |
|
226 | - ); |
|
227 | - $dispatcher->registerMiddleware($securityMiddleware); |
|
228 | - $dispatcher->registerMiddleware( |
|
229 | - new OC\AppFramework\Middleware\Security\PasswordConfirmationMiddleware( |
|
230 | - $c->query(IControllerMethodReflector::class), |
|
231 | - $c->query(ISession::class), |
|
232 | - $c->query(IUserSession::class), |
|
233 | - $c->query(ITimeFactory::class) |
|
234 | - ) |
|
235 | - ); |
|
236 | - $dispatcher->registerMiddleware( |
|
237 | - new TwoFactorMiddleware( |
|
238 | - $c->query(OC\Authentication\TwoFactorAuth\Manager::class), |
|
239 | - $c->query(IUserSession::class), |
|
240 | - $c->query(ISession::class), |
|
241 | - $c->query(IURLGenerator::class), |
|
242 | - $c->query(IControllerMethodReflector::class), |
|
243 | - $c->query(IRequest::class) |
|
244 | - ) |
|
245 | - ); |
|
246 | - $dispatcher->registerMiddleware( |
|
247 | - new OC\AppFramework\Middleware\Security\BruteForceMiddleware( |
|
248 | - $c->query(IControllerMethodReflector::class), |
|
249 | - $c->query(OC\Security\Bruteforce\Throttler::class), |
|
250 | - $c->query(IRequest::class) |
|
251 | - ) |
|
252 | - ); |
|
253 | - $dispatcher->registerMiddleware( |
|
254 | - new RateLimitingMiddleware( |
|
255 | - $c->query(IRequest::class), |
|
256 | - $c->query(IUserSession::class), |
|
257 | - $c->query(IControllerMethodReflector::class), |
|
258 | - $c->query(OC\Security\RateLimiting\Limiter::class) |
|
259 | - ) |
|
260 | - ); |
|
261 | - $dispatcher->registerMiddleware( |
|
262 | - new OC\AppFramework\Middleware\PublicShare\PublicShareMiddleware( |
|
263 | - $c->query(IRequest::class), |
|
264 | - $c->query(ISession::class), |
|
265 | - $c->query(\OCP\IConfig::class) |
|
266 | - ) |
|
267 | - ); |
|
268 | - $dispatcher->registerMiddleware( |
|
269 | - $c->query(\OC\AppFramework\Middleware\AdditionalScriptsMiddleware::class) |
|
270 | - ); |
|
271 | - |
|
272 | - foreach($this->middleWares as $middleWare) { |
|
273 | - $dispatcher->registerMiddleware($c[$middleWare]); |
|
274 | - } |
|
275 | - |
|
276 | - $dispatcher->registerMiddleware( |
|
277 | - new SessionMiddleware( |
|
278 | - $c->query(IRequest::class), |
|
279 | - $c->query(IControllerMethodReflector::class), |
|
280 | - $c->query(ISession::class) |
|
281 | - ) |
|
282 | - ); |
|
283 | - return $dispatcher; |
|
284 | - }); |
|
285 | - |
|
286 | - $this->registerAlias(\OCP\Collaboration\Resources\IManager::class, OC\Collaboration\Resources\Manager::class); |
|
287 | - } |
|
288 | - |
|
289 | - /** |
|
290 | - * @return \OCP\IServerContainer |
|
291 | - */ |
|
292 | - public function getServer() |
|
293 | - { |
|
294 | - return $this->server; |
|
295 | - } |
|
296 | - |
|
297 | - /** |
|
298 | - * @param string $middleWare |
|
299 | - * @return boolean|null |
|
300 | - */ |
|
301 | - public function registerMiddleWare($middleWare) { |
|
302 | - if (in_array($middleWare, $this->middleWares, true) !== false) { |
|
303 | - return false; |
|
304 | - } |
|
305 | - $this->middleWares[] = $middleWare; |
|
306 | - } |
|
307 | - |
|
308 | - /** |
|
309 | - * used to return the appname of the set application |
|
310 | - * @return string the name of your application |
|
311 | - */ |
|
312 | - public function getAppName() { |
|
313 | - return $this->query('AppName'); |
|
314 | - } |
|
315 | - |
|
316 | - /** |
|
317 | - * @deprecated use IUserSession->isLoggedIn() |
|
318 | - * @return boolean |
|
319 | - */ |
|
320 | - public function isLoggedIn() { |
|
321 | - return \OC::$server->getUserSession()->isLoggedIn(); |
|
322 | - } |
|
323 | - |
|
324 | - /** |
|
325 | - * @deprecated use IGroupManager->isAdmin($userId) |
|
326 | - * @return boolean |
|
327 | - */ |
|
328 | - public function isAdminUser() { |
|
329 | - $uid = $this->getUserId(); |
|
330 | - return \OC_User::isAdminUser($uid); |
|
331 | - } |
|
332 | - |
|
333 | - private function getUserId() { |
|
334 | - return $this->getServer()->getSession()->get('user_id'); |
|
335 | - } |
|
336 | - |
|
337 | - /** |
|
338 | - * @deprecated use the ILogger instead |
|
339 | - * @param string $message |
|
340 | - * @param string $level |
|
341 | - * @return mixed |
|
342 | - */ |
|
343 | - public function log($message, $level) { |
|
344 | - switch($level){ |
|
345 | - case 'debug': |
|
346 | - $level = ILogger::DEBUG; |
|
347 | - break; |
|
348 | - case 'info': |
|
349 | - $level = ILogger::INFO; |
|
350 | - break; |
|
351 | - case 'warn': |
|
352 | - $level = ILogger::WARN; |
|
353 | - break; |
|
354 | - case 'fatal': |
|
355 | - $level = ILogger::FATAL; |
|
356 | - break; |
|
357 | - default: |
|
358 | - $level = ILogger::ERROR; |
|
359 | - break; |
|
360 | - } |
|
361 | - \OCP\Util::writeLog($this->getAppName(), $message, $level); |
|
362 | - } |
|
363 | - |
|
364 | - /** |
|
365 | - * Register a capability |
|
366 | - * |
|
367 | - * @param string $serviceName e.g. 'OCA\Files\Capabilities' |
|
368 | - */ |
|
369 | - public function registerCapability($serviceName) { |
|
370 | - $this->query('OC\CapabilitiesManager')->registerCapability(function() use ($serviceName) { |
|
371 | - return $this->query($serviceName); |
|
372 | - }); |
|
373 | - } |
|
374 | - |
|
375 | - /** |
|
376 | - * @param string $name |
|
377 | - * @return mixed |
|
378 | - * @throws QueryException if the query could not be resolved |
|
379 | - */ |
|
380 | - public function query($name) { |
|
381 | - try { |
|
382 | - return $this->queryNoFallback($name); |
|
383 | - } catch (QueryException $firstException) { |
|
384 | - try { |
|
385 | - return $this->getServer()->query($name); |
|
386 | - } catch (QueryException $secondException) { |
|
387 | - if ($firstException->getCode() === 1) { |
|
388 | - throw $secondException; |
|
389 | - } |
|
390 | - throw $firstException; |
|
391 | - } |
|
392 | - } |
|
393 | - } |
|
394 | - |
|
395 | - /** |
|
396 | - * @param string $name |
|
397 | - * @return mixed |
|
398 | - * @throws QueryException if the query could not be resolved |
|
399 | - */ |
|
400 | - public function queryNoFallback($name) { |
|
401 | - $name = $this->sanitizeName($name); |
|
402 | - |
|
403 | - if ($this->offsetExists($name)) { |
|
404 | - return parent::query($name); |
|
405 | - } else { |
|
406 | - if ($this['AppName'] === 'settings' && strpos($name, 'OC\\Settings\\') === 0) { |
|
407 | - return parent::query($name); |
|
408 | - } else if ($this['AppName'] === 'core' && strpos($name, 'OC\\Core\\') === 0) { |
|
409 | - return parent::query($name); |
|
410 | - } else if (strpos($name, \OC\AppFramework\App::buildAppNamespace($this['AppName']) . '\\') === 0) { |
|
411 | - return parent::query($name); |
|
412 | - } |
|
413 | - } |
|
414 | - |
|
415 | - throw new QueryException('Could not resolve ' . $name . '!' . |
|
416 | - ' Class can not be instantiated', 1); |
|
417 | - } |
|
71 | + /** |
|
72 | + * @var array |
|
73 | + */ |
|
74 | + private $middleWares = []; |
|
75 | + |
|
76 | + /** @var ServerContainer */ |
|
77 | + private $server; |
|
78 | + |
|
79 | + /** |
|
80 | + * Put your class dependencies in here |
|
81 | + * @param string $appName the name of the app |
|
82 | + * @param array $urlParams |
|
83 | + * @param ServerContainer|null $server |
|
84 | + */ |
|
85 | + public function __construct($appName, $urlParams = array(), ServerContainer $server = null){ |
|
86 | + parent::__construct(); |
|
87 | + $this['AppName'] = $appName; |
|
88 | + $this['urlParams'] = $urlParams; |
|
89 | + |
|
90 | + $this->registerAlias('Request', IRequest::class); |
|
91 | + |
|
92 | + /** @var \OC\ServerContainer $server */ |
|
93 | + if ($server === null) { |
|
94 | + $server = \OC::$server; |
|
95 | + } |
|
96 | + $this->server = $server; |
|
97 | + $this->server->registerAppContainer($appName, $this); |
|
98 | + |
|
99 | + // aliases |
|
100 | + $this->registerAlias('appName', 'AppName'); |
|
101 | + $this->registerAlias('webRoot', 'WebRoot'); |
|
102 | + $this->registerAlias('userId', 'UserId'); |
|
103 | + |
|
104 | + /** |
|
105 | + * Core services |
|
106 | + */ |
|
107 | + $this->registerService(IOutput::class, function(){ |
|
108 | + return new Output($this->getServer()->getWebRoot()); |
|
109 | + }); |
|
110 | + |
|
111 | + $this->registerService(Folder::class, function() { |
|
112 | + return $this->getServer()->getUserFolder(); |
|
113 | + }); |
|
114 | + |
|
115 | + $this->registerService(IAppData::class, function (SimpleContainer $c) { |
|
116 | + return $this->getServer()->getAppDataDir($c->query('AppName')); |
|
117 | + }); |
|
118 | + |
|
119 | + $this->registerService(IL10N::class, function($c) { |
|
120 | + return $this->getServer()->getL10N($c->query('AppName')); |
|
121 | + }); |
|
122 | + |
|
123 | + // Log wrapper |
|
124 | + $this->registerService(ILogger::class, function ($c) { |
|
125 | + return new OC\AppFramework\Logger($this->server->query(ILogger::class), $c->query('AppName')); |
|
126 | + }); |
|
127 | + |
|
128 | + $this->registerService(IServerContainer::class, function () { |
|
129 | + return $this->getServer(); |
|
130 | + }); |
|
131 | + $this->registerAlias('ServerContainer', IServerContainer::class); |
|
132 | + |
|
133 | + $this->registerService(\OCP\WorkflowEngine\IManager::class, function ($c) { |
|
134 | + return $c->query(Manager::class); |
|
135 | + }); |
|
136 | + |
|
137 | + $this->registerService(\OCP\AppFramework\IAppContainer::class, function ($c) { |
|
138 | + return $c; |
|
139 | + }); |
|
140 | + |
|
141 | + // commonly used attributes |
|
142 | + $this->registerService('UserId', function ($c) { |
|
143 | + return $c->query(IUserSession::class)->getSession()->get('user_id'); |
|
144 | + }); |
|
145 | + |
|
146 | + $this->registerService('WebRoot', function ($c) { |
|
147 | + return $c->query('ServerContainer')->getWebRoot(); |
|
148 | + }); |
|
149 | + |
|
150 | + $this->registerService('OC_Defaults', function ($c) { |
|
151 | + return $c->getServer()->getThemingDefaults(); |
|
152 | + }); |
|
153 | + |
|
154 | + $this->registerService(IConfig::class, function ($c) { |
|
155 | + return $c->query(OC\GlobalScale\Config::class); |
|
156 | + }); |
|
157 | + |
|
158 | + $this->registerService('Protocol', function($c){ |
|
159 | + /** @var \OC\Server $server */ |
|
160 | + $server = $c->query('ServerContainer'); |
|
161 | + $protocol = $server->getRequest()->getHttpProtocol(); |
|
162 | + return new Http($_SERVER, $protocol); |
|
163 | + }); |
|
164 | + |
|
165 | + $this->registerService('Dispatcher', function($c) { |
|
166 | + return new Dispatcher( |
|
167 | + $c['Protocol'], |
|
168 | + $c['MiddlewareDispatcher'], |
|
169 | + $c->query(IControllerMethodReflector::class), |
|
170 | + $c['Request'] |
|
171 | + ); |
|
172 | + }); |
|
173 | + |
|
174 | + /** |
|
175 | + * App Framework default arguments |
|
176 | + */ |
|
177 | + $this->registerParameter('corsMethods', 'PUT, POST, GET, DELETE, PATCH'); |
|
178 | + $this->registerParameter('corsAllowedHeaders', 'Authorization, Content-Type, Accept'); |
|
179 | + $this->registerParameter('corsMaxAge', 1728000); |
|
180 | + |
|
181 | + /** |
|
182 | + * Middleware |
|
183 | + */ |
|
184 | + $this->registerService('MiddlewareDispatcher', function(SimpleContainer $c) { |
|
185 | + $server = $this->getServer(); |
|
186 | + |
|
187 | + $dispatcher = new MiddlewareDispatcher(); |
|
188 | + $dispatcher->registerMiddleware( |
|
189 | + $c->query(OC\AppFramework\Middleware\Security\ReloadExecutionMiddleware::class) |
|
190 | + ); |
|
191 | + |
|
192 | + $dispatcher->registerMiddleware( |
|
193 | + new OC\AppFramework\Middleware\Security\SameSiteCookieMiddleware( |
|
194 | + $c->query(IRequest::class), |
|
195 | + $c->query(IControllerMethodReflector::class) |
|
196 | + ) |
|
197 | + ); |
|
198 | + $dispatcher->registerMiddleware( |
|
199 | + new CORSMiddleware( |
|
200 | + $c->query(IRequest::class), |
|
201 | + $c->query(IControllerMethodReflector::class), |
|
202 | + $c->query(IUserSession::class), |
|
203 | + $c->query(OC\Security\Bruteforce\Throttler::class) |
|
204 | + ) |
|
205 | + ); |
|
206 | + $dispatcher->registerMiddleware( |
|
207 | + new OCSMiddleware( |
|
208 | + $c->query(IRequest::class) |
|
209 | + ) |
|
210 | + ); |
|
211 | + |
|
212 | + $securityMiddleware = new SecurityMiddleware( |
|
213 | + $c->query(IRequest::class), |
|
214 | + $c->query(IControllerMethodReflector::class), |
|
215 | + $c->query(INavigationManager::class), |
|
216 | + $c->query(IURLGenerator::class), |
|
217 | + $server->getLogger(), |
|
218 | + $c['AppName'], |
|
219 | + $server->getUserSession()->isLoggedIn(), |
|
220 | + $server->getGroupManager()->isAdmin($this->getUserId()), |
|
221 | + $server->getContentSecurityPolicyManager(), |
|
222 | + $server->getCsrfTokenManager(), |
|
223 | + $server->getContentSecurityPolicyNonceManager(), |
|
224 | + $server->getAppManager(), |
|
225 | + $server->getL10N('lib') |
|
226 | + ); |
|
227 | + $dispatcher->registerMiddleware($securityMiddleware); |
|
228 | + $dispatcher->registerMiddleware( |
|
229 | + new OC\AppFramework\Middleware\Security\PasswordConfirmationMiddleware( |
|
230 | + $c->query(IControllerMethodReflector::class), |
|
231 | + $c->query(ISession::class), |
|
232 | + $c->query(IUserSession::class), |
|
233 | + $c->query(ITimeFactory::class) |
|
234 | + ) |
|
235 | + ); |
|
236 | + $dispatcher->registerMiddleware( |
|
237 | + new TwoFactorMiddleware( |
|
238 | + $c->query(OC\Authentication\TwoFactorAuth\Manager::class), |
|
239 | + $c->query(IUserSession::class), |
|
240 | + $c->query(ISession::class), |
|
241 | + $c->query(IURLGenerator::class), |
|
242 | + $c->query(IControllerMethodReflector::class), |
|
243 | + $c->query(IRequest::class) |
|
244 | + ) |
|
245 | + ); |
|
246 | + $dispatcher->registerMiddleware( |
|
247 | + new OC\AppFramework\Middleware\Security\BruteForceMiddleware( |
|
248 | + $c->query(IControllerMethodReflector::class), |
|
249 | + $c->query(OC\Security\Bruteforce\Throttler::class), |
|
250 | + $c->query(IRequest::class) |
|
251 | + ) |
|
252 | + ); |
|
253 | + $dispatcher->registerMiddleware( |
|
254 | + new RateLimitingMiddleware( |
|
255 | + $c->query(IRequest::class), |
|
256 | + $c->query(IUserSession::class), |
|
257 | + $c->query(IControllerMethodReflector::class), |
|
258 | + $c->query(OC\Security\RateLimiting\Limiter::class) |
|
259 | + ) |
|
260 | + ); |
|
261 | + $dispatcher->registerMiddleware( |
|
262 | + new OC\AppFramework\Middleware\PublicShare\PublicShareMiddleware( |
|
263 | + $c->query(IRequest::class), |
|
264 | + $c->query(ISession::class), |
|
265 | + $c->query(\OCP\IConfig::class) |
|
266 | + ) |
|
267 | + ); |
|
268 | + $dispatcher->registerMiddleware( |
|
269 | + $c->query(\OC\AppFramework\Middleware\AdditionalScriptsMiddleware::class) |
|
270 | + ); |
|
271 | + |
|
272 | + foreach($this->middleWares as $middleWare) { |
|
273 | + $dispatcher->registerMiddleware($c[$middleWare]); |
|
274 | + } |
|
275 | + |
|
276 | + $dispatcher->registerMiddleware( |
|
277 | + new SessionMiddleware( |
|
278 | + $c->query(IRequest::class), |
|
279 | + $c->query(IControllerMethodReflector::class), |
|
280 | + $c->query(ISession::class) |
|
281 | + ) |
|
282 | + ); |
|
283 | + return $dispatcher; |
|
284 | + }); |
|
285 | + |
|
286 | + $this->registerAlias(\OCP\Collaboration\Resources\IManager::class, OC\Collaboration\Resources\Manager::class); |
|
287 | + } |
|
288 | + |
|
289 | + /** |
|
290 | + * @return \OCP\IServerContainer |
|
291 | + */ |
|
292 | + public function getServer() |
|
293 | + { |
|
294 | + return $this->server; |
|
295 | + } |
|
296 | + |
|
297 | + /** |
|
298 | + * @param string $middleWare |
|
299 | + * @return boolean|null |
|
300 | + */ |
|
301 | + public function registerMiddleWare($middleWare) { |
|
302 | + if (in_array($middleWare, $this->middleWares, true) !== false) { |
|
303 | + return false; |
|
304 | + } |
|
305 | + $this->middleWares[] = $middleWare; |
|
306 | + } |
|
307 | + |
|
308 | + /** |
|
309 | + * used to return the appname of the set application |
|
310 | + * @return string the name of your application |
|
311 | + */ |
|
312 | + public function getAppName() { |
|
313 | + return $this->query('AppName'); |
|
314 | + } |
|
315 | + |
|
316 | + /** |
|
317 | + * @deprecated use IUserSession->isLoggedIn() |
|
318 | + * @return boolean |
|
319 | + */ |
|
320 | + public function isLoggedIn() { |
|
321 | + return \OC::$server->getUserSession()->isLoggedIn(); |
|
322 | + } |
|
323 | + |
|
324 | + /** |
|
325 | + * @deprecated use IGroupManager->isAdmin($userId) |
|
326 | + * @return boolean |
|
327 | + */ |
|
328 | + public function isAdminUser() { |
|
329 | + $uid = $this->getUserId(); |
|
330 | + return \OC_User::isAdminUser($uid); |
|
331 | + } |
|
332 | + |
|
333 | + private function getUserId() { |
|
334 | + return $this->getServer()->getSession()->get('user_id'); |
|
335 | + } |
|
336 | + |
|
337 | + /** |
|
338 | + * @deprecated use the ILogger instead |
|
339 | + * @param string $message |
|
340 | + * @param string $level |
|
341 | + * @return mixed |
|
342 | + */ |
|
343 | + public function log($message, $level) { |
|
344 | + switch($level){ |
|
345 | + case 'debug': |
|
346 | + $level = ILogger::DEBUG; |
|
347 | + break; |
|
348 | + case 'info': |
|
349 | + $level = ILogger::INFO; |
|
350 | + break; |
|
351 | + case 'warn': |
|
352 | + $level = ILogger::WARN; |
|
353 | + break; |
|
354 | + case 'fatal': |
|
355 | + $level = ILogger::FATAL; |
|
356 | + break; |
|
357 | + default: |
|
358 | + $level = ILogger::ERROR; |
|
359 | + break; |
|
360 | + } |
|
361 | + \OCP\Util::writeLog($this->getAppName(), $message, $level); |
|
362 | + } |
|
363 | + |
|
364 | + /** |
|
365 | + * Register a capability |
|
366 | + * |
|
367 | + * @param string $serviceName e.g. 'OCA\Files\Capabilities' |
|
368 | + */ |
|
369 | + public function registerCapability($serviceName) { |
|
370 | + $this->query('OC\CapabilitiesManager')->registerCapability(function() use ($serviceName) { |
|
371 | + return $this->query($serviceName); |
|
372 | + }); |
|
373 | + } |
|
374 | + |
|
375 | + /** |
|
376 | + * @param string $name |
|
377 | + * @return mixed |
|
378 | + * @throws QueryException if the query could not be resolved |
|
379 | + */ |
|
380 | + public function query($name) { |
|
381 | + try { |
|
382 | + return $this->queryNoFallback($name); |
|
383 | + } catch (QueryException $firstException) { |
|
384 | + try { |
|
385 | + return $this->getServer()->query($name); |
|
386 | + } catch (QueryException $secondException) { |
|
387 | + if ($firstException->getCode() === 1) { |
|
388 | + throw $secondException; |
|
389 | + } |
|
390 | + throw $firstException; |
|
391 | + } |
|
392 | + } |
|
393 | + } |
|
394 | + |
|
395 | + /** |
|
396 | + * @param string $name |
|
397 | + * @return mixed |
|
398 | + * @throws QueryException if the query could not be resolved |
|
399 | + */ |
|
400 | + public function queryNoFallback($name) { |
|
401 | + $name = $this->sanitizeName($name); |
|
402 | + |
|
403 | + if ($this->offsetExists($name)) { |
|
404 | + return parent::query($name); |
|
405 | + } else { |
|
406 | + if ($this['AppName'] === 'settings' && strpos($name, 'OC\\Settings\\') === 0) { |
|
407 | + return parent::query($name); |
|
408 | + } else if ($this['AppName'] === 'core' && strpos($name, 'OC\\Core\\') === 0) { |
|
409 | + return parent::query($name); |
|
410 | + } else if (strpos($name, \OC\AppFramework\App::buildAppNamespace($this['AppName']) . '\\') === 0) { |
|
411 | + return parent::query($name); |
|
412 | + } |
|
413 | + } |
|
414 | + |
|
415 | + throw new QueryException('Could not resolve ' . $name . '!' . |
|
416 | + ' Class can not be instantiated', 1); |
|
417 | + } |
|
418 | 418 | } |