2019-01-29 23:23:59 +03:00
|
|
|
<?php
|
2019-12-03 21:57:53 +03:00
|
|
|
|
2019-01-29 23:23:59 +03:00
|
|
|
declare(strict_types=1);
|
2019-12-03 21:57:53 +03:00
|
|
|
|
2019-01-29 23:23:59 +03:00
|
|
|
/**
|
|
|
|
* @copyright Copyright (c) 2019, Roeland Jago Douma <roeland@famdouma.nl>
|
|
|
|
*
|
2019-12-19 15:16:31 +03:00
|
|
|
* @author Robin Appelman <robin@icewind.nl>
|
2019-01-29 23:23:59 +03:00
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
2019-12-03 21:57:53 +03:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2019-01-29 23:23:59 +03:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OC\AppFramework\Middleware;
|
|
|
|
|
2020-07-13 23:29:14 +03:00
|
|
|
use OCP\AppFramework\Http\Events\BeforeTemplateRenderedEvent;
|
2019-01-29 23:23:59 +03:00
|
|
|
use OCP\AppFramework\Http\Response;
|
2019-02-04 10:54:56 +03:00
|
|
|
use OCP\AppFramework\Http\StandaloneTemplateResponse;
|
2019-01-29 23:23:59 +03:00
|
|
|
use OCP\AppFramework\Http\TemplateResponse;
|
|
|
|
use OCP\AppFramework\Middleware;
|
2019-02-01 12:34:58 +03:00
|
|
|
use OCP\AppFramework\PublicShareController;
|
2019-12-09 16:24:57 +03:00
|
|
|
use OCP\EventDispatcher\GenericEvent;
|
2020-07-13 23:29:14 +03:00
|
|
|
use OCP\EventDispatcher\IEventDispatcher;
|
2019-01-29 23:23:59 +03:00
|
|
|
use OCP\IUserSession;
|
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
|
|
|
|
|
|
|
class AdditionalScriptsMiddleware extends Middleware {
|
|
|
|
/** @var EventDispatcherInterface */
|
2020-07-13 23:29:14 +03:00
|
|
|
private $legacyDispatcher;
|
2019-01-29 23:23:59 +03:00
|
|
|
/** @var IUserSession */
|
|
|
|
private $userSession;
|
2020-07-13 23:29:14 +03:00
|
|
|
/** @var IEventDispatcher */
|
|
|
|
private $dispatcher;
|
2019-01-29 23:23:59 +03:00
|
|
|
|
2020-07-13 23:29:14 +03:00
|
|
|
public function __construct(EventDispatcherInterface $legacyDispatcher, IUserSession $userSession, IEventDispatcher $dispatcher) {
|
|
|
|
$this->legacyDispatcher = $legacyDispatcher;
|
2019-01-29 23:23:59 +03:00
|
|
|
$this->userSession = $userSession;
|
2020-07-13 23:29:14 +03:00
|
|
|
$this->dispatcher = $dispatcher;
|
2019-01-29 23:23:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function afterController($controller, $methodName, Response $response): Response {
|
2019-02-01 12:34:58 +03:00
|
|
|
/*
|
|
|
|
* There is no need to emit these signals on a public share page
|
|
|
|
* There is a separate event for that already
|
|
|
|
*/
|
|
|
|
if ($controller instanceof PublicShareController) {
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
2019-01-29 23:23:59 +03:00
|
|
|
if ($response instanceof TemplateResponse) {
|
2020-07-13 23:29:14 +03:00
|
|
|
$this->legacyDispatcher->dispatch(TemplateResponse::EVENT_LOAD_ADDITIONAL_SCRIPTS, new GenericEvent());
|
2019-01-29 23:23:59 +03:00
|
|
|
|
2019-02-04 10:54:56 +03:00
|
|
|
if (!($response instanceof StandaloneTemplateResponse) && $this->userSession->isLoggedIn()) {
|
2020-07-13 23:29:14 +03:00
|
|
|
$this->legacyDispatcher->dispatch(TemplateResponse::EVENT_LOAD_ADDITIONAL_SCRIPTS_LOGGEDIN, new GenericEvent());
|
|
|
|
$isLoggedIn = true;
|
|
|
|
} else {
|
|
|
|
$isLoggedIn = false;
|
2019-01-29 23:23:59 +03:00
|
|
|
}
|
2020-07-13 23:29:14 +03:00
|
|
|
|
|
|
|
$this->dispatcher->dispatchTyped(new BeforeTemplateRenderedEvent($isLoggedIn));
|
2019-01-29 23:23:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
}
|