1313 ***/
1414
1515use ChristianEssl \Impersonate \Exception \NoAdminUserException ;
16- use ChristianEssl \Impersonate \Utility \ConfigurationUtility ;
17- use Psr \Log \NullLogger ;
1816use TYPO3 \CMS \Core \Authentication \BackendUserAuthentication ;
1917use TYPO3 \CMS \Core \Error \Http \ServiceUnavailableException ;
2018use TYPO3 \CMS \Core \Utility \GeneralUtility ;
21- use TYPO3 \CMS \Core \Utility \VersionNumberUtility ;
2219use 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+ }
0 commit comments