[TASK] Use SiteFinder in EXT:felogin 57/58257/5
authorGeorg Ringer <georg.ringer@gmail.com>
Wed, 12 Sep 2018 18:06:46 +0000 (20:06 +0200)
committerFrank Naegler <frank.naegler@typo3.org>
Fri, 14 Sep 2018 12:49:56 +0000 (14:49 +0200)
Instead of only checking the sys_domains with a database query
the SiteFinder and PseudoSiteFinder should be used.

Resolves: #86219
Releases: master
Change-Id: I99f9367258f9b19ef3a1310cb2fd61076083333c
Reviewed-on: https://review.typo3.org/58257
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Benni Mack <benni@typo3.org>
Reviewed-by: Susanne Moog <susanne.moog@typo3.org>
Tested-by: Susanne Moog <susanne.moog@typo3.org>
Reviewed-by: Wouter Wolters <typo3@wouterwolters.nl>
Reviewed-by: Jörg Bösche <typo3@joergboesche.de>
Reviewed-by: Frank Naegler <frank.naegler@typo3.org>
Tested-by: Frank Naegler <frank.naegler@typo3.org>
typo3/sysext/felogin/Classes/Controller/FrontendLoginController.php
typo3/sysext/felogin/Tests/Unit/Controller/FrontendLoginControllerTest.php

index 3dc5248..c5b317c 100644 (file)
@@ -23,6 +23,8 @@ use TYPO3\CMS\Core\Crypto\Random;
 use TYPO3\CMS\Core\Database\Connection;
 use TYPO3\CMS\Core\Database\ConnectionPool;
 use TYPO3\CMS\Core\Database\Query\Restriction\FrontendRestrictionContainer;
+use TYPO3\CMS\Core\Exception\SiteNotFoundException;
+use TYPO3\CMS\Core\Site\SiteFinder;
 use TYPO3\CMS\Core\Utility\GeneralUtility;
 use TYPO3\CMS\Frontend\Plugin\AbstractPlugin;
 
@@ -92,6 +94,9 @@ class FrontendLoginController extends AbstractPlugin implements LoggerAwareInter
      */
     protected $logintype;
 
+    /** @var SiteFinder */
+    protected $siteFinder;
+
     /**
      * A list of page UIDs, either an integer or a comma-separated list of integers
      *
@@ -116,6 +121,8 @@ class FrontendLoginController extends AbstractPlugin implements LoggerAwareInter
      */
     public function main($content, $conf)
     {
+        $this->siteFinder = GeneralUtility::makeInstance(SiteFinder::class);
+
         // Loading TypoScript array into object variable:
         $this->conf = $conf;
         // Loading default pivars
@@ -1052,28 +1059,36 @@ class FrontendLoginController extends AbstractPlugin implements LoggerAwareInter
             $parsedUrl = parse_url($url);
             if ($parsedUrl['scheme'] === 'http' || $parsedUrl['scheme'] === 'https') {
                 $host = $parsedUrl['host'];
-                // Removes the last path segment and slash sequences like /// (if given):
-                $path = preg_replace('#/+[^/]*$#', '', $parsedUrl['path'] ?? '');
 
-                $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('sys_domain');
-                $queryBuilder->setRestrictions(GeneralUtility::makeInstance(FrontendRestrictionContainer::class));
-                $localDomains = $queryBuilder->select('domainName')
-                    ->from('sys_domain')
-                    ->execute()
-                    ->fetchAll();
+                try {
+                    $site = $this->siteFinder->getSiteByPageId((int)$this->frontendController->id);
+                    return $site->getBase()->getHost() === $host;
+                } catch (SiteNotFoundException $e) {
 
-                if (is_array($localDomains)) {
-                    foreach ($localDomains as $localDomain) {
-                        // strip trailing slashes (if given)
-                        $domainName = rtrim($localDomain['domainName'], '/');
-                        if (GeneralUtility::isFirstPartOfStr($host . $path . '/', $domainName . '/')) {
-                            $result = true;
-                            break;
+                    // Removes the last path segment and slash sequences like /// (if given):
+                    $path = preg_replace('#/+[^/]*$#', '', $parsedUrl['path'] ?? '');
+
+                    $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('sys_domain');
+                    $queryBuilder->setRestrictions(GeneralUtility::makeInstance(FrontendRestrictionContainer::class));
+                    $localDomains = $queryBuilder->select('domainName')
+                        ->from('sys_domain')
+                        ->execute()
+                        ->fetchAll();
+
+                    if (is_array($localDomains)) {
+                        foreach ($localDomains as $localDomain) {
+                            // strip trailing slashes (if given)
+                            $domainName = rtrim($localDomain['domainName'], '/');
+                            if (GeneralUtility::isFirstPartOfStr($host . $path . '/', $domainName . '/')) {
+                                $result = true;
+                                break;
+                            }
                         }
                     }
                 }
             }
         }
+
         return $result;
     }
 
index 08adb3a..e72dfb7 100644 (file)
@@ -23,6 +23,8 @@ use TYPO3\CMS\Core\Database\Connection;
 use TYPO3\CMS\Core\Database\ConnectionPool;
 use TYPO3\CMS\Core\Database\Query\Expression\ExpressionBuilder;
 use TYPO3\CMS\Core\Database\Query\QueryBuilder;
+use TYPO3\CMS\Core\Exception\SiteNotFoundException;
+use TYPO3\CMS\Core\Site\SiteFinder;
 use TYPO3\CMS\Core\Tests\Unit\Database\Mocks\MockPlatform;
 use TYPO3\CMS\Core\Utility\GeneralUtility;
 use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
@@ -66,6 +68,11 @@ class FrontendLoginControllerTest extends UnitTestCase
         $this->accessibleFixture->cObj = $this->createMock(\TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer::class);
         $this->accessibleFixture->_set('frontendController', $this->createMock(\TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController::class));
         $this->accessibleFixture->setLogger(new NullLogger());
+
+        $mockedSiteFinder = $this->getAccessibleMock(SiteFinder::class, ['getSiteByPageId'], [], '', false, false);
+        $mockedSiteFinder->method('getSiteByPageId')->willThrowException(new SiteNotFoundException('Site not found', 1536819047));
+        $this->accessibleFixture->_set('siteFinder', $mockedSiteFinder);
+
         $this->setUpFakeSitePathAndHost();
     }