From 8f60f5afbe6137af8c961577be0e542720978bc2 Mon Sep 17 00:00:00 2001 From: Akhil Date: Fri, 16 Dec 2022 13:32:50 +0530 Subject: [PATCH 1/5] Add listener --- lib/AppInfo/Application.php | 6 +- .../BeforeTemplateRenderedListener.php | 76 +++++++++++++++++++ 2 files changed, 79 insertions(+), 3 deletions(-) create mode 100644 lib/Listeners/BeforeTemplateRenderedListener.php diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php index 84458246..888602f2 100644 --- a/lib/AppInfo/Application.php +++ b/lib/AppInfo/Application.php @@ -35,8 +35,8 @@ use OCA\EcloudAccounts\Service\LDAPConnectionService; use OCP\User\Events\BeforeUserDeletedEvent; use OCP\User\Events\UserChangedEvent; use OCA\EcloudAccounts\Listeners\UserChangedListener; -use OCA\EcloudAccounts\Listeners\UserAddedToBetaGroupListener; -use OCP\Group\Events\UserAddedEvent; +use OCP\AppFramework\Http\Events\BeforeTemplateRenderedEvent; +use OCA\EcloudAccounts\Listeners\BeforeTemplateRenderedListener; class Application extends App implements IBootstrap { public const APP_ID = 'ecloud-accounts'; @@ -48,7 +48,7 @@ class Application extends App implements IBootstrap { public function register(IRegistrationContext $context): void { $context->registerEventListener(BeforeUserDeletedEvent::class, BeforeUserDeletedListener::class); $context->registerEventListener(UserChangedEvent::class, UserChangedListener::class); - $context->registerEventListener(UserAddedEvent::class, UserAddedToBetaGroupListener::class); + $context->registerEventListener(BeforeTemplateRenderedEvent::class, BeforeTemplateRenderedListener::class); } public function boot(IBootContext $context): void { diff --git a/lib/Listeners/BeforeTemplateRenderedListener.php b/lib/Listeners/BeforeTemplateRenderedListener.php new file mode 100644 index 00000000..8b751ed5 --- /dev/null +++ b/lib/Listeners/BeforeTemplateRenderedListener.php @@ -0,0 +1,76 @@ +appName = $appName; + $this->userSession = $userSession; + $this->request = $request; + $this->session = $session; + $this->config = $config; + $this->appManager = $appManager; + } + + public function handle(Event $event): void { + if (!($event instanceof BeforeTemplateRenderedEvent)) { + return; + } + if ($this->userSession->isLoggedIn() && $this->appManager->isEnabledForUser(self::SNAPPYMAIL_APP_ID) && strpos($this->request->getPathInfo(), self::SNAPPYMAIL_URL) !== false) { + $this->autoLoginWebmail(); + } + } + + + private function autoLoginWebmail() { + $isOidcLogin = $this->session->get('is_oidc'); + if (!$isOidcLogin) { + return; + } + $email = $this->getEmail(); + $actions = \RainLoop\Api::Actions(); + + if (empty($email) || $actions->getMainAccountFromToken(false)) { + return; + } + + $password = $this->session->get('oidc_access_token'); + + $account = $actions->LoginProcess($email, $password, false); + if ($account) { + $actions->Plugins()->RunHook('login.success', array($account)); + $actions->SetAuthToken($account); + } + } + + private function getEmail() : string { + $username = $this->userSession->getUser()->getUID(); + if ($this->config->getAppValue('snappymail', 'snappymail-autologin', false)) { + return $username; + } + if ($this->config->getAppValue('snappymail', 'snappymail-autologin-with-email', false)) { + return $this->config->getUserValue($username, 'settings', 'email', ''); + } + } +} -- GitLab From fc86b09338486d963fec4b522054060025e98b39 Mon Sep 17 00:00:00 2001 From: Akhil Date: Fri, 16 Dec 2022 13:57:14 +0530 Subject: [PATCH 2/5] Return if empty pwd --- lib/Listeners/BeforeTemplateRenderedListener.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/Listeners/BeforeTemplateRenderedListener.php b/lib/Listeners/BeforeTemplateRenderedListener.php index 8b751ed5..1c0786ea 100644 --- a/lib/Listeners/BeforeTemplateRenderedListener.php +++ b/lib/Listeners/BeforeTemplateRenderedListener.php @@ -56,6 +56,9 @@ class BeforeTemplateRenderedListener implements IEventListener { } $password = $this->session->get('oidc_access_token'); + if(empty($password)) { + return; + } $account = $actions->LoginProcess($email, $password, false); if ($account) { -- GitLab From 2dddd96098418ad5acd9488e1a22fe40d17546ff Mon Sep 17 00:00:00 2001 From: Akhil Date: Fri, 16 Dec 2022 14:08:40 +0530 Subject: [PATCH 3/5] send pwd as 1 --- lib/Listeners/BeforeTemplateRenderedListener.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/lib/Listeners/BeforeTemplateRenderedListener.php b/lib/Listeners/BeforeTemplateRenderedListener.php index 1c0786ea..d0027a98 100644 --- a/lib/Listeners/BeforeTemplateRenderedListener.php +++ b/lib/Listeners/BeforeTemplateRenderedListener.php @@ -55,10 +55,8 @@ class BeforeTemplateRenderedListener implements IEventListener { return; } - $password = $this->session->get('oidc_access_token'); - if(empty($password)) { - return; - } + // Just send over '1' as the plugin must set the correct one + $password = strval($isOidcLogin); $account = $actions->LoginProcess($email, $password, false); if ($account) { -- GitLab From 62cbd27a7a8eea7f54335783102b001c6b9c1ffc Mon Sep 17 00:00:00 2001 From: Nivesh Krishna Date: Fri, 16 Dec 2022 15:30:37 +0530 Subject: [PATCH 4/5] restore beta group event listener --- lib/AppInfo/Application.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php index 888602f2..e9267b00 100644 --- a/lib/AppInfo/Application.php +++ b/lib/AppInfo/Application.php @@ -35,6 +35,8 @@ use OCA\EcloudAccounts\Service\LDAPConnectionService; use OCP\User\Events\BeforeUserDeletedEvent; use OCP\User\Events\UserChangedEvent; use OCA\EcloudAccounts\Listeners\UserChangedListener; +use OCA\EcloudAccounts\Listeners\UserAddedToBetaGroupListener; +use OCP\Group\Events\UserAddedEvent; use OCP\AppFramework\Http\Events\BeforeTemplateRenderedEvent; use OCA\EcloudAccounts\Listeners\BeforeTemplateRenderedListener; @@ -48,6 +50,7 @@ class Application extends App implements IBootstrap { public function register(IRegistrationContext $context): void { $context->registerEventListener(BeforeUserDeletedEvent::class, BeforeUserDeletedListener::class); $context->registerEventListener(UserChangedEvent::class, UserChangedListener::class); + $context->registerEventListener(UserAddedEvent::class, UserAddedToBetaGroupListener::class); $context->registerEventListener(BeforeTemplateRenderedEvent::class, BeforeTemplateRenderedListener::class); } -- GitLab From 27a07b7ed1002d54b2fd5851d542d7af5e8eaf61 Mon Sep 17 00:00:00 2001 From: Akhil Date: Tue, 10 Jan 2023 17:42:24 +0530 Subject: [PATCH 5/5] fix variables and cconstants --- lib/Listeners/BeforeTemplateRenderedListener.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/Listeners/BeforeTemplateRenderedListener.php b/lib/Listeners/BeforeTemplateRenderedListener.php index d0027a98..513705c3 100644 --- a/lib/Listeners/BeforeTemplateRenderedListener.php +++ b/lib/Listeners/BeforeTemplateRenderedListener.php @@ -23,6 +23,7 @@ class BeforeTemplateRenderedListener implements IEventListener { private const SNAPPYMAIL_APP_ID = 'snappymail'; private const SNAPPYMAIL_URL = '/apps/snappymail/'; + private const SNAPPYMAIL_AUTOLOGIN_PWD = '1'; public function __construct($appName, IUserSession $userSession, IRequest $request, ISession $session, IConfig $config, IAppManager $appManager) { $this->appName = $appName; @@ -48,24 +49,23 @@ class BeforeTemplateRenderedListener implements IEventListener { if (!$isOidcLogin) { return; } - $email = $this->getEmail(); + $accountId = $this->getAccountId(); $actions = \RainLoop\Api::Actions(); - if (empty($email) || $actions->getMainAccountFromToken(false)) { + if (empty($accountId) || $actions->getMainAccountFromToken(false)) { return; } - // Just send over '1' as the plugin must set the correct one - $password = strval($isOidcLogin); + // Just send over '1' as password to trigger login as the plugin will set the correct access token - $account = $actions->LoginProcess($email, $password, false); + $account = $actions->LoginProcess($accountId, self::SNAPPYMAIL_AUTOLOGIN_PWD, false); if ($account) { $actions->Plugins()->RunHook('login.success', array($account)); $actions->SetAuthToken($account); } } - private function getEmail() : string { + private function getAccountId() : string { $username = $this->userSession->getUser()->getUID(); if ($this->config->getAppValue('snappymail', 'snappymail-autologin', false)) { return $username; -- GitLab