Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add LoadAdditionalScriptsEvent for files_sharing #21815

Merged
merged 1 commit into from
Jul 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions apps/files_sharing/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
'OCA\\Files_Sharing\\Controller\\ShareInfoController' => $baseDir . '/../lib/Controller/ShareInfoController.php',
'OCA\\Files_Sharing\\Controller\\ShareesAPIController' => $baseDir . '/../lib/Controller/ShareesAPIController.php',
'OCA\\Files_Sharing\\DeleteOrphanedSharesJob' => $baseDir . '/../lib/DeleteOrphanedSharesJob.php',
'OCA\\Files_Sharing\\Event\\BeforeTemplateRenderedEvent' => $baseDir . '/../lib/Event/BeforeTemplateRenderedEvent.php',
'OCA\\Files_Sharing\\Exceptions\\BrokenPath' => $baseDir . '/../lib/Exceptions/BrokenPath.php',
'OCA\\Files_Sharing\\Exceptions\\S2SException' => $baseDir . '/../lib/Exceptions/S2SException.php',
'OCA\\Files_Sharing\\Exceptions\\SharingRightsException' => $baseDir . '/../lib/Exceptions/SharingRightsException.php',
Expand All @@ -48,6 +49,7 @@
'OCA\\Files_Sharing\\Helper' => $baseDir . '/../lib/Helper.php',
'OCA\\Files_Sharing\\Hooks' => $baseDir . '/../lib/Hooks.php',
'OCA\\Files_Sharing\\ISharedStorage' => $baseDir . '/../lib/ISharedStorage.php',
'OCA\\Files_Sharing\\Listener\\LegacyBeforeTemplateRenderedListener' => $baseDir . '/../lib/Listener/LegacyBeforeTemplateRenderedListener.php',
'OCA\\Files_Sharing\\Listener\\LoadAdditionalListener' => $baseDir . '/../lib/Listener/LoadAdditionalListener.php',
'OCA\\Files_Sharing\\Listener\\LoadSidebarListener' => $baseDir . '/../lib/Listener/LoadSidebarListener.php',
'OCA\\Files_Sharing\\Listener\\ShareInteractionListener' => $baseDir . '/../lib/Listener/ShareInteractionListener.php',
Expand Down
2 changes: 2 additions & 0 deletions apps/files_sharing/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class ComposerStaticInitFiles_Sharing
'OCA\\Files_Sharing\\Controller\\ShareInfoController' => __DIR__ . '/..' . '/../lib/Controller/ShareInfoController.php',
'OCA\\Files_Sharing\\Controller\\ShareesAPIController' => __DIR__ . '/..' . '/../lib/Controller/ShareesAPIController.php',
'OCA\\Files_Sharing\\DeleteOrphanedSharesJob' => __DIR__ . '/..' . '/../lib/DeleteOrphanedSharesJob.php',
'OCA\\Files_Sharing\\Event\\BeforeTemplateRenderedEvent' => __DIR__ . '/..' . '/../lib/Event/BeforeTemplateRenderedEvent.php',
'OCA\\Files_Sharing\\Exceptions\\BrokenPath' => __DIR__ . '/..' . '/../lib/Exceptions/BrokenPath.php',
'OCA\\Files_Sharing\\Exceptions\\S2SException' => __DIR__ . '/..' . '/../lib/Exceptions/S2SException.php',
'OCA\\Files_Sharing\\Exceptions\\SharingRightsException' => __DIR__ . '/..' . '/../lib/Exceptions/SharingRightsException.php',
Expand All @@ -63,6 +64,7 @@ class ComposerStaticInitFiles_Sharing
'OCA\\Files_Sharing\\Helper' => __DIR__ . '/..' . '/../lib/Helper.php',
'OCA\\Files_Sharing\\Hooks' => __DIR__ . '/..' . '/../lib/Hooks.php',
'OCA\\Files_Sharing\\ISharedStorage' => __DIR__ . '/..' . '/../lib/ISharedStorage.php',
'OCA\\Files_Sharing\\Listener\\LegacyBeforeTemplateRenderedListener' => __DIR__ . '/..' . '/../lib/Listener/LegacyBeforeTemplateRenderedListener.php',
'OCA\\Files_Sharing\\Listener\\LoadAdditionalListener' => __DIR__ . '/..' . '/../lib/Listener/LoadAdditionalListener.php',
'OCA\\Files_Sharing\\Listener\\LoadSidebarListener' => __DIR__ . '/..' . '/../lib/Listener/LoadSidebarListener.php',
'OCA\\Files_Sharing\\Listener\\ShareInteractionListener' => __DIR__ . '/..' . '/../lib/Listener/ShareInteractionListener.php',
Expand Down
3 changes: 3 additions & 0 deletions apps/files_sharing/lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@

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;
Expand Down Expand Up @@ -138,6 +140,7 @@ protected function registerMountProviders(IMountProviderCollection $mountProvide
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 () {
Expand Down
21 changes: 9 additions & 12 deletions apps/files_sharing/lib/Controller/ShareController.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
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;
Expand All @@ -58,6 +59,7 @@
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;
Expand All @@ -75,8 +77,6 @@
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
Expand All @@ -101,7 +101,7 @@ class ShareController extends AuthPublicShareController {
protected $federatedShareProvider;
/** @var IAccountManager */
protected $accountManager;
/** @var EventDispatcherInterface */
/** @var IEventDispatcher */
protected $eventDispatcher;
/** @var IL10N */
protected $l10n;
Expand All @@ -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
*/
Expand All @@ -144,7 +144,7 @@ public function __construct(string $appName,
IRootFolder $rootFolder,
FederatedShareProvider $federatedShareProvider,
IAccountManager $accountManager,
EventDispatcherInterface $eventDispatcher,
IEventDispatcher $eventDispatcher,
IL10N $l10n,
Defaults $defaults) {
parent::__construct($appName, $request, $session, $urlGenerator);
Expand Down Expand Up @@ -173,8 +173,7 @@ public function __construct(string $appName,
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()) {
Expand All @@ -193,8 +192,7 @@ public function showAuthenticate(): TemplateResponse {
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()) {
Expand Down Expand Up @@ -478,7 +476,7 @@ public function showShare($path = ''): TemplateResponse {

// Load Viewer scripts
if (class_exists(LoadViewer::class)) {
$this->eventDispatcher->dispatch(LoadViewer::class, new LoadViewer());
$this->eventDispatcher->dispatchTyped(new LoadViewer());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can do that? :O

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep - that's the new way and makes things fully typed and thus allows the be a bit more safe.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also you just register for the ::class and then the IDE finds properly all usages ;)

}
}

Expand All @@ -490,8 +488,7 @@ public function showShare($path = ''): TemplateResponse {
\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\'');
Expand Down
54 changes: 54 additions & 0 deletions apps/files_sharing/lib/Event/BeforeTemplateRenderedEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

declare(strict_types=1);

/**
* @copyright Copyright (c) 2020 Julius Härtl <[email protected]>
*
* @author Julius Härtl <[email protected]>
*
* @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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

declare(strict_types=1);

/**
* @copyright Copyright (c) 2019, Roeland Jago Douma <[email protected]>
*
* @author Joas Schilling <[email protected]>
* @author Roeland Jago Douma <[email protected]>
*
* @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);
}
}
21 changes: 9 additions & 12 deletions apps/files_sharing/tests/Controller/ShareControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
use OCP\AppFramework\Http\Template\PublicTemplateResponse;
use OCP\AppFramework\Http\Template\SimpleMenuAction;
use OCP\Constants;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Files\File;
use OCP\Files\NotFoundException;
use OCP\Files\Storage;
Expand All @@ -64,7 +65,6 @@
use OCP\Share\Exceptions\ShareNotFound;
use OCP\Share\IShare;
use PHPUnit\Framework\MockObject\MockObject;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use OCP\Activity\IManager;
use OCP\Files\IRootFolder;
use OCP\Defaults;
Expand Down Expand Up @@ -102,7 +102,7 @@ class ShareControllerTest extends \Test\TestCase {
private $federatedShareProvider;
/** @var IAccountManager|MockObject */
private $accountManager;
/** @var EventDispatcherInterface|MockObject */
/** @var IEventDispatcher|MockObject */
private $eventDispatcher;
/** @var IL10N */
private $l10n;
Expand All @@ -123,7 +123,7 @@ protected function setUp(): void {
$this->federatedShareProvider->expects($this->any())
->method('isIncomingServer2serverShareEnabled')->willReturn(true);
$this->accountManager = $this->createMock(IAccountManager::class);
$this->eventDispatcher = $this->createMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
$this->eventDispatcher = $this->createMock(IEventDispatcher::class);
$this->l10n = $this->createMock(IL10N::class);

$this->shareController = new \OCA\Files_Sharing\Controller\ShareController(
Expand Down Expand Up @@ -296,11 +296,10 @@ public function testShowShare() {
});

$this->eventDispatcher->expects($this->once())
->method('dispatch')
->method('dispatchTyped')
->with(
'OCA\Files_Sharing::loadAdditionalScripts',
$this->callback(function ($event) use ($share) {
return $event->getArgument('share') === $share;
return $event->getShare() === $share;
})
);

Expand Down Expand Up @@ -443,11 +442,10 @@ public function testShowShareWithPrivateName() {
});

$this->eventDispatcher->expects($this->once())
->method('dispatch')
->method('dispatchTyped')
->with(
'OCA\Files_Sharing::loadAdditionalScripts',
$this->callback(function ($event) use ($share) {
return $event->getArgument('share') === $share;
return $event->getShare() === $share;
})
);

Expand Down Expand Up @@ -594,11 +592,10 @@ public function testShowShareHideDownload() {
});

$this->eventDispatcher->expects($this->once())
->method('dispatch')
->method('dispatchTyped')
->with(
'OCA\Files_Sharing::loadAdditionalScripts',
$this->callback(function ($event) use ($share) {
return $event->getArgument('share') === $share;
return $event->getShare() === $share;
})
);

Expand Down