diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php index 844582461ea35a1204d800b62f3e8215d86d8dde..e9267b0051b83332d5cc1caf910ebd8a5d57806f 100644 --- a/lib/AppInfo/Application.php +++ b/lib/AppInfo/Application.php @@ -37,6 +37,8 @@ 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'; @@ -49,6 +51,7 @@ class Application extends App implements IBootstrap { $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 0000000000000000000000000000000000000000..513705c384c1e1e95140358e1a55e12cea62680d --- /dev/null +++ b/lib/Listeners/BeforeTemplateRenderedListener.php @@ -0,0 +1,77 @@ +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; + } + $accountId = $this->getAccountId(); + $actions = \RainLoop\Api::Actions(); + + if (empty($accountId) || $actions->getMainAccountFromToken(false)) { + return; + } + + // Just send over '1' as password to trigger login as the plugin will set the correct access token + + $account = $actions->LoginProcess($accountId, self::SNAPPYMAIL_AUTOLOGIN_PWD, false); + if ($account) { + $actions->Plugins()->RunHook('login.success', array($account)); + $actions->SetAuthToken($account); + } + } + + private function getAccountId() : 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', ''); + } + } +}