Skip to content

Commit

Permalink
Merge pull request #4585 from pKallert/fix/LogoutFlashMessage
Browse files Browse the repository at this point in the history
FEATURE: Refactor login controller and improve translation of flash messages
  • Loading branch information
markusguenther authored Oct 12, 2023
2 parents a036264 + 2002bd9 commit 45f90a2
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 48 deletions.
64 changes: 23 additions & 41 deletions Neos.Neos/Classes/Controller/LoginController.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
use Neos\Flow\Session\SessionInterface;
use Neos\Flow\Session\SessionManagerInterface;
use Neos\Fusion\View\FusionView;
use Neos\Neos\Controller\Module\ModuleTranslationTrait;
use Neos\Neos\Controller\TranslationTrait;
use Neos\Neos\Domain\Repository\DomainRepository;
use Neos\Neos\Domain\Repository\SiteRepository;
use Neos\Neos\Service\BackendRedirectionService;
Expand All @@ -41,42 +41,30 @@
*/
class LoginController extends AbstractAuthenticationController
{
use ModuleTranslationTrait;
use TranslationTrait;

/**
* @var string
*/
protected $defaultViewObjectName = FusionView::class;

/**
* @Flow\Inject
* @var SessionInterface
*/
protected $session;
#[Flow\Inject]
protected SessionInterface $session;

/**
* @Flow\Inject
* @var SessionManagerInterface
*/
protected $sessionManager;
#[Flow\Inject]
protected SessionManagerInterface $sessionManager;

/**
* @Flow\Inject
* @var BackendRedirectionService
*/
protected $backendRedirectionService;
#[Flow\Inject]
protected BackendRedirectionService $backendRedirectionService;

/**
* @Flow\Inject
* @var DomainRepository
*/
protected $domainRepository;
#[Flow\Inject]
protected DomainRepository $domainRepository;

/**
* @Flow\Inject
* @var SiteRepository
*/
protected $siteRepository;
#[Flow\Inject]
protected SiteRepository $siteRepository;

#[Flow\Inject]
protected FlashMessageService $flashMessageService;

/**
* @Flow\Inject
Expand All @@ -90,12 +78,6 @@ class LoginController extends AbstractAuthenticationController
*/
protected $sessionName;

/**
* @Flow\Inject
* @var FlashMessageService
*/
protected $flashMessageService;

/**
* @var array<string,class-string>
*/
Expand Down Expand Up @@ -211,14 +193,14 @@ public function tokenLoginAction(string $token): void
* @param AuthenticationRequiredException $exception The exception thrown while the authentication process
* @return void
*/
protected function onAuthenticationFailure(AuthenticationRequiredException $exception = null)
protected function onAuthenticationFailure(AuthenticationRequiredException $exception = null): void
{
if ($this->view instanceof JsonView) {
$this->view->assign('value', ['success' => false]);
} else {
$this->addFlashMessage(
$this->getModuleLabel('login.wrongCredentials.body'),
$this->getModuleLabel('login.wrongCredentials.title'),
$this->getLabel('login.wrongCredentials.body'),
$this->getLabel('login.wrongCredentials.title'),
Message::SEVERITY_ERROR,
[],
$exception === null ? 1347016771 : $exception->getCode()
Expand All @@ -237,7 +219,7 @@ protected function onAuthenticationFailure(AuthenticationRequiredException $exce
* @throws StopActionException
* @throws \Neos\Flow\Mvc\Exception\NoSuchArgumentException
*/
protected function onAuthenticationSuccess(ActionRequest $originalRequest = null)
protected function onAuthenticationSuccess(ActionRequest $originalRequest = null): void
{
if ($this->view instanceof JsonView) {
$this->view->assign(
Expand Down Expand Up @@ -272,7 +254,7 @@ protected function onAuthenticationSuccess(ActionRequest $originalRequest = null
*
* @return void
*/
public function logoutAction()
public function logoutAction(): void
{
$possibleRedirectionUri = $this->backendRedirectionService->getAfterLogoutRedirectionUri($this->request);
parent::logoutAction();
Expand All @@ -285,8 +267,8 @@ public function logoutAction()
$this->redirectToUri($possibleRedirectionUri);
}
$this->addFlashMessage(
$this->getModuleLabel('login.loggedOut.body'),
$this->getModuleLabel('login.loggedOut.title'),
$this->getLabel('login.loggedOut.body'),
$this->getLabel('login.loggedOut.title'),
Message::SEVERITY_NOTICE,
[],
1318421560
Expand All @@ -302,7 +284,7 @@ public function logoutAction()
* @phpstan-ignore-next-line Flow does not properly declare its types here
* @return false
*/
protected function getErrorFlashMessage()
protected function getErrorFlashMessage(): bool
{
return false;
}
Expand Down
42 changes: 42 additions & 0 deletions Neos.Neos/Classes/Controller/TranslationTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

/*
* This file is part of the Neos.Neos package.
*
* (c) Contributors of the Neos Project - www.neos.io
*
* This package is Open Source Software. For the full copyright and license
* information, please view the LICENSE file which was distributed with this
* source code.
*/

declare(strict_types=1);

namespace Neos\Neos\Controller;

use Neos\Flow\Annotations as Flow;
use Neos\Flow\I18n\Translator;

/**
* A trait to do easy backend module translations
*/
trait TranslationTrait
{
#[Flow\Inject]
protected Translator $translator;

/**
* @param array<int|string,mixed> $arguments
*/
public function getLabel(string $id, array $arguments = []): string
{
return $this->translator->translateById(
$id,
$arguments,
null,
null,
'Main',
'Neos.Neos'
) ?: $id;
}
}
4 changes: 2 additions & 2 deletions Neos.Neos/Resources/Private/Fusion/Backend/Views/Login.fusion
Original file line number Diff line number Diff line change
Expand Up @@ -152,12 +152,12 @@ prototype(Neos.Neos:Component.Login.Form) < prototype(Neos.Fusion:Component) {
</script>
<div class="neos-tooltip-arrow"></div>
<div class="neos-tooltip-inner" role="alert">
{I18n.id('flashMessage.' + flashMessage.code).value(flashMessage).package('Neos.Neos').source('Main')}
{flashMessage.message}
</div>
</div>
</Neos.Fusion:Loop>
</div>
</fieldset>
</Neos.Fusion.Form:Form>
`
}
}
5 changes: 0 additions & 5 deletions Neos.Neos/Resources/Private/Translations/en/Main.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -729,11 +729,6 @@
<source>Node Tree loading error.</source>
</trans-unit>

<!-- FlashMessages -->
<trans-unit id="flashMessage.1222204027" xml:space="preserve">
<source>The entered username or password was wrong</source>
</trans-unit>

<!-- Miscellaneous -->
<trans-unit id="masterPlugins.nodeTypeOnPageLabel" xml:space="preserve">
<source>"{nodeTypeName}" on page "{pageLabel}"</source>
Expand Down

0 comments on commit 45f90a2

Please sign in to comment.