From ba60fafb9a16f49e5d2a9b5e1970408744fe1e4a Mon Sep 17 00:00:00 2001 From: Roeland Jago Douma Date: Sat, 27 Jul 2019 12:56:27 +0200 Subject: [PATCH] Add proper PostLoginEvent This can be used by othr mechanisms to listen for this event in a lazy fashion. Signed-off-by: Roeland Jago Douma --- lib/composer/composer/autoload_classmap.php | 1 + lib/composer/composer/autoload_static.php | 1 + lib/private/Server.php | 4 +- lib/private/User/Events/PostLoginEvent.php | 63 +++++++++++++++++++++ lib/private/User/Session.php | 15 ++++- 5 files changed, 82 insertions(+), 2 deletions(-) create mode 100644 lib/private/User/Events/PostLoginEvent.php diff --git a/lib/composer/composer/autoload_classmap.php b/lib/composer/composer/autoload_classmap.php index 6ff7ca9de27..7d1d5b50c0a 100644 --- a/lib/composer/composer/autoload_classmap.php +++ b/lib/composer/composer/autoload_classmap.php @@ -1212,6 +1212,7 @@ return array( 'OC\\Updater\\VersionCheck' => $baseDir . '/lib/private/Updater/VersionCheck.php', 'OC\\User\\Backend' => $baseDir . '/lib/private/User/Backend.php', 'OC\\User\\Database' => $baseDir . '/lib/private/User/Database.php', + 'OC\\User\\Events\\PostLoginEvent' => $baseDir . '/lib/private/User/Events/PostLoginEvent.php', 'OC\\User\\LoginException' => $baseDir . '/lib/private/User/LoginException.php', 'OC\\User\\Manager' => $baseDir . '/lib/private/User/Manager.php', 'OC\\User\\NoUserException' => $baseDir . '/lib/private/User/NoUserException.php', diff --git a/lib/composer/composer/autoload_static.php b/lib/composer/composer/autoload_static.php index 9ca4f5e0d9a..f9dd644a2c6 100644 --- a/lib/composer/composer/autoload_static.php +++ b/lib/composer/composer/autoload_static.php @@ -1246,6 +1246,7 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c 'OC\\Updater\\VersionCheck' => __DIR__ . '/../../..' . '/lib/private/Updater/VersionCheck.php', 'OC\\User\\Backend' => __DIR__ . '/../../..' . '/lib/private/User/Backend.php', 'OC\\User\\Database' => __DIR__ . '/../../..' . '/lib/private/User/Database.php', + 'OC\\User\\Events\\PostLoginEvent' => __DIR__ . '/../../..' . '/lib/private/User/Events/PostLoginEvent.php', 'OC\\User\\LoginException' => __DIR__ . '/../../..' . '/lib/private/User/LoginException.php', 'OC\\User\\Manager' => __DIR__ . '/../../..' . '/lib/private/User/Manager.php', 'OC\\User\\NoUserException' => __DIR__ . '/../../..' . '/lib/private/User/NoUserException.php', diff --git a/lib/private/Server.php b/lib/private/Server.php index 64180f1cd7c..c25808a9ccc 100644 --- a/lib/private/Server.php +++ b/lib/private/Server.php @@ -135,6 +135,7 @@ use OCP\Contacts\ContactsMenu\IActionFactory; use OCP\Contacts\ContactsMenu\IContactsStore; use OCP\Dashboard\IDashboardManager; use OCP\Defaults; +use OCP\EventDispatcher\IEventDispatcher; use OCP\Federation\ICloudFederationFactory; use OCP\Federation\ICloudFederationProviderManager; use OCP\Federation\ICloudIdManager; @@ -385,7 +386,8 @@ class Server extends ServerContainer implements IServerContainer { $c->getConfig(), $c->getSecureRandom(), $c->getLockdownManager(), - $c->getLogger() + $c->getLogger(), + $c->query(IEventDispatcher::class) ); $userSession->listen('\OC\User', 'preCreateUser', function ($uid, $password) { \OC_Hook::emit('OC_User', 'pre_createUser', array('run' => true, 'uid' => $uid, 'password' => $password)); diff --git a/lib/private/User/Events/PostLoginEvent.php b/lib/private/User/Events/PostLoginEvent.php new file mode 100644 index 00000000000..d14030b5294 --- /dev/null +++ b/lib/private/User/Events/PostLoginEvent.php @@ -0,0 +1,63 @@ + + * + * @author Roeland Jago Douma + * + * @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 . + * + */ + +namespace OC\User\Events; + +use OCP\EventDispatcher\Event; +use OCP\IUser; + +class PostLoginEvent extends Event { + + /** @var IUser */ + private $user; + /** @var string */ + private $password; + /** @var bool */ + private $isTokenLogin; + + + public function __construct(IUser $user, string $password, bool $isTokenLogin) { + parent::__construct(); + + $this->user = $user; + $this->password = $password; + $this->isTokenLogin = $isTokenLogin; + } + + public function getUser(): IUser { + return $this->user; + } + + public function hasPassword(): bool { + return $this->password !== ''; + } + + public function getPassword(): string { + return $this->password; + } + + public function getIsTokenLogin(): bool { + return $this->isTokenLogin; + } +} diff --git a/lib/private/User/Session.php b/lib/private/User/Session.php index 45f9cbc260f..13519d97ef4 100644 --- a/lib/private/User/Session.php +++ b/lib/private/User/Session.php @@ -50,6 +50,7 @@ use OC_User; use OC_Util; use OCA\DAV\Connector\Sabre\Auth; use OCP\AppFramework\Utility\ITimeFactory; +use OCP\EventDispatcher\IEventDispatcher; use OCP\Files\NotPermittedException; use OCP\IConfig; use OCP\ILogger; @@ -113,6 +114,8 @@ class Session implements IUserSession, Emitter { /** @var ILogger */ private $logger; + /** @var IEventDispatcher */ + private $dispatcher; /** * @param Manager $manager @@ -131,7 +134,8 @@ class Session implements IUserSession, Emitter { IConfig $config, ISecureRandom $random, ILockdownManager $lockdownManager, - ILogger $logger) { + ILogger $logger, + IEventDispatcher $dispatcher) { $this->manager = $manager; $this->session = $session; $this->timeFactory = $timeFactory; @@ -140,6 +144,7 @@ class Session implements IUserSession, Emitter { $this->random = $random; $this->lockdownManager = $lockdownManager; $this->logger = $logger; + $this->dispatcher = $dispatcher; } /** @@ -369,6 +374,14 @@ class Session implements IUserSession, Emitter { $this->setToken(null); $firstTimeLogin = $user->updateLastLoginTimestamp(); } + + $postLoginEvent = new OC\User\Events\PostLoginEvent( + $user, + $loginDetails['password'], + $isToken + ); + $this->dispatcher->dispatch(OC\User\Events\PostLoginEvent::class, $postLoginEvent); + $this->manager->emit('\OC\User', 'postLogin', [ $user, $loginDetails['password'], -- 2.39.5