зеркало из https://github.com/nextcloud/spreed.git
Use DI for the middleware
Signed-off-by: Joas Schilling <coding@schilljs.com>
This commit is contained in:
Родитель
9643156a29
Коммит
1c950c6a93
|
@ -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 {
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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) {
|
||||
|
|
Загрузка…
Ссылка в новой задаче