Logout TYPO3 FE user when he enter specified page.

I wrote the following function to logout the FE user having specified usergroup, if he enter the specified page.

Specify the user function

includeLibs.logoutcheck = EXT:myextension/Classes/UserFunctions/UserLogoutCheck.php
page.30 = USER
page.30 {
userFunc = tx_user_logoutcheck->checkPageToLogout
logoutpageRoot = 10,20 #user will be logout if he enters any page under page id 10 and 20
userGroupToLogout = 1 #user having usergroup with uid 1 will be logout
}

On File UserLogoutCheck.php

<?php

use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\HttpUtility;

class tx_user_logoutcheck{

/**
*
* @param  string      Empty string (no content to process)
* @param  array        TypoScript configuration
* @return void
*/
public function checkPageToLogout($content, $conf) {

if($GLOBALS[‘TSFE’]->fe_user){
$logoutpageRoot = $conf[‘logoutpageRoot’];
$userGroupToLogout = $conf[‘userGroupToLogout’];
$currentUserGroup = $GLOBALS[‘TSFE’]->fe_user->user[‘usergroup’];

if(($userGroupToLogout == $currentUserGroup) && $userGroupToLogout != ”) {
$logoutpageArray = explode(‘,’ ,$logoutpageRoot);
$local_cObj = GeneralUtility::makeInstance(‘TYPO3\\CMS\\Frontend\\ContentObject\\ContentObjectRenderer’);
$findRootline = $GLOBALS[‘TSFE’]->sys_page->getRootLine(intval($GLOBALS[‘TSFE’]->id));
foreach($findRootline as $key=>$value){
if((in_array($value[‘pid’] , $logoutpageArray)) ||  (in_array($value[‘uid’] , $logoutpageArray))){
$GLOBALS[‘TSFE’]->fe_user->logoff();
$GLOBALS[‘TSFE’]->loginUser = 0;

//Redirect to same page: oterhwise userwill not logout due to cache issue.
$linkconf[‘parameter’] = intval($GLOBALS[‘TSFE’]->id);
$linkconf[‘returnLast’] = ‘url’;
// $linkconf[‘additionalParams’] = ‘&logintype=logout’;
$linkUrl = $local_cObj->typoLink(”,$linkconf);
HttpUtility::redirect($linkUrl);
}
}
}
}
}
}