diff options
author | Joas Schilling <coding@schilljs.com> | 2019-10-17 09:59:29 +0300 |
---|---|---|
committer | Joas Schilling <coding@schilljs.com> | 2019-10-17 11:09:37 +0300 |
commit | 1c950c6a934f563629187370e00038a59c953b67 (patch) | |
tree | e0a4abc2a94985800aeff380ae86cbcbfd5f233c /lib | |
parent | 9643156a29479ae8f62fb18d4048019bb268db23 (diff) |
Use DI for the middleware
Signed-off-by: Joas Schilling <coding@schilljs.com>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/AppInfo/Application.php | 26 | ||||
-rw-r--r-- | lib/Middleware/CanUseTalkMiddleware.php | 18 | ||||
-rw-r--r-- | lib/Middleware/InjectionMiddleware.php | 8 |
3 files changed, 18 insertions, 34 deletions
diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php index e1837f65e..8b9cce0d0 100644 --- a/lib/AppInfo/Application.php +++ b/lib/AppInfo/Application.php @@ -67,33 +67,11 @@ class Application extends App { public function __construct(array $urlParams = []) { parent::__construct('spreed', $urlParams); - $server = $this->getContainer()->getServer(); - $this->getContainer()->registerService('CanUseTalkMiddleware', function() use ($server) { - /** @var Config $config */ - $config = $server->query(Config::class); - $user = $server->getUserSession()->getUser(); - - return new CanUseTalkMiddleware( - !$user instanceof IUser || - !$config->isDisabledForUser($user) - ); - }); - - $this->getContainer()->registerService('InjectionMiddleware', function() use ($server) { - return new InjectionMiddleware( - $server->getRequest(), - $server->query(IControllerMethodReflector::class), - $this->getContainer()->query(TalkSession::class), - $this->getContainer()->query(Manager::class), - $this->getContainer()->query('userId') - ); - }); - // This needs to be in the constructor, // because otherwise the middleware is registered on a wrong object, // when it is requested by the Router. - $this->getContainer()->registerMiddleWare('CanUseTalkMiddleware'); - $this->getContainer()->registerMiddleWare('InjectionMiddleware'); + $this->getContainer()->registerMiddleWare(CanUseTalkMiddleware::class); + $this->getContainer()->registerMiddleWare(InjectionMiddleware::class); } public function register(): void { diff --git a/lib/Middleware/CanUseTalkMiddleware.php b/lib/Middleware/CanUseTalkMiddleware.php index d2fa92c25..791c17cbb 100644 --- a/lib/Middleware/CanUseTalkMiddleware.php +++ b/lib/Middleware/CanUseTalkMiddleware.php @@ -22,6 +22,7 @@ declare(strict_types=1); namespace OCA\Talk\Middleware; +use OCA\Talk\Config; use OCA\Talk\Exceptions\ForbiddenException; use OCA\Talk\Middleware\Exceptions\CanNotUseTalkException; use OCP\AppFramework\Controller; @@ -31,14 +32,20 @@ use OCP\AppFramework\Http\RedirectToDefaultAppResponse; use OCP\AppFramework\Middleware; use OCP\AppFramework\OCS\OCSException; use OCP\AppFramework\OCSController; +use OCP\IUser; +use OCP\IUserSession; class CanUseTalkMiddleware extends Middleware { - /** @var bool */ - private $canAccessTalk; + /** @var IUserSession */ + private $userSession; + /** @var Config */ + private $config; - public function __construct(bool $canAccessTalk) { - $this->canAccessTalk = $canAccessTalk; + public function __construct(IUserSession $userSession, + Config $config) { + $this->userSession = $userSession; + $this->config = $config; } /** @@ -48,7 +55,8 @@ class CanUseTalkMiddleware extends Middleware { * @throws CanNotUseTalkException */ public function beforeController($controller, $methodName): void { - if (!$this->canAccessTalk) { + $user = $this->userSession->getUser(); + if ($user instanceof IUser && $this->config->isDisabledForUser($user)) { throw new CanNotUseTalkException(); } } diff --git a/lib/Middleware/InjectionMiddleware.php b/lib/Middleware/InjectionMiddleware.php index 0ede2fa4d..a9da6e4df 100644 --- a/lib/Middleware/InjectionMiddleware.php +++ b/lib/Middleware/InjectionMiddleware.php @@ -22,9 +22,7 @@ declare(strict_types=1); namespace OCA\Talk\Middleware; -use OC\AppFramework\Utility\ControllerMethodReflector; use OCA\Talk\Controller\AEnvironmentAwareController; -use OCA\Talk\Controller\EnvironmentAwareTrait; use OCA\Talk\Exceptions\ParticipantNotFoundException; use OCA\Talk\Exceptions\RoomNotFoundException; use OCA\Talk\Manager; @@ -41,14 +39,14 @@ use OCP\AppFramework\Http\RedirectToDefaultAppResponse; use OCP\AppFramework\Middleware; use OCP\AppFramework\OCS\OCSException; use OCP\AppFramework\OCSController; +use OCP\AppFramework\Utility\IControllerMethodReflector; use OCP\IRequest; -use OCP\IUserSession; class InjectionMiddleware extends Middleware { /** @var IRequest */ private $request; - /** @var ControllerMethodReflector */ + /** @var IControllerMethodReflector */ private $reflector; /** @var TalkSession */ private $talkSession; @@ -58,7 +56,7 @@ class InjectionMiddleware extends Middleware { private $userId; public function __construct(IRequest $request, - ControllerMethodReflector $reflector, + IControllerMethodReflector $reflector, TalkSession $talkSession, Manager $manager, ?string $userId) { |