‪TYPO3CMS  ‪main
RedirectModeHandler.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 
26 
33 {
34  public function ‪__construct(
35  protected readonly ‪UriBuilder $uriBuilder,
36  protected readonly ‪RedirectUrlValidator $redirectUrlValidator,
37  private readonly ‪FrontendUserRepository $frontendUserRepository,
38  private readonly ‪FrontendUserGroupRepository $frontendUserGroupRepository
39  ) {}
40 
44  public function ‪redirectModeGroupLogin(‪RequestInterface $request): string
45  {
46  $groups = $request->getAttribute('frontend.user')->userGroups;
47  if (empty($groups)) {
48  return '';
49  }
50  $groupUids = array_keys($groups);
51  // Take the first group with a redirect page
52  foreach ($groupUids as $groupUid) {
53  $redirectPageId = (int)$this->frontendUserGroupRepository
54  ->findRedirectPageIdByGroupId($groupUid);
55  if ($redirectPageId > 0) {
56  return $this->‪buildUriForPageUid($request, $redirectPageId);
57  }
58  }
59  return '';
60  }
61 
65  public function ‪redirectModeUserLogin(‪RequestInterface $request): string
66  {
67  $userUid = (int)$request->getAttribute('frontend.user')->user['uid'];
68  $redirectPageId = $this->frontendUserRepository->findRedirectIdPageByUserId($userUid);
69  if ($redirectPageId === null) {
70  return '';
71  }
72  return $this->‪buildUriForPageUid($request, $redirectPageId);
73  }
74 
78  public function ‪redirectModeLogin(‪RequestInterface $request, int $redirectPageLogin): string
79  {
80  $redirectUrl = '';
81  if ($redirectPageLogin !== 0) {
82  $redirectUrl = $this->‪buildUriForPageUid($request, $redirectPageLogin);
83  }
84  return $redirectUrl;
85  }
86 
90  public function ‪redirectModeReferrer(‪RequestInterface $request, string $redirectReferrer): string
91  {
92  $redirectUrl = '';
93  if ($redirectReferrer !== 'off') {
94  // Avoid forced logout, when trying to login immediately after a logout
95  $redirectUrl = preg_replace('/[&?]logintype=[a-z]+/', '', $this->‪getReferrer($request));
96  }
97  return $redirectUrl ?? '';
98  }
99 
103  public function ‪redirectModeReferrerDomains(‪RequestInterface $request, string $domains, string $redirectReferrer): string
104  {
105  $redirectUrl = '';
106  if ($redirectReferrer !== '') {
107  return '';
108  }
109 
110  // Auto redirect.
111  // Feature to redirect to the page where the user came from (HTTP_REFERER).
112  // Allowed domains to redirect to, can be configured with plugin.tx_felogin_login.domains
113  // also avoid redirect when logging in after changing password
114  if ($domains) {
115  ‪$url = $this->‪getReferrer($request);
116  // Is referring url allowed to redirect?
117  $match = [];
118  if (preg_match('#^https?://([[:alnum:].-]+)/#', ‪$url, $match)) {
119  $redirectDomain = $match[1];
120  $found = false;
121  foreach (‪GeneralUtility::trimExplode(',', $domains, true) as $domain) {
122  if (preg_match('/(?:^|\\.)' . preg_quote($domain, '/') . '$/', $redirectDomain)) {
123  $found = true;
124  break;
125  }
126  }
127  if (!$found) {
128  ‪$url = '';
129  }
130  }
131  // Avoid forced logout, when trying to login immediately after a logout
132  if (‪$url) {
133  $redirectUrl = preg_replace('/[&?]logintype=[a-z]+/', '', ‪$url);
134  }
135  }
136 
137  return $redirectUrl ?? '';
138  }
139 
143  public function ‪redirectModeLoginError(‪RequestInterface $request, int $redirectPageLoginError = 0): string
144  {
145  $redirectUrl = '';
146  if ($redirectPageLoginError > 0) {
147  $redirectUrl = $this->‪buildUriForPageUid($request, $redirectPageLoginError);
148  }
149  return $redirectUrl;
150  }
151 
155  public function ‪redirectModeLogout(‪RequestInterface $request, int $redirectPageLogout): string
156  {
157  $redirectUrl = '';
158  if ($redirectPageLogout > 0) {
159  $redirectUrl = $this->‪buildUriForPageUid($request, $redirectPageLogout);
160  }
161  return $redirectUrl;
162  }
163 
164  protected function ‪buildUriForPageUid(‪RequestInterface $request, int $pageUid): string
165  {
166  $this->uriBuilder->reset();
167  $this->uriBuilder->setRequest($request);
168  $this->uriBuilder->setTargetPageUid($pageUid);
169  return $this->uriBuilder->build();
170  }
171 
172  protected function ‪getReferrer(‪RequestInterface $request): string
173  {
174  $referrer = '';
175  $requestReferrer = (string)($request->getParsedBody()['referer'] ?? $request->getQueryParams()['referer'] ?? '');
176  if ($this->redirectUrlValidator->isValid($request, $requestReferrer)) {
177  $referrer = $requestReferrer;
178  }
179  return $referrer;
180  }
181 }
‪TYPO3\CMS\FrontendLogin\Redirect\RedirectModeHandler
Definition: RedirectModeHandler.php:33
‪TYPO3\CMS\FrontendLogin\Redirect\RedirectModeHandler\__construct
‪__construct(protected readonly UriBuilder $uriBuilder, protected readonly RedirectUrlValidator $redirectUrlValidator, private readonly FrontendUserRepository $frontendUserRepository, private readonly FrontendUserGroupRepository $frontendUserGroupRepository)
Definition: RedirectModeHandler.php:34
‪TYPO3\CMS\FrontendLogin\Domain\Repository\FrontendUserRepository
Definition: FrontendUserRepository.php:28
‪TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder
Definition: UriBuilder.php:38
‪TYPO3\CMS\FrontendLogin\Redirect\RedirectModeHandler\buildUriForPageUid
‪buildUriForPageUid(RequestInterface $request, int $pageUid)
Definition: RedirectModeHandler.php:164
‪TYPO3\CMS\FrontendLogin\Redirect\RedirectModeHandler\redirectModeGroupLogin
‪redirectModeGroupLogin(RequestInterface $request)
Definition: RedirectModeHandler.php:44
‪TYPO3\CMS\FrontendLogin\Redirect\RedirectModeHandler\redirectModeLogout
‪redirectModeLogout(RequestInterface $request, int $redirectPageLogout)
Definition: RedirectModeHandler.php:155
‪TYPO3\CMS\FrontendLogin\Redirect\RedirectModeHandler\redirectModeReferrer
‪redirectModeReferrer(RequestInterface $request, string $redirectReferrer)
Definition: RedirectModeHandler.php:90
‪TYPO3\CMS\FrontendLogin\Redirect\RedirectModeHandler\getReferrer
‪getReferrer(RequestInterface $request)
Definition: RedirectModeHandler.php:172
‪TYPO3\CMS\FrontendLogin\Redirect\RedirectModeHandler\redirectModeLoginError
‪redirectModeLoginError(RequestInterface $request, int $redirectPageLoginError=0)
Definition: RedirectModeHandler.php:143
‪TYPO3\CMS\FrontendLogin\Redirect
Definition: RedirectHandler.php:18
‪TYPO3\CMS\FrontendLogin\Redirect\RedirectModeHandler\redirectModeLogin
‪redirectModeLogin(RequestInterface $request, int $redirectPageLogin)
Definition: RedirectModeHandler.php:78
‪TYPO3\CMS\Extbase\Mvc\RequestInterface
Definition: RequestInterface.php:24
‪TYPO3\CMS\Webhooks\Message\$url
‪identifier readonly UriInterface $url
Definition: LoginErrorOccurredMessage.php:36
‪TYPO3\CMS\FrontendLogin\Redirect\RedirectModeHandler\redirectModeReferrerDomains
‪redirectModeReferrerDomains(RequestInterface $request, string $domains, string $redirectReferrer)
Definition: RedirectModeHandler.php:103
‪TYPO3\CMS\FrontendLogin\Validation\RedirectUrlValidator
Definition: RedirectUrlValidator.php:33
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\FrontendLogin\Domain\Repository\FrontendUserGroupRepository
Definition: FrontendUserGroupRepository.php:27
‪TYPO3\CMS\Core\Utility\GeneralUtility\trimExplode
‪static list< string > trimExplode(string $delim, string $string, bool $removeEmptyValues=false, int $limit=0)
Definition: GeneralUtility.php:822
‪TYPO3\CMS\FrontendLogin\Redirect\RedirectModeHandler\redirectModeUserLogin
‪redirectModeUserLogin(RequestInterface $request)
Definition: RedirectModeHandler.php:65