diff options
author | Julius Härtl <jus@bitgrid.net> | 2020-07-13 16:05:11 +0200 |
---|---|---|
committer | Morris Jobke <hey@morrisjobke.de> | 2020-07-15 13:55:26 +0200 |
commit | 217a69ebf9cd766c23b6f69037cbbe07cb00b7ce (patch) | |
tree | f95428cddfe142f569a5c1a2dfd35fd6e3648e5c /apps/files_sharing/lib | |
parent | 2ad95feab6dd525cdda5aa61c22119c98d21ca97 (diff) | |
download | nextcloud-server-217a69ebf9cd766c23b6f69037cbbe07cb00b7ce.tar.gz nextcloud-server-217a69ebf9cd766c23b6f69037cbbe07cb00b7ce.zip |
Add BeforeTemplateRenderedEvent for files_sharing
Signed-off-by: Julius Härtl <jus@bitgrid.net>
Diffstat (limited to 'apps/files_sharing/lib')
4 files changed, 125 insertions, 12 deletions
diff --git a/apps/files_sharing/lib/AppInfo/Application.php b/apps/files_sharing/lib/AppInfo/Application.php index 34cde50c438..acf1462601d 100644 --- a/apps/files_sharing/lib/AppInfo/Application.php +++ b/apps/files_sharing/lib/AppInfo/Application.php @@ -32,7 +32,9 @@ namespace OCA\Files_Sharing\AppInfo; use OC\AppFramework\Utility\SimpleContainer; use OCA\Files_Sharing\Capabilities; +use OCA\Files_Sharing\Event\BeforeTemplateRenderedEvent; use OCA\Files_Sharing\External\Manager; +use OCA\Files_Sharing\Listener\LegacyBeforeTemplateRenderedListener; use OCA\Files_Sharing\Listener\LoadAdditionalListener; use OCA\Files_Sharing\Listener\LoadSidebarListener; use OCA\Files_Sharing\Listener\ShareInteractionListener; @@ -138,6 +140,7 @@ class Application extends App { protected function registerEventsScripts(IEventDispatcher $dispatcher) { // sidebar and files scripts $dispatcher->addServiceListener(LoadAdditionalScriptsEvent::class, LoadAdditionalListener::class); + $dispatcher->addServiceListener(BeforeTemplateRenderedEvent::class, LegacyBeforeTemplateRenderedListener::class); $dispatcher->addServiceListener(LoadSidebar::class, LoadSidebarListener::class); $dispatcher->addServiceListener(ShareCreatedEvent::class, ShareInteractionListener::class); $dispatcher->addListener('\OCP\Collaboration\Resources::loadAdditionalScripts', function () { diff --git a/apps/files_sharing/lib/Controller/ShareController.php b/apps/files_sharing/lib/Controller/ShareController.php index 9e4e591e40d..359382441d6 100644 --- a/apps/files_sharing/lib/Controller/ShareController.php +++ b/apps/files_sharing/lib/Controller/ShareController.php @@ -48,6 +48,7 @@ use OC_Util; use OC\Security\CSP\ContentSecurityPolicy; use OCA\FederatedFileSharing\FederatedShareProvider; use OCA\Files_Sharing\Activity\Providers\Downloads; +use OCA\Files_Sharing\Event\BeforeTemplateRenderedEvent; use OCA\Viewer\Event\LoadViewer; use OCP\Accounts\IAccountManager; use OCP\AppFramework\AuthPublicShareController; @@ -58,6 +59,7 @@ use OCP\AppFramework\Http\Template\PublicTemplateResponse; use OCP\AppFramework\Http\Template\SimpleMenuAction; use OCP\AppFramework\Http\TemplateResponse; use OCP\Defaults; +use OCP\EventDispatcher\IEventDispatcher; use OCP\Files\Folder; use OCP\Files\IRootFolder; use OCP\Files\NotFoundException; @@ -75,8 +77,6 @@ use OCP\Share\Exceptions\ShareNotFound; use OCP\Share\IManager as ShareManager; use OCP\Share\IShare; use OCP\Template; -use Symfony\Component\EventDispatcher\EventDispatcherInterface; -use Symfony\Component\EventDispatcher\GenericEvent; /** * Class ShareController @@ -101,7 +101,7 @@ class ShareController extends AuthPublicShareController { protected $federatedShareProvider; /** @var IAccountManager */ protected $accountManager; - /** @var EventDispatcherInterface */ + /** @var IEventDispatcher */ protected $eventDispatcher; /** @var IL10N */ protected $l10n; @@ -127,7 +127,7 @@ class ShareController extends AuthPublicShareController { * @param IRootFolder $rootFolder * @param FederatedShareProvider $federatedShareProvider * @param IAccountManager $accountManager - * @param EventDispatcherInterface $eventDispatcher + * @param IEventDispatcher $eventDispatcher * @param IL10N $l10n * @param Defaults $defaults */ @@ -144,7 +144,7 @@ class ShareController extends AuthPublicShareController { IRootFolder $rootFolder, FederatedShareProvider $federatedShareProvider, IAccountManager $accountManager, - EventDispatcherInterface $eventDispatcher, + IEventDispatcher $eventDispatcher, IL10N $l10n, Defaults $defaults) { parent::__construct($appName, $request, $session, $urlGenerator); @@ -173,8 +173,7 @@ class ShareController extends AuthPublicShareController { public function showAuthenticate(): TemplateResponse { $templateParameters = ['share' => $this->share]; - $event = new GenericEvent(null, $templateParameters); - $this->eventDispatcher->dispatch('OCA\Files_Sharing::loadAdditionalScripts::publicShareAuth', $event); + $this->eventDispatcher->dispatchTyped(new BeforeTemplateRenderedEvent($this->share, BeforeTemplateRenderedEvent::SCOPE_PUBLIC_SHARE_AUTH)); $response = new TemplateResponse('core', 'publicshareauth', $templateParameters, 'guest'); if ($this->share->getSendPasswordByTalk()) { @@ -193,8 +192,7 @@ class ShareController extends AuthPublicShareController { protected function showAuthFailed(): TemplateResponse { $templateParameters = ['share' => $this->share, 'wrongpw' => true]; - $event = new GenericEvent(null, $templateParameters); - $this->eventDispatcher->dispatch('OCA\Files_Sharing::loadAdditionalScripts::publicShareAuth', $event); + $this->eventDispatcher->dispatchTyped(new BeforeTemplateRenderedEvent($this->share, BeforeTemplateRenderedEvent::SCOPE_PUBLIC_SHARE_AUTH)); $response = new TemplateResponse('core', 'publicshareauth', $templateParameters, 'guest'); if ($this->share->getSendPasswordByTalk()) { @@ -478,7 +476,7 @@ class ShareController extends AuthPublicShareController { // Load Viewer scripts if (class_exists(LoadViewer::class)) { - $this->eventDispatcher->dispatch(LoadViewer::class, new LoadViewer()); + $this->eventDispatcher->dispatchTyped(new LoadViewer()); } } @@ -490,8 +488,7 @@ class ShareController extends AuthPublicShareController { \OCP\Util::addHeader('meta', ['property' => "og:type", 'content' => "object"]); \OCP\Util::addHeader('meta', ['property' => "og:image", 'content' => $ogPreview]); - $event = new GenericEvent(null, ['share' => $share]); - $this->eventDispatcher->dispatch('OCA\Files_Sharing::loadAdditionalScripts', $event); + $this->eventDispatcher->dispatchTyped(new BeforeTemplateRenderedEvent($share)); $csp = new \OCP\AppFramework\Http\ContentSecurityPolicy(); $csp->addAllowedFrameDomain('\'self\''); diff --git a/apps/files_sharing/lib/Event/BeforeTemplateRenderedEvent.php b/apps/files_sharing/lib/Event/BeforeTemplateRenderedEvent.php new file mode 100644 index 00000000000..5f40eaca46d --- /dev/null +++ b/apps/files_sharing/lib/Event/BeforeTemplateRenderedEvent.php @@ -0,0 +1,54 @@ +<?php + +declare(strict_types=1); + +/** + * @copyright Copyright (c) 2020 Julius Härtl <jus@bitgrid.net> + * + * @author Julius Härtl <jus@bitgrid.net> + * + * @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 + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +namespace OCA\Files_Sharing\Event; + +use OCP\EventDispatcher\Event; +use OCP\Share\IShare; + +class BeforeTemplateRenderedEvent extends Event { + public const SCOPE_PUBLIC_SHARE_AUTH = 'publicShareAuth'; + + /** @var IShare */ + private $share; + /** @var string|null */ + private $scope; + + public function __construct(IShare $share, ?string $scope = null) { + parent::__construct(); + + $this->share = $share; + $this->scope = $scope; + } + + public function getShare(): IShare { + return $this->share; + } + + public function getScope(): ?string { + return $this->scope; + } +} diff --git a/apps/files_sharing/lib/Listener/LegacyBeforeTemplateRenderedListener.php b/apps/files_sharing/lib/Listener/LegacyBeforeTemplateRenderedListener.php new file mode 100644 index 00000000000..fb651f091f0 --- /dev/null +++ b/apps/files_sharing/lib/Listener/LegacyBeforeTemplateRenderedListener.php @@ -0,0 +1,59 @@ +<?php + +declare(strict_types=1); + +/** + * @copyright Copyright (c) 2019, Roeland Jago Douma <roeland@famdouma.nl> + * + * @author Joas Schilling <coding@schilljs.com> + * @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 + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +namespace OCA\Files_Sharing\Listener; + +use OC\EventDispatcher\SymfonyAdapter; +use OCA\Files_Sharing\Event\BeforeTemplateRenderedEvent; +use OCP\EventDispatcher\Event; +use OCP\EventDispatcher\IEventListener; +use Symfony\Component\EventDispatcher\GenericEvent; + +class LegacyBeforeTemplateRenderedListener implements IEventListener { + + /** @var SymfonyAdapter */ + private $dispatcher; + + public function __construct(SymfonyAdapter $dispatcher) { + $this->dispatcher = $dispatcher; + } + + public function handle(Event $event): void { + if (!($event instanceof BeforeTemplateRenderedEvent)) { + return; + } + + $eventName = 'OCA\Files_Sharing::loadAdditionalScripts'; + + if ($event->getScope() !== null) { + $eventName .= '::' . $event->getScope(); + } + + $legacyEvent = new GenericEvent(null, ['share' => $event->getShare()]); + $this->dispatcher->dispatch($eventName, $legacyEvent); + } +} |