From 74f7904f8f578eedcafac133412a2da6df971c75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20E=C3=9Fl?= Date: Fri, 12 Mar 2021 10:27:13 +0100 Subject: [PATCH] update for TYPO3 10.4 --- Classes/Authentication/AuthService.php | 37 ++++++ .../FrontendUserAuthenticator.php | 108 +++++------------- .../Controller/FrontendLoginController.php | 3 +- composer.json | 4 +- ext_emconf.php | 4 +- ext_tables.php | 2 +- 6 files changed, 72 insertions(+), 86 deletions(-) create mode 100644 Classes/Authentication/AuthService.php diff --git a/Classes/Authentication/AuthService.php b/Classes/Authentication/AuthService.php new file mode 100644 index 0000000..e3432aa --- /dev/null +++ b/Classes/Authentication/AuthService.php @@ -0,0 +1,37 @@ +getQueryBuilderForTable('fe_users'); + $queryBuilder + ->select('*') + ->from('fe_users') + ->where($queryBuilder->expr()->eq( + 'uid', + $queryBuilder->createNamedParameter($uid, \PDO::PARAM_INT) + )); + + return $queryBuilder->execute()->fetch(); + } + + public function authUser(array $user): int + { + return (int) (GeneralUtility::_GET('route') === '/impersonate/login' && + $GLOBALS['BE_USER'] instanceof BackendUserAuthentication && + $GLOBALS['BE_USER']->isAdmin()); + } + +} diff --git a/Classes/Authentication/FrontendUserAuthenticator.php b/Classes/Authentication/FrontendUserAuthenticator.php index 32e22b8..3694fbf 100644 --- a/Classes/Authentication/FrontendUserAuthenticator.php +++ b/Classes/Authentication/FrontendUserAuthenticator.php @@ -13,14 +13,10 @@ ***/ use ChristianEssl\Impersonate\Exception\NoAdminUserException; -use ChristianEssl\Impersonate\Utility\ConfigurationUtility; -use Psr\Log\NullLogger; use TYPO3\CMS\Core\Authentication\BackendUserAuthentication; use TYPO3\CMS\Core\Error\Http\ServiceUnavailableException; use TYPO3\CMS\Core\Utility\GeneralUtility; -use TYPO3\CMS\Core\Utility\VersionNumberUtility; use TYPO3\CMS\Frontend\Authentication\FrontendUserAuthentication; -use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController; /** * Logs in a frontend user without a password - use with care! @@ -38,89 +34,43 @@ public function authenticate($uid) if (!$this->isAdminUserLoggedIn()) { throw new NoAdminUserException('Missing backend administrator authentication.'); } - $this->buildTSFE(); - $this->loginFrontendUser($uid); - } - - /** - * @todo: fix this for TYPO3 10 - * Initializing the TypoScriptFrontendController this way is deprecated, but the new - * TypoScriptFrontendInitialization middleware is not production ready yet - fix this in TYPO3 10 - * - * @throws ServiceUnavailableException - */ - protected function buildTSFE() - { - $rootPageId = ConfigurationUtility::getRootPageId(); - $GLOBALS['TSFE'] = new TypoScriptFrontendController(null, $rootPageId, 0); - - if (VersionNumberUtility::convertVersionNumberToInteger(TYPO3_version) >= 9000000) { - $GLOBALS['TSFE']->setLogger(new NullLogger()); - } - - $GLOBALS['TSFE']->connectToDB(); - $GLOBALS['TSFE']->initFEuser(); + $this->loginFrontendUser((int)$uid); } /** * Login the frontend user * - * @param integer $uid + * @param int $uid */ - protected function loginFrontendUser($uid) + protected function loginFrontendUser(int $uid) { - $GLOBALS['TSFE']->fe_user->is_permanent = false; - $GLOBALS['TSFE']->fe_user->checkPid = false; - $GLOBALS['TSFE']->fe_user->createUserSession(['uid' => $uid]); - $GLOBALS['TSFE']->fe_user->user = $GLOBALS['TSFE']->fe_user->fetchUserSession(); - $GLOBALS['TSFE']->fe_user->fetchGroupData(); - $GLOBALS['TSFE']->fe_user->forceSetCookie = false; - $GLOBALS['TSFE']->fe_user->setAndSaveSessionData('Authenticated via impersonate extension', true); - $this->setSessionCookie($GLOBALS['TSFE']->fe_user); - } + \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addService( + 'impersonate', + 'auth', + \ChristianEssl\Impersonate\Authentication\AuthService::class, + [ + 'title' => 'Temporary AuthService for impersonating a user', + 'description' => 'Temporary AuthService for impersonating a user', + 'subtype' => 'authUserFE,getUserFE', + 'available' => true, + 'priority' => 100, + 'quality' => 70, + 'os' => '', + 'exec' => '', + 'className' => \ChristianEssl\Impersonate\Authentication\AuthService::class, + ] + ); - /** - * Set the session cookie after login (otherwise the login will fail on first time, if no session cookie exists yet) - * - * @param FrontendUserAuthentication $user - */ - protected function setSessionCookie(FrontendUserAuthentication $user) - { - $cookieDomain = $this->getCookieDomain($user); - $cookiePath = $cookieDomain ? '/' : GeneralUtility::getIndpEnv('TYPO3_SITE_PATH'); - $cookieSecure = (bool)$GLOBALS['TYPO3_CONF_VARS']['SYS']['cookieSecure'] && GeneralUtility::getIndpEnv('TYPO3_SSL'); - setcookie($user->name, $user->id, 0, $cookiePath, $cookieDomain, $cookieSecure, true); - } + $frontendUser = GeneralUtility::makeInstance(FrontendUserAuthentication::class); + $frontendUser->svConfig = [ + 'setup' => [ + 'FE_alwaysFetchUser' => true + ] + ]; - /** - * Gets the domain to be used on setting cookies. - * Code taken from typo3/sysext/core/Classes/Authentication/AbstractUserAuthentication - * - * @param FrontendUserAuthentication $user - * - * @return string The domain to be used on setting cookies - */ - protected function getCookieDomain(FrontendUserAuthentication $user) - { - $result = ''; - $cookieDomain = $GLOBALS['TYPO3_CONF_VARS']['SYS']['cookieDomain']; - // If a specific cookie domain is defined for a given TYPO3_MODE, - // use that domain - if (!empty($GLOBALS['TYPO3_CONF_VARS'][$user->loginType]['cookieDomain'])) { - $cookieDomain = $GLOBALS['TYPO3_CONF_VARS'][$user->loginType]['cookieDomain']; - } - if ($cookieDomain) { - if ($cookieDomain[0] === '/') { - $match = []; - $matchCnt = @preg_match($cookieDomain, GeneralUtility::getIndpEnv('TYPO3_HOST_ONLY'), $match); - if ($matchCnt) { - $result = $match[0]; - } - } else { - $result = $cookieDomain; - } - } - return $result; + $frontendUser->start(); + $frontendUser->unpack_uc(); + $frontendUser->storeSessionData(); } /** @@ -131,4 +81,4 @@ protected function isAdminUserLoggedIn() return $GLOBALS['BE_USER'] instanceof BackendUserAuthentication && $GLOBALS['BE_USER']->isAdmin(); } -} \ No newline at end of file +} diff --git a/Classes/Controller/FrontendLoginController.php b/Classes/Controller/FrontendLoginController.php index 1ba6e02..405b5ea 100644 --- a/Classes/Controller/FrontendLoginController.php +++ b/Classes/Controller/FrontendLoginController.php @@ -31,14 +31,13 @@ class FrontendLoginController { /** * @param ServerRequestInterface $request - * @param ResponseInterface $response * * @return RedirectResponse * @throws NoUserIdException * @throws ServiceUnavailableException * @throws NoAdminUserException */ - public function loginAction(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface + public function loginAction(ServerRequestInterface $request): ResponseInterface { $uid = (int) $request->getQueryParams()['uid']; diff --git a/composer.json b/composer.json index 7f170ef..7b1b343 100644 --- a/composer.json +++ b/composer.json @@ -14,9 +14,9 @@ "role": "Developer" } ], - "version": "1.0.0", + "version": "1.1.0", "require": { - "typo3/cms-core": "^8.7.0 || ^9.5.0 || ^10.4.0" + "typo3/cms-core": "^10.4.0" }, "autoload": { "psr-4": { diff --git a/ext_emconf.php b/ext_emconf.php index 0c5ef7d..c0b387b 100644 --- a/ext_emconf.php +++ b/ext_emconf.php @@ -14,10 +14,10 @@ 'uploadfolder' => 0, 'createDirs' => '', 'clearCacheOnLoad' => 0, - 'version' => '1.0.0', + 'version' => '1.1.0', 'constraints' => [ 'depends' => [ - 'typo3' => '8.7.0-10.4.99', + 'typo3' => '10.4.0-10.4.99', ], 'conflicts' => [], 'suggests' => [], diff --git a/ext_tables.php b/ext_tables.php index 21dda5d..2b62032 100644 --- a/ext_tables.php +++ b/ext_tables.php @@ -3,4 +3,4 @@ die('Access denied.'); } -\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addStaticFile($_EXTKEY, 'Configuration/TypoScript', 'Impersonate'); \ No newline at end of file +\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addStaticFile('impersonate', 'Configuration/TypoScript', 'Impersonate'); \ No newline at end of file