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);
}
}
}
}
}
}

Felogin logout redirect does not work

When we try to logout from an access restricted page, we will see either a 404 (Page not found) or a 401 (Authorization required) error.
This issue seems quite old, but unfortunately there are still some bugs when we use the Logout template of the felogin.

The issue is reproduceable even on Typo3 4.2.8, I am not sure if this is fixed on 4.3 onwards.

There is a patch that we can use to patch felogin as given on the following URL:
http://bugs.typo3.org/view.php?id=8677

Due to various reasons it may not always be possible to patch.
In such circumstances it is wiser to use an alternate technique, such as creating the Logout link manually.
One such example is:

[loginUser = *]  

lib.logoutbox = COA
lib.logoutbox {
    20 = TEXT
    20 {
       data = TSFE:fe_user|user|username
       # link to account edit page
       stdWrap.typolink.parameter = 60
    }
    30 = IMAGE
    30 {
        file = fileadmin/templates/images/logout.gif
        stdWrap {
             
             # Let us link to home page for logging out
             typolink.parameter = 1
             typolink.additionalParams = &logintype=logout
             wrap =  |
         }
    }
}

[end]

In this example I am displaying the logged in username (which links to user account page) followed by a working logout button.

Note:
– Here we are not using the felogin plugin.
– Do not link the logout button to any access restricted page.

The following page from Typo3wizard might be useful for displaying more user-details if needed:
http://www.typo3wizard.com/en/snippets/cool-stuff-typoscript/userinfo-for-currently-logged-in-user.html