Skip to content

Commit

Permalink
Merge pull request #39571 from nextcloud/bugfix/noid/move-lib-to-IEve…
Browse files Browse the repository at this point in the history
…ntDispatcher

Move lib/ events to IEventDispatcher where possible
  • Loading branch information
nickvergessen authored Jul 27, 2023
2 parents 5fb5d85 + 35c313e commit 6b72217
Show file tree
Hide file tree
Showing 27 changed files with 93 additions and 369 deletions.
1 change: 0 additions & 1 deletion apps/dav/lib/Connector/Sabre/Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
use OC\Authentication\Exceptions\PasswordLoginForbiddenException;
use OC\Authentication\TwoFactorAuth\Manager;
use OC\Security\Bruteforce\Throttler;
use OC\User\LoginException;
use OC\User\Session;
use OCA\DAV\Connector\Sabre\Exception\PasswordLoginForbidden;
use OCA\DAV\Connector\Sabre\Exception\TooManyRequests;
Expand Down
10 changes: 3 additions & 7 deletions lib/private/App/AppManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@
use OCP\IUserSession;
use OCP\Settings\IManager as ISettingsManager;
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

class AppManager implements IAppManager {
/**
Expand All @@ -79,7 +78,6 @@ class AppManager implements IAppManager {
private AppConfig $appConfig;
private IGroupManager $groupManager;
private ICacheFactory $memCacheFactory;
private EventDispatcherInterface $legacyDispatcher;
private IEventDispatcher $dispatcher;
private LoggerInterface $logger;

Expand Down Expand Up @@ -110,15 +108,13 @@ public function __construct(IUserSession $userSession,
AppConfig $appConfig,
IGroupManager $groupManager,
ICacheFactory $memCacheFactory,
EventDispatcherInterface $legacyDispatcher,
IEventDispatcher $dispatcher,
LoggerInterface $logger) {
$this->userSession = $userSession;
$this->config = $config;
$this->appConfig = $appConfig;
$this->groupManager = $groupManager;
$this->memCacheFactory = $memCacheFactory;
$this->legacyDispatcher = $legacyDispatcher;
$this->dispatcher = $dispatcher;
$this->logger = $logger;
}
Expand Down Expand Up @@ -543,7 +539,7 @@ public function enableApp(string $appId, bool $forceEnable = false): void {
$this->installedAppsCache[$appId] = 'yes';
$this->appConfig->setValue($appId, 'enabled', 'yes');
$this->dispatcher->dispatchTyped(new AppEnableEvent($appId));
$this->legacyDispatcher->dispatch(ManagerEvent::EVENT_APP_ENABLE, new ManagerEvent(
$this->dispatcher->dispatch(ManagerEvent::EVENT_APP_ENABLE, new ManagerEvent(
ManagerEvent::EVENT_APP_ENABLE, $appId
));
$this->clearAppsCache();
Expand Down Expand Up @@ -597,7 +593,7 @@ public function enableAppForGroups(string $appId, array $groups, bool $forceEnab
$this->installedAppsCache[$appId] = json_encode($groupIds);
$this->appConfig->setValue($appId, 'enabled', json_encode($groupIds));
$this->dispatcher->dispatchTyped(new AppEnableEvent($appId, $groupIds));
$this->legacyDispatcher->dispatch(ManagerEvent::EVENT_APP_ENABLE_FOR_GROUPS, new ManagerEvent(
$this->dispatcher->dispatch(ManagerEvent::EVENT_APP_ENABLE_FOR_GROUPS, new ManagerEvent(
ManagerEvent::EVENT_APP_ENABLE_FOR_GROUPS, $appId, $groups
));
$this->clearAppsCache();
Expand Down Expand Up @@ -633,7 +629,7 @@ public function disableApp($appId, $automaticDisabled = false) {
}

$this->dispatcher->dispatchTyped(new AppDisableEvent($appId));
$this->legacyDispatcher->dispatch(ManagerEvent::EVENT_APP_DISABLE, new ManagerEvent(
$this->dispatcher->dispatch(ManagerEvent::EVENT_APP_DISABLE, new ManagerEvent(
ManagerEvent::EVENT_APP_DISABLE, $appId
));
$this->clearAppsCache();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,44 +32,19 @@
use OCP\AppFramework\Http\StandaloneTemplateResponse;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\AppFramework\Middleware;
use OCP\AppFramework\PublicShareController;
use OCP\EventDispatcher\GenericEvent;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\IUserSession;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

class AdditionalScriptsMiddleware extends Middleware {
/** @var EventDispatcherInterface */
private $legacyDispatcher;
/** @var IUserSession */
private $userSession;
/** @var IEventDispatcher */
private $dispatcher;

public function __construct(EventDispatcherInterface $legacyDispatcher, IUserSession $userSession, IEventDispatcher $dispatcher) {
$this->legacyDispatcher = $legacyDispatcher;
$this->userSession = $userSession;
$this->dispatcher = $dispatcher;
public function __construct(
private IUserSession $userSession,
private IEventDispatcher $dispatcher,
) {
}

public function afterController($controller, $methodName, Response $response): Response {
if ($response instanceof TemplateResponse) {
if (!$controller instanceof PublicShareController) {
/*
* The old event was not dispatched on the public share controller as there was
* OCA\Files_Sharing::loadAdditionalScripts for that. This is kept for compatibility reasons
* only for the old event as this is now also included in BeforeTemplateRenderedEvent
*/
$this->legacyDispatcher->dispatch(TemplateResponse::EVENT_LOAD_ADDITIONAL_SCRIPTS, new GenericEvent());
}

if (!($response instanceof StandaloneTemplateResponse) && $this->userSession->isLoggedIn()) {
$this->legacyDispatcher->dispatch(TemplateResponse::EVENT_LOAD_ADDITIONAL_SCRIPTS_LOGGEDIN, new GenericEvent());
$isLoggedIn = true;
} else {
$isLoggedIn = false;
}

$isLoggedIn = !($response instanceof StandaloneTemplateResponse) && $this->userSession->isLoggedIn();
$this->dispatcher->dispatchTyped(new BeforeTemplateRenderedEvent($isLoggedIn, $response));
}

Expand Down
15 changes: 1 addition & 14 deletions lib/private/Authentication/TwoFactorAuth/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@
use OCP\IUser;
use OCP\Session\Exceptions\SessionNotAvailableException;
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\GenericEvent;
use function array_diff;
use function array_filter;

Expand Down Expand Up @@ -87,9 +85,6 @@ class Manager {
/** @var IEventDispatcher */
private $dispatcher;

/** @var EventDispatcherInterface */
private $legacyDispatcher;

/** @psalm-var array<string, bool> */
private $userIsTwoFactorAuthenticated = [];

Expand All @@ -102,8 +97,7 @@ public function __construct(ProviderLoader $providerLoader,
LoggerInterface $logger,
TokenProvider $tokenProvider,
ITimeFactory $timeFactory,
IEventDispatcher $eventDispatcher,
EventDispatcherInterface $legacyDispatcher) {
IEventDispatcher $eventDispatcher) {
$this->providerLoader = $providerLoader;
$this->providerRegistry = $providerRegistry;
$this->mandatoryTwoFactor = $mandatoryTwoFactor;
Expand All @@ -114,7 +108,6 @@ public function __construct(ProviderLoader $providerLoader,
$this->tokenProvider = $tokenProvider;
$this->timeFactory = $timeFactory;
$this->dispatcher = $eventDispatcher;
$this->legacyDispatcher = $legacyDispatcher;
}

/**
Expand Down Expand Up @@ -284,19 +277,13 @@ public function verifyChallenge(string $providerId, IUser $user, string $challen
$tokenId = $token->getId();
$this->config->deleteUserValue($user->getUID(), 'login_token_2fa', (string)$tokenId);

$dispatchEvent = new GenericEvent($user, ['provider' => $provider->getDisplayName()]);
$this->legacyDispatcher->dispatch(IProvider::EVENT_SUCCESS, $dispatchEvent);

$this->dispatcher->dispatchTyped(new TwoFactorProviderForUserEnabled($user, $provider));
$this->dispatcher->dispatchTyped(new TwoFactorProviderChallengePassed($user, $provider));

$this->publishEvent($user, 'twofactor_success', [
'provider' => $provider->getDisplayName(),
]);
} else {
$dispatchEvent = new GenericEvent($user, ['provider' => $provider->getDisplayName()]);
$this->legacyDispatcher->dispatch(IProvider::EVENT_FAILED, $dispatchEvent);

$this->dispatcher->dispatchTyped(new TwoFactorProviderForUserDisabled($user, $provider));
$this->dispatcher->dispatchTyped(new TwoFactorProviderChallengeFailed($user, $provider));

Expand Down
38 changes: 13 additions & 25 deletions lib/private/Files/Node/HookConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
use OCP\Files\FileInfo;
use OCP\Files\IRootFolder;
use OCP\Util;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

class HookConnector {
/** @var IRootFolder */
Expand All @@ -58,26 +57,15 @@ class HookConnector {
/** @var FileInfo[] */
private $deleteMetaCache = [];

/** @var EventDispatcherInterface */
private $legacyDispatcher;

/** @var IEventDispatcher */
private $dispatcher;

/**
* HookConnector constructor.
*
* @param Root $root
* @param View $view
*/
public function __construct(
IRootFolder $root,
View $view,
EventDispatcherInterface $legacyDispatcher,
IEventDispatcher $dispatcher) {
$this->root = $root;
$this->view = $view;
$this->legacyDispatcher = $legacyDispatcher;
$this->dispatcher = $dispatcher;
}

Expand Down Expand Up @@ -106,7 +94,7 @@ public function viewToNode() {
public function write($arguments) {
$node = $this->getNodeForPath($arguments['path']);
$this->root->emit('\OC\Files', 'preWrite', [$node]);
$this->legacyDispatcher->dispatch('\OCP\Files::preWrite', new GenericEvent($node));
$this->dispatcher->dispatch('\OCP\Files::preWrite', new GenericEvent($node));

$event = new BeforeNodeWrittenEvent($node);
$this->dispatcher->dispatchTyped($event);
Expand All @@ -115,7 +103,7 @@ public function write($arguments) {
public function postWrite($arguments) {
$node = $this->getNodeForPath($arguments['path']);
$this->root->emit('\OC\Files', 'postWrite', [$node]);
$this->legacyDispatcher->dispatch('\OCP\Files::postWrite', new GenericEvent($node));
$this->dispatcher->dispatch('\OCP\Files::postWrite', new GenericEvent($node));

$event = new NodeWrittenEvent($node);
$this->dispatcher->dispatchTyped($event);
Expand All @@ -124,7 +112,7 @@ public function postWrite($arguments) {
public function create($arguments) {
$node = $this->getNodeForPath($arguments['path']);
$this->root->emit('\OC\Files', 'preCreate', [$node]);
$this->legacyDispatcher->dispatch('\OCP\Files::preCreate', new GenericEvent($node));
$this->dispatcher->dispatch('\OCP\Files::preCreate', new GenericEvent($node));

$event = new BeforeNodeCreatedEvent($node);
$this->dispatcher->dispatchTyped($event);
Expand All @@ -133,7 +121,7 @@ public function create($arguments) {
public function postCreate($arguments) {
$node = $this->getNodeForPath($arguments['path']);
$this->root->emit('\OC\Files', 'postCreate', [$node]);
$this->legacyDispatcher->dispatch('\OCP\Files::postCreate', new GenericEvent($node));
$this->dispatcher->dispatch('\OCP\Files::postCreate', new GenericEvent($node));

$event = new NodeCreatedEvent($node);
$this->dispatcher->dispatchTyped($event);
Expand All @@ -143,7 +131,7 @@ public function delete($arguments) {
$node = $this->getNodeForPath($arguments['path']);
$this->deleteMetaCache[$node->getPath()] = $node->getFileInfo();
$this->root->emit('\OC\Files', 'preDelete', [$node]);
$this->legacyDispatcher->dispatch('\OCP\Files::preDelete', new GenericEvent($node));
$this->dispatcher->dispatch('\OCP\Files::preDelete', new GenericEvent($node));

$event = new BeforeNodeDeletedEvent($node);
$this->dispatcher->dispatchTyped($event);
Expand All @@ -153,7 +141,7 @@ public function postDelete($arguments) {
$node = $this->getNodeForPath($arguments['path']);
unset($this->deleteMetaCache[$node->getPath()]);
$this->root->emit('\OC\Files', 'postDelete', [$node]);
$this->legacyDispatcher->dispatch('\OCP\Files::postDelete', new GenericEvent($node));
$this->dispatcher->dispatch('\OCP\Files::postDelete', new GenericEvent($node));

$event = new NodeDeletedEvent($node);
$this->dispatcher->dispatchTyped($event);
Expand All @@ -162,7 +150,7 @@ public function postDelete($arguments) {
public function touch($arguments) {
$node = $this->getNodeForPath($arguments['path']);
$this->root->emit('\OC\Files', 'preTouch', [$node]);
$this->legacyDispatcher->dispatch('\OCP\Files::preTouch', new GenericEvent($node));
$this->dispatcher->dispatch('\OCP\Files::preTouch', new GenericEvent($node));

$event = new BeforeNodeTouchedEvent($node);
$this->dispatcher->dispatchTyped($event);
Expand All @@ -171,7 +159,7 @@ public function touch($arguments) {
public function postTouch($arguments) {
$node = $this->getNodeForPath($arguments['path']);
$this->root->emit('\OC\Files', 'postTouch', [$node]);
$this->legacyDispatcher->dispatch('\OCP\Files::postTouch', new GenericEvent($node));
$this->dispatcher->dispatch('\OCP\Files::postTouch', new GenericEvent($node));

$event = new NodeTouchedEvent($node);
$this->dispatcher->dispatchTyped($event);
Expand All @@ -181,7 +169,7 @@ public function rename($arguments) {
$source = $this->getNodeForPath($arguments['oldpath']);
$target = $this->getNodeForPath($arguments['newpath']);
$this->root->emit('\OC\Files', 'preRename', [$source, $target]);
$this->legacyDispatcher->dispatch('\OCP\Files::preRename', new GenericEvent([$source, $target]));
$this->dispatcher->dispatch('\OCP\Files::preRename', new GenericEvent([$source, $target]));

$event = new BeforeNodeRenamedEvent($source, $target);
$this->dispatcher->dispatchTyped($event);
Expand All @@ -191,7 +179,7 @@ public function postRename($arguments) {
$source = $this->getNodeForPath($arguments['oldpath']);
$target = $this->getNodeForPath($arguments['newpath']);
$this->root->emit('\OC\Files', 'postRename', [$source, $target]);
$this->legacyDispatcher->dispatch('\OCP\Files::postRename', new GenericEvent([$source, $target]));
$this->dispatcher->dispatch('\OCP\Files::postRename', new GenericEvent([$source, $target]));

$event = new NodeRenamedEvent($source, $target);
$this->dispatcher->dispatchTyped($event);
Expand All @@ -201,7 +189,7 @@ public function copy($arguments) {
$source = $this->getNodeForPath($arguments['oldpath']);
$target = $this->getNodeForPath($arguments['newpath']);
$this->root->emit('\OC\Files', 'preCopy', [$source, $target]);
$this->legacyDispatcher->dispatch('\OCP\Files::preCopy', new GenericEvent([$source, $target]));
$this->dispatcher->dispatch('\OCP\Files::preCopy', new GenericEvent([$source, $target]));

$event = new BeforeNodeCopiedEvent($source, $target);
$this->dispatcher->dispatchTyped($event);
Expand All @@ -211,7 +199,7 @@ public function postCopy($arguments) {
$source = $this->getNodeForPath($arguments['oldpath']);
$target = $this->getNodeForPath($arguments['newpath']);
$this->root->emit('\OC\Files', 'postCopy', [$source, $target]);
$this->legacyDispatcher->dispatch('\OCP\Files::postCopy', new GenericEvent([$source, $target]));
$this->dispatcher->dispatch('\OCP\Files::postCopy', new GenericEvent([$source, $target]));

$event = new NodeCopiedEvent($source, $target);
$this->dispatcher->dispatchTyped($event);
Expand All @@ -220,7 +208,7 @@ public function postCopy($arguments) {
public function read($arguments) {
$node = $this->getNodeForPath($arguments['path']);
$this->root->emit('\OC\Files', 'read', [$node]);
$this->legacyDispatcher->dispatch('\OCP\Files::read', new GenericEvent([$node]));
$this->dispatcher->dispatch('\OCP\Files::read', new GenericEvent([$node]));

$event = new BeforeNodeReadEvent($node);
$this->dispatcher->dispatchTyped($event);
Expand Down
10 changes: 0 additions & 10 deletions lib/private/Preview/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@
use OCP\Preview\BeforePreviewFetchedEvent;
use OCP\Preview\IProviderV2;
use OCP\Preview\IVersionedPreviewFile;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\GenericEvent;

class Generator {
public const SEMAPHORE_ID_ALL = 0x0a11;
Expand All @@ -59,8 +57,6 @@ class Generator {
private $appData;
/** @var GeneratorHelper */
private $helper;
/** @var EventDispatcherInterface */
private $legacyEventDispatcher;
/** @var IEventDispatcher */
private $eventDispatcher;

Expand All @@ -69,14 +65,12 @@ public function __construct(
IPreview $previewManager,
IAppData $appData,
GeneratorHelper $helper,
EventDispatcherInterface $legacyEventDispatcher,
IEventDispatcher $eventDispatcher
) {
$this->config = $config;
$this->previewManager = $previewManager;
$this->appData = $appData;
$this->helper = $helper;
$this->legacyEventDispatcher = $legacyEventDispatcher;
$this->eventDispatcher = $eventDispatcher;
}

Expand Down Expand Up @@ -104,10 +98,6 @@ public function getPreview(File $file, $width = -1, $height = -1, $crop = false,
'mode' => $mode,
];

$this->legacyEventDispatcher->dispatch(
IPreview::EVENT,
new GenericEvent($file, $specification)
);
$this->eventDispatcher->dispatchTyped(new BeforePreviewFetchedEvent(
$file,
$width,
Expand Down
Loading

0 comments on commit 6b72217

Please sign in to comment.