Repository navigation
Expand file tree
/
Copy pathadmin_only_mode.module
More file actions
136 lines (118 loc) · 4.08 KB
/
Copy pathadmin_only_mode.module
File metadata and controls
136 lines (118 loc) · 4.08 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
<?php
// $Id$
/**
* @file
*
* Adds an option to put the site in admin-only mode.
*/
/**
* Implementation of hook_init().
*/
function admin_only_mode_init() {
global $user;
$in_admin_only = variable_get('admin_only_mode',FALSE);
if ($in_admin_only && user_is_logged_in()) {
$message = variable_get('admin_only_mode_message','');
if ($user->uid != 1) {
// Code taken from user_logout(), avoiding drupal_goto() to allow message display
watchdog('user', 'Session closed for %name.', array('%name' => $user->name));
// Destroy the current session:
session_destroy();
module_invoke_all('user', 'logout', NULL, $user);
// Load the anonymous user
$user = drupal_anonymous_user();
drupal_get_messages(NULL, TRUE); // thow away error messages from complaining modules
drupal_set_message(t('Sorry, you do not currently have permission to be logged in.'), 'warning');
} else if (!empty($message)) {
// Show a message to the admin reminding him that we're in admin-only mode
drupal_set_message($message, 'status', FALSE /*repeat*/);
}
}
}
/**
* Implementation of hook_form_FORM_ID_alter().
* Alter the Site Login form.
*/
function admin_only_mode_form_user_login_alter(&$form, $form_state) {
// If the site is in admin-only mode, show the message at the top of this form
$in_admin_only = variable_get('admin_only_mode',FALSE);
$message = variable_get('admin_only_mode_message', '');
if ($in_admin_only) {
if (!empty($message)) {
$form['#prefix'] = $message;
}
$form['#validate'] = array_merge(
array('admin_only_mode_user_login_validator'),
$form['#validate']
);
}
}
/**
* Implementation of hook_form_FORM_ID_alter().
* Alter the Site Maintenance form.
*/
function admin_only_mode_form_system_site_maintenance_settings_alter(&$form, $form_state) {
$form["admin_only_mode_fieldset"] = array(
"#title" => "Admin Only Mode",
"#type" => "fieldset",
"#weight" => 0,
"#collapsible" => FALSE,
);
$form["admin_only_mode_fieldset"]["admin_only_mode"] = array(
"#type" => "checkbox",
"#title" => t("Admin only mode"),
"#description" => t("When set, only allow admins to be logged into the site. Other users will be logged out."),
"#weight" => 0,
"#default_value" => variable_get('admin_only_mode', FALSE),
);
$form["admin_only_mode_fieldset"]["admin_only_mode_message"] = array(
"#type" => "textarea",
"#title" => t("Site admin-only message"),
"#description" => t("Message to show visitors when the site is in admin-only mode. Leave this area empty to disable the display of a message."),
"#default_value" => variable_get('admin_only_mode_message', ''),
);
}
/**
* Validator for user_login form.
*
* @see admin_only_mode_form_user_login_alter()
*/
function admin_only_mode_user_login_validator($form, &$form_state) {
$in_admin_only = variable_get('admin_only_mode',FALSE);
if ($in_admin_only) {
$myuser = user_load(array('name' => $form_state['values']['name']));
if ($myuser->uid != 1) {
form_set_error('name', t('Sorry, only the admin user may login currently'));
watchdog('admin_only_mode', 'Login attempt failed for %user.', array('%user' => $form_state['values']['name']));
}
}
}
/**
* Sets whether we are in admin-only mode.
*
* @param $mode
* Integer setting the admin-only mode state -- 1 for on, 0 for off.
* @param $clearcache
* Boolean whether to clear the cachse after setting the state. Defaults to TRUE.
*/
function admin_only_mode_set($mode, $clearcache=TRUE) {
$mode = $mode ? 1 : 0;
variable_set('admin_only_mode',$mode);
if ($clearcache) {
cache_clear_all();
}
}
/**
* Sets the admin-only message, displayed on auto-logout and the login form.
*
* @param $message
* A string containing the admin-only message.
* @param $clearcache
* Boolean whether to clear the cachse after setting the state. Defaults to TRUE.
*/
function admin_only_mode_set_message($message, $clearcache=TRUE) {
variable_set('admin_only_mode_message',$message);
if ($clearcache) {
cache_clear_all();
}
}