summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChristoph Wurst <ChristophWurst@users.noreply.github.com>2021-03-02 08:18:59 +0100
committerGitHub <noreply@github.com>2021-03-02 08:18:59 +0100
commit7be2ce82e7a8274070b99c5dc58bb99935f168c8 (patch)
treea955d64b08b963fccdfc929e57515c2b2230f380
parent1d5cb3f6402bf0042fa13c21301e3a2959181d8a (diff)
parentf8808e260dacfd8214e405d493365f4ecaf4d953 (diff)
downloadnextcloud-server-7be2ce82e7a8274070b99c5dc58bb99935f168c8.tar.gz
nextcloud-server-7be2ce82e7a8274070b99c5dc58bb99935f168c8.zip
Merge pull request #25544 from nextcloud/refactor/app-password-created-event
Move app_password_created to a typed event
-rw-r--r--apps/settings/composer/composer/autoload_classmap.php1
-rw-r--r--apps/settings/composer/composer/autoload_static.php1
-rw-r--r--apps/settings/lib/AppInfo/Application.php37
-rw-r--r--apps/settings/lib/Listener/AppPasswordCreatedActivityListener.php73
-rw-r--r--core/Controller/AppPasswordController.php13
-rw-r--r--core/Controller/ClientFlowLoginController.php15
-rw-r--r--lib/composer/composer/autoload_classmap.php1
-rw-r--r--lib/composer/composer/autoload_static.php1
-rw-r--r--lib/private/Authentication/Events/AppPasswordCreatedEvent.php43
9 files changed, 140 insertions, 45 deletions
diff --git a/apps/settings/composer/composer/autoload_classmap.php b/apps/settings/composer/composer/autoload_classmap.php
index bf15d877fe0..a6f2d2e5685 100644
--- a/apps/settings/composer/composer/autoload_classmap.php
+++ b/apps/settings/composer/composer/autoload_classmap.php
@@ -32,6 +32,7 @@ return array(
'OCA\\Settings\\Controller\\WebAuthnController' => $baseDir . '/../lib/Controller/WebAuthnController.php',
'OCA\\Settings\\Events\\BeforeTemplateRenderedEvent' => $baseDir . '/../lib/Events/BeforeTemplateRenderedEvent.php',
'OCA\\Settings\\Hooks' => $baseDir . '/../lib/Hooks.php',
+ 'OCA\\Settings\\Listener\\AppPasswordCreatedActivityListener' => $baseDir . '/../lib/Listener/AppPasswordCreatedActivityListener.php',
'OCA\\Settings\\Mailer\\NewUserMailHelper' => $baseDir . '/../lib/Mailer/NewUserMailHelper.php',
'OCA\\Settings\\Middleware\\SubadminMiddleware' => $baseDir . '/../lib/Middleware/SubadminMiddleware.php',
'OCA\\Settings\\Search\\AppSearch' => $baseDir . '/../lib/Search/AppSearch.php',
diff --git a/apps/settings/composer/composer/autoload_static.php b/apps/settings/composer/composer/autoload_static.php
index 87635f63586..096e6a2d5b4 100644
--- a/apps/settings/composer/composer/autoload_static.php
+++ b/apps/settings/composer/composer/autoload_static.php
@@ -47,6 +47,7 @@ class ComposerStaticInitSettings
'OCA\\Settings\\Controller\\WebAuthnController' => __DIR__ . '/..' . '/../lib/Controller/WebAuthnController.php',
'OCA\\Settings\\Events\\BeforeTemplateRenderedEvent' => __DIR__ . '/..' . '/../lib/Events/BeforeTemplateRenderedEvent.php',
'OCA\\Settings\\Hooks' => __DIR__ . '/..' . '/../lib/Hooks.php',
+ 'OCA\\Settings\\Listener\\AppPasswordCreatedActivityListener' => __DIR__ . '/..' . '/../lib/Listener/AppPasswordCreatedActivityListener.php',
'OCA\\Settings\\Mailer\\NewUserMailHelper' => __DIR__ . '/..' . '/../lib/Mailer/NewUserMailHelper.php',
'OCA\\Settings\\Middleware\\SubadminMiddleware' => __DIR__ . '/..' . '/../lib/Middleware/SubadminMiddleware.php',
'OCA\\Settings\\Search\\AppSearch' => __DIR__ . '/..' . '/../lib/Search/AppSearch.php',
diff --git a/apps/settings/lib/AppInfo/Application.php b/apps/settings/lib/AppInfo/Application.php
index 8240b310558..7fa3f8f347c 100644
--- a/apps/settings/lib/AppInfo/Application.php
+++ b/apps/settings/lib/AppInfo/Application.php
@@ -35,19 +35,17 @@ declare(strict_types=1);
namespace OCA\Settings\AppInfo;
-use BadMethodCallException;
use OC\AppFramework\Utility\TimeFactory;
+use OC\Authentication\Events\AppPasswordCreatedEvent;
use OC\Authentication\Token\IProvider;
-use OC\Authentication\Token\IToken;
use OC\Group\Manager;
use OC\Server;
-use OCA\Settings\Activity\Provider;
use OCA\Settings\Hooks;
+use OCA\Settings\Listener\AppPasswordCreatedActivityListener;
use OCA\Settings\Mailer\NewUserMailHelper;
use OCA\Settings\Middleware\SubadminMiddleware;
use OCA\Settings\Search\AppSearch;
use OCA\Settings\Search\SectionSearch;
-use OCP\Activity\IManager as IActivityManager;
use OCP\AppFramework\App;
use OCP\AppFramework\Bootstrap\IBootContext;
use OCP\AppFramework\Bootstrap\IBootstrap;
@@ -56,13 +54,10 @@ use OCP\AppFramework\IAppContainer;
use OCP\Defaults;
use OCP\IGroup;
use OCP\IGroupManager;
-use OCP\ILogger;
use OCP\IServerContainer;
use OCP\IUser;
use OCP\Settings\IManager;
use OCP\Util;
-use Symfony\Component\EventDispatcher\EventDispatcherInterface;
-use Symfony\Component\EventDispatcher\GenericEvent;
class Application extends App implements IBootstrap {
public const APP_ID = 'settings';
@@ -81,6 +76,9 @@ class Application extends App implements IBootstrap {
$context->registerSearchProvider(SectionSearch::class);
$context->registerSearchProvider(AppSearch::class);
+ // Register listeners
+ $context->registerEventListener(AppPasswordCreatedEvent::class, AppPasswordCreatedActivityListener::class);
+
/**
* Core class wrappers
*/
@@ -129,31 +127,6 @@ class Application extends App implements IBootstrap {
}
public function boot(IBootContext $context): void {
- $context->injectFn(function (EventDispatcherInterface $dispatcher, IAppContainer $appContainer) {
- $dispatcher->addListener('app_password_created', function (GenericEvent $event) use ($appContainer) {
- if (($token = $event->getSubject()) instanceof IToken) {
- /** @var IActivityManager $activityManager */
- $activityManager = $appContainer->get(IActivityManager::class);
- /** @var ILogger $logger */
- $logger = $appContainer->get(ILogger::class);
-
- $activity = $activityManager->generateEvent();
- $activity->setApp('settings')
- ->setType('security')
- ->setAffectedUser($token->getUID())
- ->setAuthor($token->getUID())
- ->setSubject(Provider::APP_TOKEN_CREATED, ['name' => $token->getName()])
- ->setObject('app_token', $token->getId());
-
- try {
- $activityManager->publish($activity);
- } catch (BadMethodCallException $e) {
- $logger->logException($e, ['message' => 'could not publish activity', 'level' => ILogger::WARN]);
- }
- }
- });
- });
-
Util::connectHook('OC_User', 'post_setPassword', $this, 'onChangePassword');
Util::connectHook('OC_User', 'changeUser', $this, 'onChangeInfo');
diff --git a/apps/settings/lib/Listener/AppPasswordCreatedActivityListener.php b/apps/settings/lib/Listener/AppPasswordCreatedActivityListener.php
new file mode 100644
index 00000000000..83aa67642a7
--- /dev/null
+++ b/apps/settings/lib/Listener/AppPasswordCreatedActivityListener.php
@@ -0,0 +1,73 @@
+<?php
+
+declare(strict_types=1);
+
+/*
+ * @copyright 2021 Christoph Wurst <christoph@winzerhof-wurst.at>
+ *
+ * @author 2021 Christoph Wurst <christoph@winzerhof-wurst.at>
+ *
+ * @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\Settings\Listener;
+
+use BadMethodCallException;
+use OC\Authentication\Events\AppPasswordCreatedEvent;
+use OCA\Settings\Activity\Provider;
+use OCP\Activity\IManager as IActivityManager;
+use OCP\EventDispatcher\Event;
+use OCP\EventDispatcher\IEventListener;
+use Psr\Log\LoggerInterface;
+
+/**
+ * @template-implements IEventListener<\OC\Authentication\Events\AppPasswordCreatedEvent>
+ */
+class AppPasswordCreatedActivityListener implements IEventListener {
+ /** @var IActivityManager */
+ private $activityManager;
+
+ /** @var LoggerInterface */
+ private $logger;
+
+ public function __construct(IActivityManager $activityManager,
+ LoggerInterface $logger) {
+ $this->activityManager = $activityManager;
+ $this->logger = $logger;
+ }
+
+ public function handle(Event $event): void {
+ if (!($event instanceof AppPasswordCreatedEvent)) {
+ return;
+ }
+
+ $activity = $this->activityManager->generateEvent();
+ $activity->setApp('settings')
+ ->setType('security')
+ ->setAffectedUser($event->getToken()->getUID())
+ ->setAuthor($event->getToken()->getUID())
+ ->setSubject(Provider::APP_TOKEN_CREATED, ['name' => $event->getToken()->getName()])
+ ->setObject('app_token', $event->getToken()->getId());
+
+ try {
+ $this->activityManager->publish($activity);
+ } catch (BadMethodCallException $e) {
+ $this->logger->warning('Could not publish activity: ' . $e->getMessage(), [
+ 'exception' => $e
+ ]);
+ }
+ }
+}
diff --git a/core/Controller/AppPasswordController.php b/core/Controller/AppPasswordController.php
index 2f8c1184def..15f86b4ad6a 100644
--- a/core/Controller/AppPasswordController.php
+++ b/core/Controller/AppPasswordController.php
@@ -27,6 +27,7 @@ declare(strict_types=1);
namespace OC\Core\Controller;
+use OC\Authentication\Events\AppPasswordCreatedEvent;
use OC\Authentication\Exceptions\InvalidTokenException;
use OC\Authentication\Token\IProvider;
use OC\Authentication\Token\IToken;
@@ -35,11 +36,10 @@ use OCP\AppFramework\OCS\OCSForbiddenException;
use OCP\Authentication\Exceptions\CredentialsUnavailableException;
use OCP\Authentication\Exceptions\PasswordUnavailableException;
use OCP\Authentication\LoginCredentials\IStore;
+use OCP\EventDispatcher\IEventDispatcher;
use OCP\IRequest;
use OCP\ISession;
use OCP\Security\ISecureRandom;
-use Symfony\Component\EventDispatcher\EventDispatcherInterface;
-use Symfony\Component\EventDispatcher\GenericEvent;
class AppPasswordController extends \OCP\AppFramework\OCSController {
@@ -55,7 +55,7 @@ class AppPasswordController extends \OCP\AppFramework\OCSController {
/** @var IStore */
private $credentialStore;
- /** @var EventDispatcherInterface */
+ /** @var IEventDispatcher */
private $eventDispatcher;
public function __construct(string $appName,
@@ -64,7 +64,7 @@ class AppPasswordController extends \OCP\AppFramework\OCSController {
ISecureRandom $random,
IProvider $tokenProvider,
IStore $credentialStore,
- EventDispatcherInterface $eventDispatcher) {
+ IEventDispatcher $eventDispatcher) {
parent::__construct($appName, $request);
$this->session = $session;
@@ -112,8 +112,9 @@ class AppPasswordController extends \OCP\AppFramework\OCSController {
IToken::DO_NOT_REMEMBER
);
- $event = new GenericEvent($generatedToken);
- $this->eventDispatcher->dispatch('app_password_created', $event);
+ $this->eventDispatcher->dispatchTyped(
+ new AppPasswordCreatedEvent($generatedToken)
+ );
return new DataResponse([
'apppassword' => $token
diff --git a/core/Controller/ClientFlowLoginController.php b/core/Controller/ClientFlowLoginController.php
index 9a7296281b0..b7599acc3c6 100644
--- a/core/Controller/ClientFlowLoginController.php
+++ b/core/Controller/ClientFlowLoginController.php
@@ -32,6 +32,7 @@
namespace OC\Core\Controller;
+use OC\Authentication\Events\AppPasswordCreatedEvent;
use OC\Authentication\Exceptions\InvalidTokenException;
use OC\Authentication\Exceptions\PasswordlessTokenException;
use OC\Authentication\Token\IProvider;
@@ -44,6 +45,7 @@ use OCP\AppFramework\Http;
use OCP\AppFramework\Http\Response;
use OCP\AppFramework\Http\StandaloneTemplateResponse;
use OCP\Defaults;
+use OCP\EventDispatcher\IEventDispatcher;
use OCP\IL10N;
use OCP\IRequest;
use OCP\ISession;
@@ -52,8 +54,6 @@ use OCP\IUserSession;
use OCP\Security\ICrypto;
use OCP\Security\ISecureRandom;
use OCP\Session\Exceptions\SessionNotAvailableException;
-use Symfony\Component\EventDispatcher\EventDispatcherInterface;
-use Symfony\Component\EventDispatcher\GenericEvent;
class ClientFlowLoginController extends Controller {
/** @var IUserSession */
@@ -76,7 +76,7 @@ class ClientFlowLoginController extends Controller {
private $accessTokenMapper;
/** @var ICrypto */
private $crypto;
- /** @var EventDispatcherInterface */
+ /** @var IEventDispatcher */
private $eventDispatcher;
public const STATE_NAME = 'client.flow.state.token';
@@ -94,7 +94,7 @@ class ClientFlowLoginController extends Controller {
* @param ClientMapper $clientMapper
* @param AccessTokenMapper $accessTokenMapper
* @param ICrypto $crypto
- * @param EventDispatcherInterface $eventDispatcher
+ * @param IEventDispatcher $eventDispatcher
*/
public function __construct($appName,
IRequest $request,
@@ -108,7 +108,7 @@ class ClientFlowLoginController extends Controller {
ClientMapper $clientMapper,
AccessTokenMapper $accessTokenMapper,
ICrypto $crypto,
- EventDispatcherInterface $eventDispatcher) {
+ IEventDispatcher $eventDispatcher) {
parent::__construct($appName, $request);
$this->userSession = $userSession;
$this->l10n = $l10n;
@@ -364,8 +364,9 @@ class ClientFlowLoginController extends Controller {
$this->tokenProvider->invalidateToken($sessionId);
}
- $event = new GenericEvent($generatedToken);
- $this->eventDispatcher->dispatch('app_password_created', $event);
+ $this->eventDispatcher->dispatchTyped(
+ new AppPasswordCreatedEvent($generatedToken)
+ );
return new Http\RedirectResponse($redirectUri);
}
diff --git a/lib/composer/composer/autoload_classmap.php b/lib/composer/composer/autoload_classmap.php
index bdbdbcf00d1..621aa598b61 100644
--- a/lib/composer/composer/autoload_classmap.php
+++ b/lib/composer/composer/autoload_classmap.php
@@ -662,6 +662,7 @@ return array(
'OC\\Archive\\TAR' => $baseDir . '/lib/private/Archive/TAR.php',
'OC\\Archive\\ZIP' => $baseDir . '/lib/private/Archive/ZIP.php',
'OC\\Authentication\\Events\\ARemoteWipeEvent' => $baseDir . '/lib/private/Authentication/Events/ARemoteWipeEvent.php',
+ 'OC\\Authentication\\Events\\AppPasswordCreatedEvent' => $baseDir . '/lib/private/Authentication/Events/AppPasswordCreatedEvent.php',
'OC\\Authentication\\Events\\LoginFailed' => $baseDir . '/lib/private/Authentication/Events/LoginFailed.php',
'OC\\Authentication\\Events\\RemoteWipeFinished' => $baseDir . '/lib/private/Authentication/Events/RemoteWipeFinished.php',
'OC\\Authentication\\Events\\RemoteWipeStarted' => $baseDir . '/lib/private/Authentication/Events/RemoteWipeStarted.php',
diff --git a/lib/composer/composer/autoload_static.php b/lib/composer/composer/autoload_static.php
index d39ac46d153..68df3a21442 100644
--- a/lib/composer/composer/autoload_static.php
+++ b/lib/composer/composer/autoload_static.php
@@ -691,6 +691,7 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c
'OC\\Archive\\TAR' => __DIR__ . '/../../..' . '/lib/private/Archive/TAR.php',
'OC\\Archive\\ZIP' => __DIR__ . '/../../..' . '/lib/private/Archive/ZIP.php',
'OC\\Authentication\\Events\\ARemoteWipeEvent' => __DIR__ . '/../../..' . '/lib/private/Authentication/Events/ARemoteWipeEvent.php',
+ 'OC\\Authentication\\Events\\AppPasswordCreatedEvent' => __DIR__ . '/../../..' . '/lib/private/Authentication/Events/AppPasswordCreatedEvent.php',
'OC\\Authentication\\Events\\LoginFailed' => __DIR__ . '/../../..' . '/lib/private/Authentication/Events/LoginFailed.php',
'OC\\Authentication\\Events\\RemoteWipeFinished' => __DIR__ . '/../../..' . '/lib/private/Authentication/Events/RemoteWipeFinished.php',
'OC\\Authentication\\Events\\RemoteWipeStarted' => __DIR__ . '/../../..' . '/lib/private/Authentication/Events/RemoteWipeStarted.php',
diff --git a/lib/private/Authentication/Events/AppPasswordCreatedEvent.php b/lib/private/Authentication/Events/AppPasswordCreatedEvent.php
new file mode 100644
index 00000000000..6daa504527e
--- /dev/null
+++ b/lib/private/Authentication/Events/AppPasswordCreatedEvent.php
@@ -0,0 +1,43 @@
+<?php
+
+declare(strict_types=1);
+
+/*
+ * @copyright 2021 Christoph Wurst <christoph@winzerhof-wurst.at>
+ *
+ * @author 2021 Christoph Wurst <christoph@winzerhof-wurst.at>
+ *
+ * @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 OC\Authentication\Events;
+
+use OC\Authentication\Token\IToken;
+use OCP\EventDispatcher\Event;
+
+class AppPasswordCreatedEvent extends Event {
+ /** @var IToken */
+ private $token;
+
+ public function __construct(IToken $token) {
+ parent::__construct();
+ $this->token = $token;
+ }
+
+ public function getToken(): IToken {
+ return $this->token;
+ }
+}