-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjs_logout.module
More file actions
166 lines (145 loc) · 4.75 KB
/
js_logout.module
File metadata and controls
166 lines (145 loc) · 4.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<?php
// $Id:
/**
* @file
* Used to automagically log out a user after a preset time.
*/
/**
* Implementation of hook_perm().
*/
function js_logout_perm() {
return array('administer js_logout');
}
/**
* Implementation of hook_menu().
*/
function js_logout_menu() {
$items = array();
$items['admin/settings/js_logout'] = array(
'title' => 'JS Logout',
'description' => 'Administer JS Logout settings.',
'page callback' => 'drupal_get_form',
'page arguments' => array('js_logout_settings'),
'access arguments' => array('administer js_logout'),
'file' => 'js_logout.admin.inc',
'type' => MENU_NORMAL_ITEM
);
$items['js_logout_ahah_logout'] = array(
'title' => 'JS Logout',
'page callback' => 'js_logout_ahah_logout',
'access callback' => '_js_logout_access_ahah',
'type' => MENU_CALLBACK
);
$items['js_logout_ahah_set_last'] = array(
'title' => 'JS Logout AHAH Set Last',
'page callback' => 'js_logout_ahah_set_last',
'access callback' => '_js_logout_access_ahah',
'type' => MENU_CALLBACK
);
return $items;
}
/**
* Access callback for ahah menu items
*/
function _js_logout_access_ahah() {
global $user;
return $user->uid && user_access('access content');
}
/**
* Implementation of hook_help().
*/
function js_logout_help($path, $arg) {
switch ($path) {
case 'admin/help#js_logout':
return '<p>'. t('This module allows you to force site users to be logged out after a given amount of time due to inactivity after first being presented with a confirmation dialog.') .'</p>';
}
}
/**
* Implementation of hook_init().
*/
function js_logout_init() {
global $user;
if ($user->uid && _js_logout_logout_role($user)) {
// should we be enforcing on admin pages?
if (arg(0) == 'admin' && !variable_get('js_logout_enforce_admin', FALSE)) {
return;
}
$now = time();
$timeout = variable_get('js_logout_timeout', 1800);
$timeout_padding = variable_get('js_logout_padding', 10);
$redirct_url = variable_get('js_logout_redirect_url', 'user/login');
// add jquery ui files
$jquery_ui = module_exists('jquery_ui');
if ($jquery_ui) {
jquery_ui_add(array('ui.dialog'));
// add default css. @todo: not sure if this is the best approach
drupal_add_css(drupal_get_path('module', 'jquery_ui') . '/jquery.ui/themes/default/ui.all.css');
}
// return all the drupal settings we'll need
$msg = t('@msg', array('@msg' => variable_get('js_logout_message', 'Your session is about to expire. Do you want to reset it?')));
$settings = array(
'timeout' => $timeout * 1000,
'timeout_padding' => $timeout_padding * 1000,
'message' => t('@msg', array('@msg' => variable_get('js_logout_message', 'Your session is about to expire. Do you want to reset it?'))),
'redirect_url' => url($redirct_url, array('query' => drupal_get_destination())),
'jquery_ui' => $jquery_ui,
'title' => t('@name Alert', array('@name' => variable_get('site_name', 'Drupal')))
);
drupal_add_js(array('js_logout' => $settings), 'setting');
drupal_add_js(drupal_get_path('module', 'js_logout') . '/js_logout.js');
// we need a backup plan if JS is disabled
if (isset($_SESSION['js_logout_last'])) {
// if time since last access is > than the timeout + some padding, logout the user
if (($now - $_SESSION['js_logout_last']) >= ($timeout + (int)$timeout_padding)) {
_js_logout_logout();
drupal_goto($redirct_url, drupal_get_destination());
}
else {
$_SESSION['js_logout_last'] = $now;
}
}
else {
$_SESSION['js_logout_last'] = $now;
}
}
}
/**
* Callback that performs the actual logout and redirects the user.
*/
function js_logout_ahah_logout() {
_js_logout_logout();
exit();
}
/**
* Callback to reset the last access session variable.
*/
function js_logout_ahah_set_last() {
$_SESSION['js_logout_last'] = time();
}
/**
* Helper to perform the actual logout.
*/
function _js_logout_logout() {
global $user;
watchdog('user', 'Session automatically closed for %name by js_logout.', array('%name' => $user->name));
// Destroy the current session:
session_destroy();
// Only variables can be passed by reference workaround.
$null = NULL;
user_module_invoke('logout', $null, $user);
// Load the anonymous user
$user = drupal_anonymous_user();
// @todo: this message is not being displayed once logged out
drupal_set_message(t('You have been logged out due to inactivity.'));
}
/**
* Helper to determine if a given user should be auto logged out
*/
function _js_logout_logout_role($user) {
foreach ($user->roles as $key => $role) {
if (variable_get('js_logout_role_' . $key, FALSE)) {
return TRUE;
}
}
return FALSE;
}