Skip to content

Commit

Permalink
Task/felogin adjustments (TYPO3-Headless#481)
Browse files Browse the repository at this point in the history
* [TASK] Add felogin adjustments, fix recovery url, add login data to initial data, fix fieeld name
  • Loading branch information
lukaszuznanski authored Sep 6, 2022
1 parent 2147ca8 commit 0cb1e1d
Show file tree
Hide file tree
Showing 11 changed files with 170 additions and 29 deletions.
5 changes: 2 additions & 3 deletions Classes/ViewHelpers/LoginFormViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -144,7 +144,6 @@ protected function setFormActionUri()
);
$this->formActionUriArguments = $uriBuilder->getArguments();
}
$this->data['action'] = $formActionUri;
}

/**
Expand Down Expand Up @@ -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;
}
}
111 changes: 111 additions & 0 deletions Classes/XClass/Controller/LoginController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?php

/*
* This file is part of the "headless" Extension for TYPO3 CMS.
*
* For the full copyright and license information, please read the
* LICENSE.md file that was distributed with this source code.
*/

declare(strict_types=1);

namespace FriendsOfTYPO3\Headless\XClass\Controller;

use Psr\Http\Message\ResponseInterface;
use TYPO3\CMS\Extbase\Http\ForwardResponse;
use TYPO3\CMS\FrontendLogin\Event\BeforeRedirectEvent;
use TYPO3\CMS\FrontendLogin\Event\LoginConfirmedEvent;
use TYPO3\CMS\FrontendLogin\Event\LoginErrorOccurredEvent;
use TYPO3\CMS\FrontendLogin\Event\LogoutConfirmedEvent;
use TYPO3\CMS\FrontendLogin\Event\ModifyLoginFormViewEvent;

/**
* @codeCoverageIgnore
*/
class LoginController extends \TYPO3\CMS\FrontendLogin\Controller\LoginController
{
/**
* Show login form
*/
public function loginAction(): ResponseInterface
{
$status = 'success';

if ($this->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();
}
}
6 changes: 6 additions & 0 deletions Configuration/Services.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
12 changes: 12 additions & 0 deletions Configuration/TypoScript/Configuration/FeLogin.typoscript
Original file line number Diff line number Diff line change
Expand Up @@ -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]
23 changes: 5 additions & 18 deletions Resources/Private/FeLogin/Partials/LoginFields.html
Original file line number Diff line number Diff line change
Expand Up @@ -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()}'
}"
/>,
<f:format.json value="{
type: 'password',
label: '{f:translate(key:\'input.password\', extensionName:\'headless\')}',
name: 'pass',
validate: {
required: 1,
password: 1
},
options: {
attr: {
placeholder: '{f:translate(key:\'input.password.placeholder\', extensionName:\'headless\')}'
}
}
placeholder: '{f:translate(key:\'input.password.placeholder\', extensionName:\'headless\')}',
validators: '{f:render(partial: \'Validators\') -> headless:format.json.decode()}'
}"
/>,
<f:if condition="{permaloginStatus} > -1">
Expand Down
10 changes: 10 additions & 0 deletions Resources/Private/FeLogin/Partials/Validators.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<f:spaceless>
[
<f:format.raw>
<f:format.json value="{
identifier: 'required',
message: 'This field is mandatory.'
}"/>
</f:format.raw>
]
</f:spaceless>
5 changes: 3 additions & 2 deletions Resources/Private/FeLogin/Templates/Login/Login.html
Original file line number Diff line number Diff line change
Expand Up @@ -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()}'
}"/>
</f:format.raw>
Expand Down
2 changes: 1 addition & 1 deletion Resources/Private/FeLogin/Templates/Login/Logout.html
Original file line number Diff line number Diff line change
Expand Up @@ -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\\\'}\')}',
Expand Down
9 changes: 9 additions & 0 deletions Resources/Private/FeLogin/Templates/Login/Overview.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<f:spaceless>
<f:format.raw>
<f:format.json value="{
status: '{status}',
login: '{user.username}',
message: '{f:translate(key:\'success_header\')}'
}"/>
</f:format.raw>
</f:spaceless>
Original file line number Diff line number Diff line change
Expand Up @@ -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()}'
}"/>
Expand All @@ -24,7 +24,7 @@
<f:spaceless>
<f:format.raw>
[
<f:format.json value="{
<f:format.json value="{
type: 'text',
label: '{f:translate(key:\'input.username.recovery\', extensionName:\'headless\')}',
name: 'userIdentifier',
Expand All @@ -39,13 +39,13 @@
}
}
}"
/>,
<f:format.json value="{
/>,
<f:format.json value="{
type: 'submit',
name: 'submit',
value: '{f:translate(key:\'input.submit.recovery\', extensionName:\'headless\')}'
}"
/>
/>
]
</f:format.raw>
</f:spaceless>
Expand Down
6 changes: 6 additions & 0 deletions ext_localconf.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 0cb1e1d

Please sign in to comment.