2013-08-17 13:16:48 +04:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-21 18:07:57 +03:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Arthur Schiwon <blizzz@arthur-schiwon.de>
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Bernhard Posselt <dev@bernhard-posselt.com>
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Bjoern Schiessle <bjoern@schiessle.org>
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
|
2016-07-21 18:07:57 +03:00
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Jörn Friedrich Dreyer <jfd@butonic.de>
|
2016-05-26 20:56:05 +03:00
|
|
|
* @author Lukas Reschke <lukas@statuscode.ch>
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2016-01-12 17:02:16 +03:00
|
|
|
* @author Robin McCorkell <robin@mccorkell.me.uk>
|
2016-07-21 18:07:57 +03:00
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Sebastian Wessalowski <sebastian@wessalowski.org>
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Thomas Müller <thomas.mueller@tmit.eu>
|
|
|
|
* @author Thomas Tanghus <thomas@tanghus.net>
|
2013-08-17 13:16:48 +04:00
|
|
|
*
|
2015-03-26 13:44:34 +03:00
|
|
|
* @license AGPL-3.0
|
2013-08-17 13:16:48 +04:00
|
|
|
*
|
2015-03-26 13:44:34 +03:00
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
2013-08-17 13:16:48 +04:00
|
|
|
*
|
2015-03-26 13:44:34 +03:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
2013-08-17 13:16:48 +04:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2015-03-26 13:44:34 +03:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
2013-08-17 13:16:48 +04:00
|
|
|
*
|
2015-03-26 13:44:34 +03:00
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3,
|
2019-12-03 21:57:53 +03:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
2013-08-17 13:16:48 +04:00
|
|
|
*
|
|
|
|
*/
|
2015-02-26 13:37:37 +03:00
|
|
|
|
2013-08-17 13:16:48 +04:00
|
|
|
namespace OC\AppFramework\DependencyInjection;
|
|
|
|
|
2015-01-22 17:33:09 +03:00
|
|
|
use OC;
|
2013-10-23 07:57:34 +04:00
|
|
|
use OC\AppFramework\Http;
|
2013-08-17 13:16:48 +04:00
|
|
|
use OC\AppFramework\Http\Dispatcher;
|
2015-01-22 22:26:46 +03:00
|
|
|
use OC\AppFramework\Http\Output;
|
2013-08-17 13:16:48 +04:00
|
|
|
use OC\AppFramework\Middleware\MiddlewareDispatcher;
|
2016-07-20 10:43:02 +03:00
|
|
|
use OC\AppFramework\Middleware\OCSMiddleware;
|
2019-11-22 22:52:10 +03:00
|
|
|
use OC\AppFramework\Middleware\Security\CORSMiddleware;
|
2017-04-12 23:14:11 +03:00
|
|
|
use OC\AppFramework\Middleware\Security\RateLimitingMiddleware;
|
2016-05-11 12:23:25 +03:00
|
|
|
use OC\AppFramework\Middleware\Security\SecurityMiddleware;
|
2014-10-21 16:53:10 +04:00
|
|
|
use OC\AppFramework\Middleware\SessionMiddleware;
|
2020-07-16 20:11:47 +03:00
|
|
|
use OC\AppFramework\ScopedPsrLogger;
|
2013-08-20 19:21:14 +04:00
|
|
|
use OC\AppFramework\Utility\SimpleContainer;
|
2016-05-11 12:23:25 +03:00
|
|
|
use OC\Core\Middleware\TwoFactorMiddleware;
|
2020-07-16 20:11:47 +03:00
|
|
|
use OC\Log\PsrLoggerAdapter;
|
2017-03-20 01:14:47 +03:00
|
|
|
use OC\ServerContainer;
|
2019-11-22 22:52:10 +03:00
|
|
|
use OCA\WorkflowEngine\Manager;
|
2017-03-21 12:00:02 +03:00
|
|
|
use OCP\AppFramework\Http\IOutput;
|
2013-08-21 03:02:15 +04:00
|
|
|
use OCP\AppFramework\IAppContainer;
|
2017-03-22 12:33:30 +03:00
|
|
|
use OCP\AppFramework\QueryException;
|
2020-05-02 16:34:28 +03:00
|
|
|
use OCP\AppFramework\Services\IAppConfig;
|
2020-05-07 11:13:06 +03:00
|
|
|
use OCP\AppFramework\Services\IInitialState;
|
2018-11-01 13:29:29 +03:00
|
|
|
use OCP\AppFramework\Utility\IControllerMethodReflector;
|
2018-01-02 23:13:32 +03:00
|
|
|
use OCP\AppFramework\Utility\ITimeFactory;
|
2017-03-21 12:00:02 +03:00
|
|
|
use OCP\Files\Folder;
|
2016-09-08 16:20:39 +03:00
|
|
|
use OCP\Files\IAppData;
|
2019-05-22 11:48:51 +03:00
|
|
|
use OCP\Group\ISubAdmin;
|
2020-07-14 10:18:39 +03:00
|
|
|
use OCP\IConfig;
|
2020-09-25 15:47:14 +03:00
|
|
|
use OCP\IDBConnection;
|
2020-07-14 10:18:39 +03:00
|
|
|
use OCP\IInitialStateService;
|
2017-03-21 12:00:02 +03:00
|
|
|
use OCP\IL10N;
|
2018-04-25 16:22:28 +03:00
|
|
|
use OCP\ILogger;
|
2018-11-23 13:11:10 +03:00
|
|
|
use OCP\INavigationManager;
|
2018-11-01 13:29:29 +03:00
|
|
|
use OCP\IRequest;
|
2017-03-20 00:20:56 +03:00
|
|
|
use OCP\IServerContainer;
|
2018-05-18 13:28:52 +03:00
|
|
|
use OCP\ISession;
|
2018-11-23 13:11:10 +03:00
|
|
|
use OCP\IURLGenerator;
|
2017-03-21 12:00:02 +03:00
|
|
|
use OCP\IUserSession;
|
2020-07-13 12:18:14 +03:00
|
|
|
use Psr\Container\ContainerInterface;
|
2020-07-16 20:11:47 +03:00
|
|
|
use Psr\Log\LoggerInterface;
|
2013-08-17 13:16:48 +04:00
|
|
|
|
2020-07-13 12:18:14 +03:00
|
|
|
/**
|
|
|
|
* @deprecated 20.0.0
|
|
|
|
*/
|
2014-12-13 21:28:20 +03:00
|
|
|
class DIContainer extends SimpleContainer implements IAppContainer {
|
2013-08-17 13:16:48 +04:00
|
|
|
|
2013-09-27 19:15:26 +04:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2018-11-23 13:11:10 +03:00
|
|
|
private $middleWares = [];
|
2013-08-17 13:16:48 +04:00
|
|
|
|
2017-03-20 01:14:47 +03:00
|
|
|
/** @var ServerContainer */
|
2017-03-20 00:20:56 +03:00
|
|
|
private $server;
|
|
|
|
|
2013-08-17 13:16:48 +04:00
|
|
|
/**
|
|
|
|
* Put your class dependencies in here
|
|
|
|
* @param string $appName the name of the app
|
2017-03-20 00:20:56 +03:00
|
|
|
* @param array $urlParams
|
2017-07-24 12:36:20 +03:00
|
|
|
* @param ServerContainer|null $server
|
2013-08-17 13:16:48 +04:00
|
|
|
*/
|
2020-04-09 14:53:40 +03:00
|
|
|
public function __construct($appName, $urlParams = [], ServerContainer $server = null) {
|
2015-07-24 14:43:50 +03:00
|
|
|
parent::__construct();
|
2013-08-17 13:16:48 +04:00
|
|
|
$this['AppName'] = $appName;
|
2013-10-01 21:01:52 +04:00
|
|
|
$this['urlParams'] = $urlParams;
|
2013-08-17 13:16:48 +04:00
|
|
|
|
2018-11-01 13:29:29 +03:00
|
|
|
$this->registerAlias('Request', IRequest::class);
|
|
|
|
|
2015-12-18 13:43:39 +03:00
|
|
|
/** @var \OC\ServerContainer $server */
|
2017-03-20 00:20:56 +03:00
|
|
|
if ($server === null) {
|
2017-03-20 01:14:47 +03:00
|
|
|
$server = \OC::$server;
|
2017-03-20 00:20:56 +03:00
|
|
|
}
|
2017-03-20 01:14:47 +03:00
|
|
|
$this->server = $server;
|
2017-03-20 00:20:56 +03:00
|
|
|
$this->server->registerAppContainer($appName, $this);
|
2015-12-18 13:43:39 +03:00
|
|
|
|
2015-04-29 23:29:45 +03:00
|
|
|
// aliases
|
2015-07-17 18:18:37 +03:00
|
|
|
$this->registerAlias('appName', 'AppName');
|
|
|
|
$this->registerAlias('webRoot', 'WebRoot');
|
|
|
|
$this->registerAlias('userId', 'UserId');
|
2015-04-29 23:29:45 +03:00
|
|
|
|
2014-12-13 21:28:20 +03:00
|
|
|
/**
|
|
|
|
* Core services
|
|
|
|
*/
|
2020-04-09 14:53:40 +03:00
|
|
|
$this->registerService(IOutput::class, function () {
|
2015-10-06 16:24:19 +03:00
|
|
|
return new Output($this->getServer()->getWebRoot());
|
2015-01-22 22:26:46 +03:00
|
|
|
});
|
|
|
|
|
2020-04-09 14:53:40 +03:00
|
|
|
$this->registerService(Folder::class, function () {
|
2016-05-09 11:02:07 +03:00
|
|
|
return $this->getServer()->getUserFolder();
|
|
|
|
});
|
|
|
|
|
2020-07-13 12:18:14 +03:00
|
|
|
$this->registerService(IAppData::class, function (ContainerInterface $c) {
|
|
|
|
return $this->getServer()->getAppDataDir($c->get('AppName'));
|
2016-09-08 16:20:39 +03:00
|
|
|
});
|
|
|
|
|
2020-07-13 12:18:14 +03:00
|
|
|
$this->registerService(IL10N::class, function (ContainerInterface $c) {
|
|
|
|
return $this->getServer()->getL10N($c->get('AppName'));
|
2014-12-13 21:28:20 +03:00
|
|
|
});
|
|
|
|
|
2020-07-16 20:11:47 +03:00
|
|
|
// Log wrappers
|
|
|
|
$this->registerService(LoggerInterface::class, function (ContainerInterface $c) {
|
|
|
|
return new ScopedPsrLogger(
|
|
|
|
$c->get(PsrLoggerAdapter::class),
|
|
|
|
$c->get('AppName')
|
|
|
|
);
|
|
|
|
});
|
2020-07-13 12:18:14 +03:00
|
|
|
$this->registerService(ILogger::class, function (ContainerInterface $c) {
|
|
|
|
return new OC\AppFramework\Logger($this->server->query(ILogger::class), $c->get('AppName'));
|
2018-09-04 11:05:25 +03:00
|
|
|
});
|
|
|
|
|
2018-11-23 13:11:10 +03:00
|
|
|
$this->registerService(IServerContainer::class, function () {
|
2015-01-22 17:33:09 +03:00
|
|
|
return $this->getServer();
|
2014-12-13 21:28:20 +03:00
|
|
|
});
|
2017-03-21 12:00:02 +03:00
|
|
|
$this->registerAlias('ServerContainer', IServerContainer::class);
|
2014-12-13 21:28:20 +03:00
|
|
|
|
2020-07-13 12:18:14 +03:00
|
|
|
$this->registerService(\OCP\WorkflowEngine\IManager::class, function (ContainerInterface $c) {
|
|
|
|
return $c->get(Manager::class);
|
2016-07-27 12:46:09 +03:00
|
|
|
});
|
|
|
|
|
2020-07-13 12:18:14 +03:00
|
|
|
$this->registerService(ContainerInterface::class, function (ContainerInterface $c) {
|
2015-10-20 11:32:53 +03:00
|
|
|
return $c;
|
|
|
|
});
|
2020-07-13 12:18:14 +03:00
|
|
|
$this->registerAlias(IAppContainer::class, ContainerInterface::class);
|
2015-10-20 11:32:53 +03:00
|
|
|
|
2014-12-13 21:28:20 +03:00
|
|
|
// commonly used attributes
|
2020-07-13 12:18:14 +03:00
|
|
|
$this->registerService('UserId', function (ContainerInterface $c) {
|
|
|
|
return $c->get(IUserSession::class)->getSession()->get('user_id');
|
2014-12-13 21:28:20 +03:00
|
|
|
});
|
|
|
|
|
2020-07-13 12:18:14 +03:00
|
|
|
$this->registerService('WebRoot', function (ContainerInterface $c) {
|
|
|
|
return $c->get(IServerContainer::class)->getWebRoot();
|
2014-12-13 21:28:20 +03:00
|
|
|
});
|
|
|
|
|
2020-07-13 12:18:14 +03:00
|
|
|
$this->registerService('OC_Defaults', function (ContainerInterface $c) {
|
|
|
|
return $c->get(IServerContainer::class)->getThemingDefaults();
|
2017-02-24 22:09:09 +03:00
|
|
|
});
|
|
|
|
|
2020-07-13 12:18:14 +03:00
|
|
|
$this->registerService('Protocol', function (ContainerInterface $c) {
|
2015-10-30 20:05:30 +03:00
|
|
|
/** @var \OC\Server $server */
|
2020-07-13 12:18:14 +03:00
|
|
|
$server = $c->get(IServerContainer::class);
|
2015-10-30 20:05:30 +03:00
|
|
|
$protocol = $server->getRequest()->getHttpProtocol();
|
|
|
|
return new Http($_SERVER, $protocol);
|
2013-08-17 13:16:48 +04:00
|
|
|
});
|
|
|
|
|
2020-07-13 12:18:14 +03:00
|
|
|
$this->registerService('Dispatcher', function (ContainerInterface $c) {
|
2014-05-06 18:29:19 +04:00
|
|
|
return new Dispatcher(
|
2020-07-13 12:18:14 +03:00
|
|
|
$c->get('Protocol'),
|
|
|
|
$c->get(MiddlewareDispatcher::class),
|
|
|
|
$c->get(IControllerMethodReflector::class),
|
2020-09-25 15:47:14 +03:00
|
|
|
$c->get(IRequest::class),
|
|
|
|
$c->get(IConfig::class),
|
|
|
|
$c->get(IDBConnection::class),
|
|
|
|
$c->get(LoggerInterface::class)
|
2014-05-06 18:29:19 +04:00
|
|
|
);
|
2013-08-17 13:16:48 +04:00
|
|
|
});
|
|
|
|
|
2016-07-22 11:11:53 +03:00
|
|
|
/**
|
|
|
|
* App Framework default arguments
|
|
|
|
*/
|
|
|
|
$this->registerParameter('corsMethods', 'PUT, POST, GET, DELETE, PATCH');
|
|
|
|
$this->registerParameter('corsAllowedHeaders', 'Authorization, Content-Type, Accept');
|
|
|
|
$this->registerParameter('corsMaxAge', 1728000);
|
2013-08-17 13:16:48 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Middleware
|
|
|
|
*/
|
2020-07-13 12:18:14 +03:00
|
|
|
$this->registerAlias('MiddlewareDispatcher', MiddlewareDispatcher::class);
|
|
|
|
$this->registerService(MiddlewareDispatcher::class, function (ContainerInterface $c) {
|
|
|
|
$server = $this->getServer();
|
2018-11-23 13:11:10 +03:00
|
|
|
|
|
|
|
$dispatcher = new MiddlewareDispatcher();
|
2020-04-02 15:07:40 +03:00
|
|
|
|
|
|
|
$dispatcher->registerMiddleware(
|
2020-07-13 12:18:14 +03:00
|
|
|
$c->get(OC\AppFramework\Middleware\CompressionMiddleware::class)
|
2020-04-02 15:07:40 +03:00
|
|
|
);
|
|
|
|
|
2020-07-13 12:18:14 +03:00
|
|
|
$dispatcher->registerMiddleware($c->get(OC\AppFramework\Middleware\NotModifiedMiddleware::class));
|
2020-05-11 17:18:01 +03:00
|
|
|
|
2018-11-22 01:21:28 +03:00
|
|
|
$dispatcher->registerMiddleware(
|
2020-07-13 12:18:14 +03:00
|
|
|
$c->get(OC\AppFramework\Middleware\Security\ReloadExecutionMiddleware::class)
|
2018-11-22 01:21:28 +03:00
|
|
|
);
|
|
|
|
|
2018-11-23 13:11:10 +03:00
|
|
|
$dispatcher->registerMiddleware(
|
|
|
|
new OC\AppFramework\Middleware\Security\SameSiteCookieMiddleware(
|
2020-07-13 12:18:14 +03:00
|
|
|
$c->get(IRequest::class),
|
|
|
|
$c->get(IControllerMethodReflector::class)
|
2018-11-23 13:11:10 +03:00
|
|
|
)
|
|
|
|
);
|
|
|
|
$dispatcher->registerMiddleware(
|
|
|
|
new CORSMiddleware(
|
2020-07-13 12:18:14 +03:00
|
|
|
$c->get(IRequest::class),
|
|
|
|
$c->get(IControllerMethodReflector::class),
|
|
|
|
$c->get(IUserSession::class),
|
|
|
|
$c->get(OC\Security\Bruteforce\Throttler::class)
|
2018-11-23 13:11:10 +03:00
|
|
|
)
|
|
|
|
);
|
|
|
|
$dispatcher->registerMiddleware(
|
|
|
|
new OCSMiddleware(
|
2020-07-13 12:18:14 +03:00
|
|
|
$c->get(IRequest::class)
|
2018-11-23 13:11:10 +03:00
|
|
|
)
|
|
|
|
);
|
2017-01-17 13:51:10 +03:00
|
|
|
|
2021-03-02 21:34:20 +03:00
|
|
|
|
|
|
|
|
2018-11-23 13:11:10 +03:00
|
|
|
$securityMiddleware = new SecurityMiddleware(
|
2020-07-13 12:18:14 +03:00
|
|
|
$c->get(IRequest::class),
|
|
|
|
$c->get(IControllerMethodReflector::class),
|
|
|
|
$c->get(INavigationManager::class),
|
|
|
|
$c->get(IURLGenerator::class),
|
|
|
|
$server->query(ILogger::class),
|
|
|
|
$c->get('AppName'),
|
2018-01-13 16:13:59 +03:00
|
|
|
$server->getUserSession()->isLoggedIn(),
|
2021-03-02 21:34:20 +03:00
|
|
|
$this->getUserId() !== null && $server->getGroupManager()->isAdmin($this->getUserId()),
|
2019-05-22 11:48:51 +03:00
|
|
|
$server->getUserSession()->getUser() !== null && $server->query(ISubAdmin::class)->isSubAdmin($server->getUserSession()->getUser()),
|
2018-02-26 17:32:17 +03:00
|
|
|
$server->getAppManager(),
|
|
|
|
$server->getL10N('lib')
|
2014-05-06 18:29:19 +04:00
|
|
|
);
|
2018-11-23 13:11:10 +03:00
|
|
|
$dispatcher->registerMiddleware($securityMiddleware);
|
2019-07-27 17:04:51 +03:00
|
|
|
$dispatcher->registerMiddleware(
|
|
|
|
new OC\AppFramework\Middleware\Security\CSPMiddleware(
|
|
|
|
$server->query(OC\Security\CSP\ContentSecurityPolicyManager::class),
|
|
|
|
$server->query(OC\Security\CSP\ContentSecurityPolicyNonceManager::class),
|
|
|
|
$server->query(OC\Security\CSRF\CsrfTokenManager::class)
|
|
|
|
)
|
|
|
|
);
|
2019-07-27 16:33:55 +03:00
|
|
|
$dispatcher->registerMiddleware(
|
|
|
|
$server->query(OC\AppFramework\Middleware\Security\FeaturePolicyMiddleware::class)
|
|
|
|
);
|
2018-11-23 13:11:10 +03:00
|
|
|
$dispatcher->registerMiddleware(
|
|
|
|
new OC\AppFramework\Middleware\Security\PasswordConfirmationMiddleware(
|
2020-07-13 12:18:14 +03:00
|
|
|
$c->get(IControllerMethodReflector::class),
|
|
|
|
$c->get(ISession::class),
|
|
|
|
$c->get(IUserSession::class),
|
|
|
|
$c->get(ITimeFactory::class)
|
2018-11-23 13:11:10 +03:00
|
|
|
)
|
2017-04-12 23:14:11 +03:00
|
|
|
);
|
2018-11-23 13:11:10 +03:00
|
|
|
$dispatcher->registerMiddleware(
|
|
|
|
new TwoFactorMiddleware(
|
2020-07-13 12:18:14 +03:00
|
|
|
$c->get(OC\Authentication\TwoFactorAuth\Manager::class),
|
|
|
|
$c->get(IUserSession::class),
|
|
|
|
$c->get(ISession::class),
|
|
|
|
$c->get(IURLGenerator::class),
|
|
|
|
$c->get(IControllerMethodReflector::class),
|
|
|
|
$c->get(IRequest::class)
|
2018-11-23 13:11:10 +03:00
|
|
|
)
|
2014-05-11 19:55:59 +04:00
|
|
|
);
|
2018-11-23 13:11:10 +03:00
|
|
|
$dispatcher->registerMiddleware(
|
|
|
|
new OC\AppFramework\Middleware\Security\BruteForceMiddleware(
|
2020-07-13 12:18:14 +03:00
|
|
|
$c->get(IControllerMethodReflector::class),
|
|
|
|
$c->get(OC\Security\Bruteforce\Throttler::class),
|
|
|
|
$c->get(IRequest::class)
|
2018-11-23 13:11:10 +03:00
|
|
|
)
|
2014-10-21 16:53:10 +04:00
|
|
|
);
|
2018-11-23 13:11:10 +03:00
|
|
|
$dispatcher->registerMiddleware(
|
|
|
|
new RateLimitingMiddleware(
|
2020-07-13 12:18:14 +03:00
|
|
|
$c->get(IRequest::class),
|
|
|
|
$c->get(IUserSession::class),
|
|
|
|
$c->get(IControllerMethodReflector::class),
|
|
|
|
$c->get(OC\Security\RateLimiting\Limiter::class)
|
2018-11-23 13:11:10 +03:00
|
|
|
)
|
2016-07-20 10:43:02 +03:00
|
|
|
);
|
2018-11-23 13:11:10 +03:00
|
|
|
$dispatcher->registerMiddleware(
|
|
|
|
new OC\AppFramework\Middleware\PublicShare\PublicShareMiddleware(
|
2020-07-13 12:18:14 +03:00
|
|
|
$c->get(IRequest::class),
|
|
|
|
$c->get(ISession::class),
|
|
|
|
$c->get(\OCP\IConfig::class)
|
2018-11-23 13:11:10 +03:00
|
|
|
)
|
2017-09-22 13:21:44 +03:00
|
|
|
);
|
2019-01-29 23:23:59 +03:00
|
|
|
$dispatcher->registerMiddleware(
|
2020-07-13 12:18:14 +03:00
|
|
|
$c->get(\OC\AppFramework\Middleware\AdditionalScriptsMiddleware::class)
|
2019-01-29 23:23:59 +03:00
|
|
|
);
|
2017-09-22 13:21:44 +03:00
|
|
|
|
2020-04-10 15:19:56 +03:00
|
|
|
foreach ($this->middleWares as $middleWare) {
|
2020-07-13 12:18:14 +03:00
|
|
|
$dispatcher->registerMiddleware($c->get($middleWare));
|
2013-09-27 19:15:26 +04:00
|
|
|
}
|
|
|
|
|
2018-11-23 13:11:10 +03:00
|
|
|
$dispatcher->registerMiddleware(
|
|
|
|
new SessionMiddleware(
|
2020-07-13 12:18:14 +03:00
|
|
|
$c->get(IControllerMethodReflector::class),
|
|
|
|
$c->get(ISession::class)
|
2018-11-23 13:11:10 +03:00
|
|
|
)
|
|
|
|
);
|
2013-08-17 13:16:48 +04:00
|
|
|
return $dispatcher;
|
|
|
|
});
|
|
|
|
|
2020-07-13 12:18:14 +03:00
|
|
|
$this->registerService(IAppConfig::class, function (ContainerInterface $c) {
|
2020-07-14 10:18:39 +03:00
|
|
|
return new OC\AppFramework\Services\AppConfig(
|
2020-07-13 12:18:14 +03:00
|
|
|
$c->get(IConfig::class),
|
|
|
|
$c->get('AppName')
|
2020-07-14 10:18:39 +03:00
|
|
|
);
|
|
|
|
});
|
2020-07-13 12:18:14 +03:00
|
|
|
$this->registerService(IInitialState::class, function (ContainerInterface $c) {
|
2020-07-14 10:18:39 +03:00
|
|
|
return new OC\AppFramework\Services\InitialState(
|
2020-07-13 12:18:14 +03:00
|
|
|
$c->get(IInitialStateService::class),
|
|
|
|
$c->get('AppName')
|
2020-07-14 10:18:39 +03:00
|
|
|
);
|
|
|
|
});
|
2013-08-17 13:16:48 +04:00
|
|
|
}
|
|
|
|
|
2013-08-21 03:02:15 +04:00
|
|
|
/**
|
2013-08-31 23:34:29 +04:00
|
|
|
* @return \OCP\IServerContainer
|
2013-08-21 03:02:15 +04:00
|
|
|
*/
|
2020-04-10 15:19:56 +03:00
|
|
|
public function getServer() {
|
2017-03-20 00:20:56 +03:00
|
|
|
return $this->server;
|
2013-08-21 03:02:15 +04:00
|
|
|
}
|
2013-09-25 12:30:48 +04:00
|
|
|
|
|
|
|
/**
|
2014-04-10 01:59:57 +04:00
|
|
|
* @param string $middleWare
|
2014-02-06 19:30:58 +04:00
|
|
|
* @return boolean|null
|
2013-09-25 12:30:48 +04:00
|
|
|
*/
|
2017-07-23 22:03:26 +03:00
|
|
|
public function registerMiddleWare($middleWare) {
|
2019-05-06 12:28:18 +03:00
|
|
|
if (in_array($middleWare, $this->middleWares, true) !== false) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-01-26 00:36:03 +03:00
|
|
|
$this->middleWares[] = $middleWare;
|
2013-09-25 12:30:48 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* used to return the appname of the set application
|
|
|
|
* @return string the name of your application
|
|
|
|
*/
|
2017-07-23 22:03:26 +03:00
|
|
|
public function getAppName() {
|
2013-09-25 12:30:48 +04:00
|
|
|
return $this->query('AppName');
|
|
|
|
}
|
2013-10-07 02:33:54 +04:00
|
|
|
|
|
|
|
/**
|
2014-12-16 21:07:14 +03:00
|
|
|
* @deprecated use IUserSession->isLoggedIn()
|
2013-10-07 02:33:54 +04:00
|
|
|
* @return boolean
|
|
|
|
*/
|
2017-07-23 22:03:26 +03:00
|
|
|
public function isLoggedIn() {
|
2017-03-02 18:52:05 +03:00
|
|
|
return \OC::$server->getUserSession()->isLoggedIn();
|
2013-10-07 02:33:54 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-12-16 22:33:13 +03:00
|
|
|
* @deprecated use IGroupManager->isAdmin($userId)
|
2013-10-07 02:33:54 +04:00
|
|
|
* @return boolean
|
|
|
|
*/
|
2017-07-23 22:03:26 +03:00
|
|
|
public function isAdminUser() {
|
2013-10-07 02:33:54 +04:00
|
|
|
$uid = $this->getUserId();
|
|
|
|
return \OC_User::isAdminUser($uid);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getUserId() {
|
2015-01-22 17:33:09 +03:00
|
|
|
return $this->getServer()->getSession()->get('user_id');
|
2013-10-07 02:33:54 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-12-16 21:50:31 +03:00
|
|
|
* @deprecated use the ILogger instead
|
2014-05-12 00:51:30 +04:00
|
|
|
* @param string $message
|
|
|
|
* @param string $level
|
2013-10-07 02:33:54 +04:00
|
|
|
* @return mixed
|
|
|
|
*/
|
2017-07-23 22:03:26 +03:00
|
|
|
public function log($message, $level) {
|
2020-04-10 15:19:56 +03:00
|
|
|
switch ($level) {
|
2013-10-07 02:33:54 +04:00
|
|
|
case 'debug':
|
2018-04-25 16:22:28 +03:00
|
|
|
$level = ILogger::DEBUG;
|
2013-10-07 02:33:54 +04:00
|
|
|
break;
|
|
|
|
case 'info':
|
2018-04-25 16:22:28 +03:00
|
|
|
$level = ILogger::INFO;
|
2013-10-07 02:33:54 +04:00
|
|
|
break;
|
|
|
|
case 'warn':
|
2018-04-25 16:22:28 +03:00
|
|
|
$level = ILogger::WARN;
|
2013-10-07 02:33:54 +04:00
|
|
|
break;
|
|
|
|
case 'fatal':
|
2018-04-25 16:22:28 +03:00
|
|
|
$level = ILogger::FATAL;
|
2013-10-07 02:33:54 +04:00
|
|
|
break;
|
|
|
|
default:
|
2018-04-25 16:22:28 +03:00
|
|
|
$level = ILogger::ERROR;
|
2013-10-07 02:33:54 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
\OCP\Util::writeLog($this->getAppName(), $message, $level);
|
|
|
|
}
|
2014-12-13 21:28:20 +03:00
|
|
|
|
2015-07-22 14:04:56 +03:00
|
|
|
/**
|
|
|
|
* Register a capability
|
|
|
|
*
|
|
|
|
* @param string $serviceName e.g. 'OCA\Files\Capabilities'
|
|
|
|
*/
|
|
|
|
public function registerCapability($serviceName) {
|
2020-04-09 14:53:40 +03:00
|
|
|
$this->query('OC\CapabilitiesManager')->registerCapability(function () use ($serviceName) {
|
2015-07-22 14:04:56 +03:00
|
|
|
return $this->query($serviceName);
|
|
|
|
});
|
2016-10-28 22:46:28 +03:00
|
|
|
}
|
2014-12-13 21:28:20 +03:00
|
|
|
|
2020-07-13 12:18:14 +03:00
|
|
|
public function has($id): bool {
|
|
|
|
if (parent::has($id)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->server->has($id, true)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-05-30 15:20:15 +03:00
|
|
|
public function query(string $name, bool $autoload = true) {
|
2017-03-22 12:33:30 +03:00
|
|
|
try {
|
|
|
|
return $this->queryNoFallback($name);
|
2017-09-12 12:54:13 +03:00
|
|
|
} catch (QueryException $firstException) {
|
|
|
|
try {
|
2019-05-30 15:20:15 +03:00
|
|
|
return $this->getServer()->query($name, $autoload);
|
2017-09-12 12:54:13 +03:00
|
|
|
} catch (QueryException $secondException) {
|
|
|
|
if ($firstException->getCode() === 1) {
|
|
|
|
throw $secondException;
|
|
|
|
}
|
|
|
|
throw $firstException;
|
|
|
|
}
|
2017-03-22 12:33:30 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $name
|
|
|
|
* @return mixed
|
|
|
|
* @throws QueryException if the query could not be resolved
|
|
|
|
*/
|
|
|
|
public function queryNoFallback($name) {
|
2017-03-17 16:25:35 +03:00
|
|
|
$name = $this->sanitizeName($name);
|
2017-03-20 00:09:47 +03:00
|
|
|
|
2017-03-20 01:14:47 +03:00
|
|
|
if ($this->offsetExists($name)) {
|
2017-03-17 16:25:35 +03:00
|
|
|
return parent::query($name);
|
2020-07-13 12:18:14 +03:00
|
|
|
} elseif ($this['AppName'] === 'settings' && strpos($name, 'OC\\Settings\\') === 0) {
|
|
|
|
return parent::query($name);
|
|
|
|
} elseif ($this['AppName'] === 'core' && strpos($name, 'OC\\Core\\') === 0) {
|
|
|
|
return parent::query($name);
|
|
|
|
} elseif (strpos($name, \OC\AppFramework\App::buildAppNamespace($this['AppName']) . '\\') === 0) {
|
|
|
|
return parent::query($name);
|
2017-03-17 16:25:35 +03:00
|
|
|
}
|
|
|
|
|
2017-03-22 12:33:30 +03:00
|
|
|
throw new QueryException('Could not resolve ' . $name . '!' .
|
2017-09-12 12:54:13 +03:00
|
|
|
' Class can not be instantiated', 1);
|
2015-07-22 14:04:56 +03:00
|
|
|
}
|
2013-08-17 13:16:48 +04:00
|
|
|
}
|