From 0cb1e1d068f27013ccb34e0cf3bfcd68ef5b8f41 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Uzna=C5=84ski?= Date: Tue, 6 Sep 2022 16:52:59 +0200 Subject: [PATCH] Task/felogin adjustments (#481) * [TASK] Add felogin adjustments, fix recovery url, add login data to initial data, fix fieeld name --- Classes/ViewHelpers/LoginFormViewHelper.php | 5 +- Classes/XClass/Controller/LoginController.php | 111 ++++++++++++++++++ Configuration/Services.php | 6 + .../Configuration/FeLogin.typoscript | 12 ++ .../Private/FeLogin/Partials/LoginFields.html | 23 +--- .../Private/FeLogin/Partials/Validators.html | 10 ++ .../FeLogin/Templates/Login/Login.html | 5 +- .../FeLogin/Templates/Login/Logout.html | 2 +- .../FeLogin/Templates/Login/Overview.html | 9 ++ .../Templates/PasswordRecovery/Recovery.html | 10 +- ext_localconf.php | 6 + 11 files changed, 170 insertions(+), 29 deletions(-) create mode 100644 Classes/XClass/Controller/LoginController.php create mode 100644 Resources/Private/FeLogin/Partials/Validators.html create mode 100644 Resources/Private/FeLogin/Templates/Login/Overview.html diff --git a/Classes/ViewHelpers/LoginFormViewHelper.php b/Classes/ViewHelpers/LoginFormViewHelper.php index a6ae4d60..a4417fa0 100644 --- a/Classes/ViewHelpers/LoginFormViewHelper.php +++ b/Classes/ViewHelpers/LoginFormViewHelper.php @@ -80,7 +80,7 @@ public function render() $this->addFormObjectToViewHelperVariableContainer(); $this->addFieldNamePrefixToViewHelperVariableContainer(); $this->addFormFieldNamesToViewHelperVariableContainer(); - $this->data['pages'] = $this->renderChildren(); + $this->data = $this->renderChildren(); $this->renderHiddenIdentityField($this->arguments['object'] ?? null, $this->getFormObjectName()); $this->renderAdditionalIdentityFields(); @@ -144,7 +144,6 @@ protected function setFormActionUri() ); $this->formActionUriArguments = $uriBuilder->getArguments(); } - $this->data['action'] = $formActionUri; } /** @@ -268,6 +267,6 @@ protected function addHiddenField($name, $value) $tmp['name'] = $name; $tmp['type'] = 'hidden'; $tmp['value'] = $value; - $this->data['pages'][] = $tmp; + $this->data[] = $tmp; } } diff --git a/Classes/XClass/Controller/LoginController.php b/Classes/XClass/Controller/LoginController.php new file mode 100644 index 00000000..ecdc0552 --- /dev/null +++ b/Classes/XClass/Controller/LoginController.php @@ -0,0 +1,111 @@ +isLogoutSuccessful()) { + $this->eventDispatcher->dispatch(new LogoutConfirmedEvent($this, $this->view)); + } elseif ($this->hasLoginErrorOccurred()) { + $status = 'failure'; + $this->eventDispatcher->dispatch(new LoginErrorOccurredEvent()); + } + + if (($forwardResponse = $this->handleLoginForwards()) !== null) { + return $forwardResponse; + } + + if ($this->redirectUrl !== '') { + $this->eventDispatcher->dispatch(new BeforeRedirectEvent($this->loginType, $this->redirectUrl)); + $data = [ + 'redirectUrl' => $this->redirectUrl, + 'statusCode' => 301 + ]; + return $this->responseFactory->createResponse()->withHeader('Content-Type', 'application/json; charset=utf-8') + ->withBody($this->streamFactory->createStream(json_encode($data))); + } + $this->eventDispatcher->dispatch(new ModifyLoginFormViewEvent($this->view)); + + $this->view->assignMultiple( + [ + 'status' => $status, + 'cookieWarning' => $this->showCookieWarning, + 'messageKey' => $this->getStatusMessageKey(), + 'storagePid' => implode(',', $this->getStorageFolders()), + 'permaloginStatus' => $this->getPermaloginStatus(), + 'redirectURL' => $this->redirectHandler->getLoginFormRedirectUrl($this->configuration, $this->isRedirectDisabled()), + 'redirectReferrer' => $this->request->hasArgument('redirectReferrer') ? (string)$this->request->getArgument('redirectReferrer') : '', + 'referer' => $this->requestHandler->getPropertyFromGetAndPost('referer'), + 'noRedirect' => $this->isRedirectDisabled(), + ] + ); + + return $this->jsonResponse(); + } + + /** + * User overview for logged in users + * + * @param bool $showLoginMessage + * @return ResponseInterface + */ + public function overviewAction(bool $showLoginMessage = false): ResponseInterface + { + $status = 'success'; + + if (!$this->userAspect->isLoggedIn()) { + return new ForwardResponse('login'); + } + + $this->eventDispatcher->dispatch(new LoginConfirmedEvent($this, $this->view)); + + if ($this->redirectUrl !== '') { + $this->eventDispatcher->dispatch(new BeforeRedirectEvent($this->loginType, $this->redirectUrl)); + $data = [ + 'redirectUrl' => $this->redirectUrl, + 'statusCode' => 301, + 'status' => 'success' + ]; + return $this->responseFactory->createResponse()->withHeader('Content-Type', 'application/json; charset=utf-8') + ->withBody($this->streamFactory->createStream(json_encode($data))); + } + + $this->view->assignMultiple( + [ + 'status' => $status, + 'cookieWarning' => $this->showCookieWarning, + 'user' => $this->userService->getFeUserData(), + 'showLoginMessage' => $showLoginMessage, + ] + ); + + return $this->htmlResponse(); + } +} diff --git a/Configuration/Services.php b/Configuration/Services.php index 76f60e24..275ab7f2 100644 --- a/Configuration/Services.php +++ b/Configuration/Services.php @@ -32,6 +32,12 @@ ]); } + if (!class_exists(\TYPO3\CMS\FrontendLogin\Controller\LoginController::class, false)) { + $excludes = array_merge($excludes, [ + '../Classes/XClass/Controller/LoginController.php', + ]); + } + $toLoad->exclude($excludes); $services->set(HeadlessFrontendUrlInterface::class, UrlUtility::class)->autowire(false); diff --git a/Configuration/TypoScript/Configuration/FeLogin.typoscript b/Configuration/TypoScript/Configuration/FeLogin.typoscript index 6b785af4..4a8601f6 100644 --- a/Configuration/TypoScript/Configuration/FeLogin.typoscript +++ b/Configuration/TypoScript/Configuration/FeLogin.typoscript @@ -5,3 +5,15 @@ plugin.tx_felogin_login { layoutRootPaths.10 = EXT:headless//Resources/Private/FeLogin/Layouts/ } } + +initialData.10.fields.user.fields { + logged = BOOL + logged.value = 0 +} + +[frontend.user.isLoggedIn] + initialData.10.fields.user.fields { + logged = BOOL + logged.value = 1 + } +[global] diff --git a/Resources/Private/FeLogin/Partials/LoginFields.html b/Resources/Private/FeLogin/Partials/LoginFields.html index be85573d..29bb025d 100644 --- a/Resources/Private/FeLogin/Partials/LoginFields.html +++ b/Resources/Private/FeLogin/Partials/LoginFields.html @@ -9,30 +9,17 @@ type: 'text', label: '{f:translate(key:\'input.username\')}', name: 'user', - validate: { - required: 1, - email: 1 - }, - options: { - attr: { - placeholder: '{f:translate(key:\'input.username.placeholder\')}' - } - } + placeholder: '{f:translate(key:\'input.username.placeholder\')}', + validators: '{f:render(partial: \'Validators\') -> headless:format.json.decode()}' + }" />, , diff --git a/Resources/Private/FeLogin/Partials/Validators.html b/Resources/Private/FeLogin/Partials/Validators.html new file mode 100644 index 00000000..c9f70985 --- /dev/null +++ b/Resources/Private/FeLogin/Partials/Validators.html @@ -0,0 +1,10 @@ + + [ + + + +] + diff --git a/Resources/Private/FeLogin/Templates/Login/Login.html b/Resources/Private/FeLogin/Templates/Login/Login.html index e0fdf233..1374cc40 100644 --- a/Resources/Private/FeLogin/Templates/Login/Login.html +++ b/Resources/Private/FeLogin/Templates/Login/Login.html @@ -5,13 +5,14 @@ title: '{f:translate(key:\'form.login.title\')}', action: '{f:uri.action(action:\'login\')}', method: 'POST', - fields: '{f:render(partial: \'LoginFields\', arguments: \'{_all}\') -> headless:format.json.decode()-> headless:loginForm(action:\'login\', name:\'field\') -> headless:format.json.decode()}' + elements: '{f:render(partial: \'LoginFields\', arguments: \'{_all}\') -> headless:format.json.decode()-> headless:loginForm(action:\'login\', name:\'field\') -> headless:format.json.decode()}' }, message: { header: '{f:render(partial: \'RenderLabelOrMessage\', arguments: \'{key: \\\'{messageKey}_header\\\'}\')}', message: '{f:render(partial: \'RenderLabelOrMessage\', arguments: \'{key: \\\'{messageKey}_message\\\'}\')}' }, - recovery: '{f:render(partial: \'Recovery\', arguments: \'{_all}\') -> headless:format.json.decode()}', + status: '{status}', + recovery: '{f:uri.action(action:\'recovery\', controller=\'PasswordRecovery\')}', flashMessages: '{f:render(partial: \'FlashMessages\', arguments: \'{_all}\') -> headless:format.json.decode()}' }"/> diff --git a/Resources/Private/FeLogin/Templates/Login/Logout.html b/Resources/Private/FeLogin/Templates/Login/Logout.html index ef4ce873..04854c3b 100644 --- a/Resources/Private/FeLogin/Templates/Login/Logout.html +++ b/Resources/Private/FeLogin/Templates/Login/Logout.html @@ -5,7 +5,7 @@ title: '{f:translate(key:\'logout\')}', action: '{f:uri.action(action:\'login\')}', method: 'POST', - fields: '{f:render(partial: \'LogoutFields\', arguments: \'{_all}\') -> headless:format.json.decode()-> headless:loginForm(action:\'login\', name:\'field\') -> headless:format.json.decode()}' + elements: '{f:render(partial: \'LogoutFields\', arguments: \'{_all}\') -> headless:format.json.decode()-> headless:loginForm(action:\'login\', name:\'field\') -> headless:format.json.decode()}' }, message: { header: '{f:render(partial: \'RenderLabelOrMessage\', arguments: \'{key: \\\'status_header\\\'}\')}', diff --git a/Resources/Private/FeLogin/Templates/Login/Overview.html b/Resources/Private/FeLogin/Templates/Login/Overview.html new file mode 100644 index 00000000..3b12bca1 --- /dev/null +++ b/Resources/Private/FeLogin/Templates/Login/Overview.html @@ -0,0 +1,9 @@ + + + + + diff --git a/Resources/Private/FeLogin/Templates/PasswordRecovery/Recovery.html b/Resources/Private/FeLogin/Templates/PasswordRecovery/Recovery.html index 68b14c8b..681c1f70 100644 --- a/Resources/Private/FeLogin/Templates/PasswordRecovery/Recovery.html +++ b/Resources/Private/FeLogin/Templates/PasswordRecovery/Recovery.html @@ -14,7 +14,7 @@ title: '{f:render(partial: \'RenderLabelOrMessage\', arguments: \'{key: \\\'reset_password\\\'}\')}', action: '{f:uri.action(action:\'recovery\')}', method: 'POST', - fields: '{f:render(section: \'fields\', arguments: \'{_all}\') -> headless:format.json.decode()}' + elements: '{f:render(section: \'fields\', arguments: \'{_all}\') -> headless:format.json.decode()}' }, flashMessages: '{f:render(partial: \'FlashMessages\', arguments: \'{_all}\') -> headless:format.json.decode()}' }"/> @@ -24,7 +24,7 @@ [ - , - + /> ] diff --git a/ext_localconf.php b/ext_localconf.php index 42cb644d..0add05d8 100644 --- a/ext_localconf.php +++ b/ext_localconf.php @@ -56,6 +56,12 @@ static function () { ]; } + if (\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::isLoaded('felogin')) { + $GLOBALS['TYPO3_CONF_VARS']['SYS']['Objects'][\TYPO3\CMS\FrontendLogin\Controller\LoginController::class] = [ + 'className' => FriendsOfTYPO3\Headless\XClass\Controller\LoginController::class + ]; + } + if (\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::isLoaded('workspaces') && $features->isFeatureEnabled('headless.workspaces')) { $GLOBALS['TYPO3_CONF_VARS']['SYS']['Objects'][\TYPO3\CMS\Workspaces\Controller\PreviewController::class] = [ 'className' => FriendsOfTYPO3\Headless\XClass\Controller\PreviewController::class