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