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