‪TYPO3CMS  ‪main
LoginController.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use Psr\Http\Message\ResponseInterface;
36 
43 {
44  public const ‪MESSAGEKEY_DEFAULT = 'welcome';
45  public const ‪MESSAGEKEY_ERROR = 'error';
46  public const ‪MESSAGEKEY_LOGOUT = 'logout';
47 
48  protected string ‪$loginType = '';
49  protected string ‪$redirectUrl = '';
52 
53  public function ‪__construct(
54  protected readonly ‪RedirectHandler $redirectHandler,
55  protected readonly ‪Context $context,
56  protected readonly ‪PageRepository $pageRepository
57  ) {
58  $this->userAspect = $context->getAspect('frontend.user');
59  }
60 
64  public function ‪initializeAction(): void
65  {
66  $this->loginType = (string)($this->request->getParsedBody()['logintype'] ?? $this->request->getQueryParams()['logintype'] ?? '');
67  $this->configuration = ‪RedirectConfiguration::fromSettings($this->settings);
68 
69  if ($this->‪isLoginOrLogoutInProgress() && !$this->‪isRedirectDisabled()) {
70  $this->redirectUrl = $this->redirectHandler->processRedirect(
71  $this->request,
72  $this->loginType,
73  $this->configuration,
74  $this->request->hasArgument('redirectReferrer') ? $this->request->getArgument('redirectReferrer') : ''
75  );
76  }
77  }
78 
82  public function ‪loginAction(): ResponseInterface
83  {
84  if ($this->‪isLogoutSuccessful()) {
85  $this->eventDispatcher->dispatch(new ‪LogoutConfirmedEvent($this, $this->view));
86  } elseif ($this->‪hasLoginErrorOccurred()) {
87  $this->eventDispatcher->dispatch(new ‪LoginErrorOccurredEvent());
88  }
89 
90  if (($forwardResponse = $this->‪handleLoginForwards()) !== null) {
91  return $forwardResponse;
92  }
93  if (($redirectResponse = $this->‪handleRedirect()) !== null) {
94  return $redirectResponse;
95  }
96 
97  $this->eventDispatcher->dispatch(new ‪ModifyLoginFormViewEvent($this->view));
98 
99  $storagePageIds = (‪$GLOBALS['TYPO3_CONF_VARS']['FE']['checkFeUserPid'] ?? false)
100  ? $this->pageRepository->getPageIdsRecursive(‪GeneralUtility::intExplode(',', (string)($this->settings['pages'] ?? ''), true), (int)($this->settings['recursive'] ?? 0))
101  : [];
102 
103  $this->view->assignMultiple(
104  [
105  'messageKey' => $this->‪getStatusMessageKey(),
106  'permaloginStatus' => $this->‪getPermaloginStatus(),
107  'redirectURL' => $this->redirectHandler->getLoginFormRedirectUrl($this->request, $this->configuration, $this->‪isRedirectDisabled()),
108  'redirectReferrer' => $this->request->hasArgument('redirectReferrer') ? (string)$this->request->getArgument('redirectReferrer') : '',
109  'referer' => $this->redirectHandler->getReferrerForLoginForm($this->request, $this->settings),
110  'noRedirect' => $this->‪isRedirectDisabled(),
111  'requestToken' => ‪RequestToken::create('core/user-auth/fe')
112  ->withMergedParams(['pid' => implode(',', $storagePageIds)]),
113  ]
114  );
115 
116  return $this->‪htmlResponse();
117  }
118 
122  public function ‪overviewAction(bool $showLoginMessage = false): ResponseInterface
123  {
124  if (!$this->userAspect->isLoggedIn()) {
125  return new ‪ForwardResponse('login');
126  }
127  $this->eventDispatcher->dispatch(new ‪LoginConfirmedEvent($this, $this->view));
128  if (($redirectResponse = $this->‪handleRedirect()) !== null) {
129  return $redirectResponse;
130  }
131  $this->view->assignMultiple(
132  [
133  'user' => $this->request->getAttribute('frontend.user')->user,
134  'showLoginMessage' => $showLoginMessage,
135  ]
136  );
137  return $this->‪htmlResponse();
138  }
139 
143  public function ‪logoutAction(int $redirectPageLogout = 0): ResponseInterface
144  {
145  if (($redirectResponse = $this->‪handleRedirect()) !== null) {
146  return $redirectResponse;
147  }
148  $this->view->assignMultiple(
149  [
150  'user' => $this->request->getAttribute('frontend.user')->user,
151  'noRedirect' => $this->isRedirectDisabled(),
152  'actionUri' => $this->redirectHandler->getLogoutFormRedirectUrl(
153  $this->request,
154  $this->configuration,
155  $redirectPageLogout,
156  $this->isRedirectDisabled()
157  ),
158  ]
159  );
160  return $this->‪htmlResponse();
161  }
162 
166  protected function ‪handleRedirect(): ?ResponseInterface
167  {
168  if ($this->redirectUrl !== '') {
169  $event = new ‪BeforeRedirectEvent($this->loginType, $this->redirectUrl, $this->request);
170  $this->eventDispatcher->dispatch($event);
171  if ($event->getRedirectUrl() !== '') {
172  return $this->‪redirectToUri($event->getRedirectUrl());
173  }
174  }
175  return null;
176  }
177 
181  protected function ‪handleLoginForwards(): ?ResponseInterface
182  {
183  if ($this->‪shouldRedirectToOverview()) {
184  return (new ‪ForwardResponse('overview'))->withArguments(['showLoginMessage' => true]);
185  }
186 
187  if ($this->userAspect->isLoggedIn()) {
188  return (new ‪ForwardResponse('logout'))->withArguments(['redirectPageLogout' => $this->settings['redirectPageLogout']]);
189  }
190 
191  return null;
192  }
193 
198  protected function ‪getPermaloginStatus(): int
199  {
200  $permaLogin = (int)‪$GLOBALS['TYPO3_CONF_VARS']['FE']['permalogin'];
201 
202  return $this->‪isPermaloginDisabled($permaLogin) ? -1 : $permaLogin;
203  }
204 
205  protected function ‪isPermaloginDisabled(int $permaLogin): bool
206  {
207  return $permaLogin > 1
208  || (int)($this->settings['showPermaLogin'] ?? 0) === 0
209  || ‪$GLOBALS['TYPO3_CONF_VARS']['FE']['lifetime'] === 0;
210  }
211 
215  protected function ‪shouldRedirectToOverview(): bool
216  {
217  return $this->userAspect->isLoggedIn()
218  && (LoginType::tryFrom($this->loginType) === LoginType::LOGIN)
219  && !($this->settings['showLogoutFormAfterLogin'] ?? 0);
220  }
221 
225  protected function ‪getStatusMessageKey(): string
226  {
227  $messageKey = ‪self::MESSAGEKEY_DEFAULT;
228  if ($this->‪hasLoginErrorOccurred()) {
229  $messageKey = ‪self::MESSAGEKEY_ERROR;
230  } elseif (LoginType::tryFrom($this->loginType) === ‪LoginType::LOGOUT) {
231  $messageKey = ‪self::MESSAGEKEY_LOGOUT;
232  }
233 
234  return $messageKey;
235  }
236 
237  protected function ‪isLoginOrLogoutInProgress(): bool
238  {
239  $type = LoginType::tryFrom($this->loginType);
240  return $type === LoginType::LOGIN || $type === ‪LoginType::LOGOUT;
241  }
242 
246  public function ‪isRedirectDisabled(): bool
247  {
248  return
249  $this->request->hasArgument('noredirect')
250  || ($this->settings['noredirect'] ?? false)
251  || ($this->settings['redirectDisable'] ?? false);
252  }
253 
254  protected function ‪isLogoutSuccessful(): bool
255  {
256  return LoginType::tryFrom($this->loginType) === ‪LoginType::LOGOUT && !$this->userAspect->isLoggedIn();
257  }
258 
259  protected function ‪hasLoginErrorOccurred(): bool
260  {
261  return LoginType::tryFrom($this->loginType) === LoginType::LOGIN && !$this->userAspect->isLoggedIn();
262  }
263 }
‪TYPO3\CMS\FrontendLogin\Controller\LoginController\MESSAGEKEY_LOGOUT
‪const MESSAGEKEY_LOGOUT
Definition: LoginController.php:46
‪TYPO3\CMS\FrontendLogin\Controller\LoginController\$userAspect
‪UserAspect $userAspect
Definition: LoginController.php:51
‪TYPO3\CMS\FrontendLogin\Controller\LoginController\isPermaloginDisabled
‪isPermaloginDisabled(int $permaLogin)
Definition: LoginController.php:205
‪TYPO3\CMS\FrontendLogin\Controller\LoginController\$configuration
‪RedirectConfiguration $configuration
Definition: LoginController.php:50
‪TYPO3\CMS\FrontendLogin\Controller\LoginController\handleRedirect
‪handleRedirect()
Definition: LoginController.php:166
‪TYPO3\CMS\FrontendLogin\Controller\LoginController\hasLoginErrorOccurred
‪hasLoginErrorOccurred()
Definition: LoginController.php:259
‪TYPO3\CMS\FrontendLogin\Controller\LoginController\MESSAGEKEY_ERROR
‪const MESSAGEKEY_ERROR
Definition: LoginController.php:45
‪TYPO3\CMS\FrontendLogin\Event\LoginErrorOccurredEvent
Definition: LoginErrorOccurredEvent.php:23
‪TYPO3\CMS\FrontendLogin\Controller\LoginController\isLogoutSuccessful
‪isLogoutSuccessful()
Definition: LoginController.php:254
‪TYPO3\CMS\FrontendLogin\Controller\LoginController\handleLoginForwards
‪handleLoginForwards()
Definition: LoginController.php:181
‪TYPO3\CMS\FrontendLogin\Controller
Definition: LoginController.php:18
‪TYPO3\CMS\FrontendLogin\Controller\LoginController\isRedirectDisabled
‪isRedirectDisabled()
Definition: LoginController.php:246
‪TYPO3\CMS\FrontendLogin\Controller\LoginController\$loginType
‪string $loginType
Definition: LoginController.php:48
‪TYPO3\CMS\Core\Authentication\LOGOUT
‪@ LOGOUT
Definition: LoginType.php:26
‪TYPO3\CMS\FrontendLogin\Controller\LoginController\shouldRedirectToOverview
‪shouldRedirectToOverview()
Definition: LoginController.php:215
‪TYPO3\CMS\Extbase\Http\ForwardResponse
Definition: ForwardResponse.php:24
‪TYPO3\CMS\Core\Context\Context
Definition: Context.php:54
‪TYPO3\CMS\FrontendLogin\Controller\LoginController\isLoginOrLogoutInProgress
‪isLoginOrLogoutInProgress()
Definition: LoginController.php:237
‪TYPO3\CMS\FrontendLogin\Controller\LoginController\MESSAGEKEY_DEFAULT
‪const MESSAGEKEY_DEFAULT
Definition: LoginController.php:44
‪TYPO3\CMS\Extbase\Mvc\Controller\ActionController\htmlResponse
‪htmlResponse(string $html=null)
Definition: ActionController.php:802
‪TYPO3\CMS\Core\Security\RequestToken
Definition: RequestToken.php:26
‪TYPO3\CMS\FrontendLogin\Controller\LoginController\__construct
‪__construct(protected readonly RedirectHandler $redirectHandler, protected readonly Context $context, protected readonly PageRepository $pageRepository)
Definition: LoginController.php:53
‪TYPO3\CMS\Core\Authentication\LoginType
‪LoginType
Definition: LoginType.php:24
‪TYPO3\CMS\FrontendLogin\Controller\LoginController\overviewAction
‪overviewAction(bool $showLoginMessage=false)
Definition: LoginController.php:122
‪TYPO3\CMS\FrontendLogin\Controller\LoginController\$redirectUrl
‪string $redirectUrl
Definition: LoginController.php:49
‪TYPO3\CMS\FrontendLogin\Event\ModifyLoginFormViewEvent
Definition: ModifyLoginFormViewEvent.php:26
‪TYPO3\CMS\FrontendLogin\Event\LogoutConfirmedEvent
Definition: LogoutConfirmedEvent.php:24
‪TYPO3\CMS\FrontendLogin\Event\LoginConfirmedEvent
Definition: LoginConfirmedEvent.php:24
‪TYPO3\CMS\FrontendLogin\Controller\LoginController\getStatusMessageKey
‪getStatusMessageKey()
Definition: LoginController.php:225
‪TYPO3\CMS\FrontendLogin\Controller\LoginController\getPermaloginStatus
‪getPermaloginStatus()
Definition: LoginController.php:198
‪TYPO3\CMS\FrontendLogin\Controller\LoginController\logoutAction
‪logoutAction(int $redirectPageLogout=0)
Definition: LoginController.php:143
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Extbase\Mvc\Controller\ActionController
Definition: ActionController.php:63
‪TYPO3\CMS\FrontendLogin\Configuration\RedirectConfiguration\fromSettings
‪static fromSettings(array $settings)
Definition: RedirectConfiguration.php:75
‪TYPO3\CMS\FrontendLogin\Configuration\RedirectConfiguration
Definition: RedirectConfiguration.php:28
‪TYPO3\CMS\FrontendLogin\Event\BeforeRedirectEvent
Definition: BeforeRedirectEvent.php:28
‪TYPO3\CMS\Core\Domain\Repository\PageRepository
Definition: PageRepository.php:69
‪TYPO3\CMS\Core\Security\RequestToken\create
‪static create(string $scope)
Definition: RequestToken.php:43
‪TYPO3\CMS\FrontendLogin\Controller\LoginController\initializeAction
‪initializeAction()
Definition: LoginController.php:64
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\FrontendLogin\Controller\LoginController
Definition: LoginController.php:43
‪TYPO3\CMS\FrontendLogin\Redirect\RedirectHandler
Definition: RedirectHandler.php:33
‪TYPO3\CMS\Core\Utility\GeneralUtility\intExplode
‪static list< int > intExplode(string $delimiter, string $string, bool $removeEmptyValues=false)
Definition: GeneralUtility.php:756
‪TYPO3\CMS\FrontendLogin\Controller\LoginController\loginAction
‪loginAction()
Definition: LoginController.php:82
‪TYPO3\CMS\Extbase\Mvc\Controller\ActionController\redirectToUri
‪redirectToUri(string|UriInterface $uri, $_=null, int $statusCode=303)
Definition: ActionController.php:714
‪TYPO3\CMS\Core\Context\UserAspect
Definition: UserAspect.php:37