Is there any module or possibility to automatically logout user filtered by role when browser is closed?
The only thing I found was ini_set('session.cookie_lifetime', 0); in settings.php, but this is not helpful, because it is for all users.
You can call the windows beforeunload event, and make an ajax call to user/logout, which will log out the user.
To add the role testing element, you could copy the user/logout page callback function and add your own version of user_logout page callback function that checks the role. For example "user_logout_by_role"
In your custom module, add hook_menu
/**
* Implements hook_menu().
*/
function mymodule_menu() {
$items['user/logout_by_role'] = array(
'title' => 'Log out',
'access callback' => 'user_is_logged_in',
'page callback' => 'mymodule_user_logout_by_role',
'weight' => 10,
'menu_name' => 'user-menu',
'file' => 'user.pages.inc',
);
return $items;
}
then, copy the user logout function and add some code to check role
function mymodule_user_logout_by_role() {
global $user;
if (in_array('editor', $user->roles)) {
watchdog('user', 'Session closed for %name.', array('%name' => $user->name));
module_invoke_all('user_logout', $user);
// Destroy the current session, and reset $user to the anonymous user.
session_destroy();
}
die();
}
Here's some other helpful tips for the beforeunload event.