Skip to content

Commit 74f7904

Browse files
author
Christian Eßl
committed
update for TYPO3 10.4
1 parent 6541de1 commit 74f7904

File tree

6 files changed

+72
-86
lines changed

6 files changed

+72
-86
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace ChristianEssl\Impersonate\Authentication;
6+
7+
use TYPO3\CMS\Core\Authentication\AuthenticationService;
8+
use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
9+
use TYPO3\CMS\Core\Database\ConnectionPool;
10+
use TYPO3\CMS\Core\Utility\GeneralUtility;
11+
12+
class AuthService extends AuthenticationService
13+
{
14+
public function getUser()
15+
{
16+
$uid = (int)GeneralUtility::_GET('uid');
17+
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
18+
->getQueryBuilderForTable('fe_users');
19+
$queryBuilder
20+
->select('*')
21+
->from('fe_users')
22+
->where($queryBuilder->expr()->eq(
23+
'uid',
24+
$queryBuilder->createNamedParameter($uid, \PDO::PARAM_INT)
25+
));
26+
27+
return $queryBuilder->execute()->fetch();
28+
}
29+
30+
public function authUser(array $user): int
31+
{
32+
return (int) (GeneralUtility::_GET('route') === '/impersonate/login' &&
33+
$GLOBALS['BE_USER'] instanceof BackendUserAuthentication &&
34+
$GLOBALS['BE_USER']->isAdmin());
35+
}
36+
37+
}

Classes/Authentication/FrontendUserAuthenticator.php

Lines changed: 29 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,10 @@
1313
***/
1414

1515
use ChristianEssl\Impersonate\Exception\NoAdminUserException;
16-
use ChristianEssl\Impersonate\Utility\ConfigurationUtility;
17-
use Psr\Log\NullLogger;
1816
use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
1917
use TYPO3\CMS\Core\Error\Http\ServiceUnavailableException;
2018
use TYPO3\CMS\Core\Utility\GeneralUtility;
21-
use TYPO3\CMS\Core\Utility\VersionNumberUtility;
2219
use TYPO3\CMS\Frontend\Authentication\FrontendUserAuthentication;
23-
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
2420

2521
/**
2622
* Logs in a frontend user without a password - use with care!
@@ -38,89 +34,43 @@ public function authenticate($uid)
3834
if (!$this->isAdminUserLoggedIn()) {
3935
throw new NoAdminUserException('Missing backend administrator authentication.');
4036
}
41-
$this->buildTSFE();
42-
$this->loginFrontendUser($uid);
43-
}
44-
45-
/**
46-
* @todo: fix this for TYPO3 10
47-
* Initializing the TypoScriptFrontendController this way is deprecated, but the new
48-
* TypoScriptFrontendInitialization middleware is not production ready yet - fix this in TYPO3 10
49-
*
50-
* @throws ServiceUnavailableException
51-
*/
52-
protected function buildTSFE()
53-
{
54-
$rootPageId = ConfigurationUtility::getRootPageId();
55-
$GLOBALS['TSFE'] = new TypoScriptFrontendController(null, $rootPageId, 0);
56-
57-
if (VersionNumberUtility::convertVersionNumberToInteger(TYPO3_version) >= 9000000) {
58-
$GLOBALS['TSFE']->setLogger(new NullLogger());
59-
}
60-
61-
$GLOBALS['TSFE']->connectToDB();
62-
$GLOBALS['TSFE']->initFEuser();
37+
$this->loginFrontendUser((int)$uid);
6338
}
6439

6540
/**
6641
* Login the frontend user
6742
*
68-
* @param integer $uid
43+
* @param int $uid
6944
*/
70-
protected function loginFrontendUser($uid)
45+
protected function loginFrontendUser(int $uid)
7146
{
72-
$GLOBALS['TSFE']->fe_user->is_permanent = false;
73-
$GLOBALS['TSFE']->fe_user->checkPid = false;
74-
$GLOBALS['TSFE']->fe_user->createUserSession(['uid' => $uid]);
75-
$GLOBALS['TSFE']->fe_user->user = $GLOBALS['TSFE']->fe_user->fetchUserSession();
76-
$GLOBALS['TSFE']->fe_user->fetchGroupData();
77-
$GLOBALS['TSFE']->fe_user->forceSetCookie = false;
78-
$GLOBALS['TSFE']->fe_user->setAndSaveSessionData('Authenticated via impersonate extension', true);
79-
$this->setSessionCookie($GLOBALS['TSFE']->fe_user);
80-
}
47+
\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addService(
48+
'impersonate',
49+
'auth',
50+
\ChristianEssl\Impersonate\Authentication\AuthService::class,
51+
[
52+
'title' => 'Temporary AuthService for impersonating a user',
53+
'description' => 'Temporary AuthService for impersonating a user',
54+
'subtype' => 'authUserFE,getUserFE',
55+
'available' => true,
56+
'priority' => 100,
57+
'quality' => 70,
58+
'os' => '',
59+
'exec' => '',
60+
'className' => \ChristianEssl\Impersonate\Authentication\AuthService::class,
61+
]
62+
);
8163

82-
/**
83-
* Set the session cookie after login (otherwise the login will fail on first time, if no session cookie exists yet)
84-
*
85-
* @param FrontendUserAuthentication $user
86-
*/
87-
protected function setSessionCookie(FrontendUserAuthentication $user)
88-
{
89-
$cookieDomain = $this->getCookieDomain($user);
90-
$cookiePath = $cookieDomain ? '/' : GeneralUtility::getIndpEnv('TYPO3_SITE_PATH');
91-
$cookieSecure = (bool)$GLOBALS['TYPO3_CONF_VARS']['SYS']['cookieSecure'] && GeneralUtility::getIndpEnv('TYPO3_SSL');
92-
setcookie($user->name, $user->id, 0, $cookiePath, $cookieDomain, $cookieSecure, true);
93-
}
64+
$frontendUser = GeneralUtility::makeInstance(FrontendUserAuthentication::class);
65+
$frontendUser->svConfig = [
66+
'setup' => [
67+
'FE_alwaysFetchUser' => true
68+
]
69+
];
9470

95-
/**
96-
* Gets the domain to be used on setting cookies.
97-
* Code taken from typo3/sysext/core/Classes/Authentication/AbstractUserAuthentication
98-
*
99-
* @param FrontendUserAuthentication $user
100-
*
101-
* @return string The domain to be used on setting cookies
102-
*/
103-
protected function getCookieDomain(FrontendUserAuthentication $user)
104-
{
105-
$result = '';
106-
$cookieDomain = $GLOBALS['TYPO3_CONF_VARS']['SYS']['cookieDomain'];
107-
// If a specific cookie domain is defined for a given TYPO3_MODE,
108-
// use that domain
109-
if (!empty($GLOBALS['TYPO3_CONF_VARS'][$user->loginType]['cookieDomain'])) {
110-
$cookieDomain = $GLOBALS['TYPO3_CONF_VARS'][$user->loginType]['cookieDomain'];
111-
}
112-
if ($cookieDomain) {
113-
if ($cookieDomain[0] === '/') {
114-
$match = [];
115-
$matchCnt = @preg_match($cookieDomain, GeneralUtility::getIndpEnv('TYPO3_HOST_ONLY'), $match);
116-
if ($matchCnt) {
117-
$result = $match[0];
118-
}
119-
} else {
120-
$result = $cookieDomain;
121-
}
122-
}
123-
return $result;
71+
$frontendUser->start();
72+
$frontendUser->unpack_uc();
73+
$frontendUser->storeSessionData();
12474
}
12575

12676
/**
@@ -131,4 +81,4 @@ protected function isAdminUserLoggedIn()
13181
return $GLOBALS['BE_USER'] instanceof BackendUserAuthentication &&
13282
$GLOBALS['BE_USER']->isAdmin();
13383
}
134-
}
84+
}

Classes/Controller/FrontendLoginController.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,13 @@ class FrontendLoginController
3131
{
3232
/**
3333
* @param ServerRequestInterface $request
34-
* @param ResponseInterface $response
3534
*
3635
* @return RedirectResponse
3736
* @throws NoUserIdException
3837
* @throws ServiceUnavailableException
3938
* @throws NoAdminUserException
4039
*/
41-
public function loginAction(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface
40+
public function loginAction(ServerRequestInterface $request): ResponseInterface
4241
{
4342
$uid = (int) $request->getQueryParams()['uid'];
4443

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
"role": "Developer"
1515
}
1616
],
17-
"version": "1.0.0",
17+
"version": "1.1.0",
1818
"require": {
19-
"typo3/cms-core": "^8.7.0 || ^9.5.0 || ^10.4.0"
19+
"typo3/cms-core": "^10.4.0"
2020
},
2121
"autoload": {
2222
"psr-4": {

ext_emconf.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@
1414
'uploadfolder' => 0,
1515
'createDirs' => '',
1616
'clearCacheOnLoad' => 0,
17-
'version' => '1.0.0',
17+
'version' => '1.1.0',
1818
'constraints' => [
1919
'depends' => [
20-
'typo3' => '8.7.0-10.4.99',
20+
'typo3' => '10.4.0-10.4.99',
2121
],
2222
'conflicts' => [],
2323
'suggests' => [],

ext_tables.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
die('Access denied.');
44
}
55

6-
\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addStaticFile($_EXTKEY, 'Configuration/TypoScript', 'Impersonate');
6+
\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addStaticFile('impersonate', 'Configuration/TypoScript', 'Impersonate');

0 commit comments

Comments
 (0)