| 1 |
<?php
|
| 2 |
// $Id: password_policy.admin.inc,v 1.10 2009/06/14 08:06:24 deekayen Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Admin page callback file for the password_policy module.
|
| 7 |
*/
|
| 8 |
|
| 9 |
//////////////////////////////////////////////////////////////////////////////
|
| 10 |
// Password policy admin settings
|
| 11 |
|
| 12 |
/**
|
| 13 |
* Settings form display.
|
| 14 |
*/
|
| 15 |
function password_policy_admin_settings() {
|
| 16 |
$form['expiration'] = array(
|
| 17 |
'#type' => 'fieldset',
|
| 18 |
'#title' => t('Expiration settings'),
|
| 19 |
'#collapsible' => TRUE,
|
| 20 |
'#collapsed' => FALSE
|
| 21 |
);
|
| 22 |
$form['expiration']['password_policy_admin'] = array(
|
| 23 |
'#type' => 'checkbox',
|
| 24 |
'#title' => t('Admin (UID=1) password expires.'),
|
| 25 |
'#default_value' => PASSWORD_POLICY_ADMIN,
|
| 26 |
'#description' => t('Admin account password will obey expiration policy.'),
|
| 27 |
);
|
| 28 |
$form['expiration']['password_policy_begin'] = array(
|
| 29 |
'#type' => 'radios',
|
| 30 |
'#title' => t('Beginning of password expirations'),
|
| 31 |
'#default_value' => PASSWORD_POLICY_BEGIN,
|
| 32 |
'#options' => array('0' => t('After expiration time from setting a default policy (all passwords are valid during the expiration time from setting the default policy, and after that older than expiration time passwords expire).'), '1' => t('Setting a default policy (passwords older than expiration time expire after setting the default policy, retroactive behaviour).')),
|
| 33 |
);
|
| 34 |
$form['expiration']['password_policy_block'] = array(
|
| 35 |
'#type' => 'radios',
|
| 36 |
'#title' => t('Blocking expired accounts'),
|
| 37 |
'#default_value' => PASSWORD_POLICY_BLOCK,
|
| 38 |
'#options' => array('0' => t('Expired accounts are blocked. Only administrators can unblock them.'), '1' => t('The user with expired account is not blocked, but sent to a change password page. If the password is not changed, the account is blocked and the user cannot login again.')),
|
| 39 |
);
|
| 40 |
|
| 41 |
// Visibility
|
| 42 |
$form['visibility'] = array(
|
| 43 |
'#type' => 'fieldset',
|
| 44 |
'#title' => t('Visibility settings'),
|
| 45 |
'#collapsible' => TRUE,
|
| 46 |
'#collapsed' => FALSE
|
| 47 |
);
|
| 48 |
$form['visibility']['password_policy_show_restrictions'] = array(
|
| 49 |
'#type' => 'checkbox',
|
| 50 |
'#title' => t('Show restrictions on password change page.'),
|
| 51 |
'#default_value' => PASSWORD_POLICY_SHOW_RESTRICTIONS,
|
| 52 |
'#description' => t('Should password restrictions be listed on the password change page. A javascript warning block will be shown anyways if ithe typed in password does not meet the restrictions.'),
|
| 53 |
);
|
| 54 |
|
| 55 |
// E-mail notification settings.
|
| 56 |
$form['email'] = array(
|
| 57 |
'#type' => 'fieldset',
|
| 58 |
'#title' => t('E-mail notification settings'),
|
| 59 |
'#collapsible' => TRUE,
|
| 60 |
'#collapsed' => TRUE
|
| 61 |
);
|
| 62 |
$form['email']['password_policy_warning_subject'] = array(
|
| 63 |
'#type' => 'textfield',
|
| 64 |
'#title' => t('Subject of warning e-mail'),
|
| 65 |
'#default_value' => _password_policy_mail_text('warning_subject'),
|
| 66 |
'#maxlength' => 180,
|
| 67 |
'#description' => t('Customize the subject of the warning e-mail message, which is sent to remind of password expiration.') .' '. t('Available variables are:') .' !username, !site, !uri, !uri_brief, !mailto, !date, !login_uri, !edit_uri, !days_left.',
|
| 68 |
);
|
| 69 |
$form['email']['password_policy_warning_body'] = array(
|
| 70 |
'#type' => 'textarea',
|
| 71 |
'#title' => t('Body of warning e-mail'),
|
| 72 |
'#default_value' => _password_policy_mail_text('warning_body'),
|
| 73 |
'#rows' => 15,
|
| 74 |
'#description' => t('Customize the body of the warning e-mail message, which is sent to remind of password expiration.') .' '. t('Available variables are:') .' !username, !site, !uri, !uri_brief, !mailto, !date, !login_uri, !edit_uri, !days_left.',
|
| 75 |
);
|
| 76 |
|
| 77 |
$form['submit'] = array(
|
| 78 |
'#type' => 'submit',
|
| 79 |
'#value' => t('Save configuration'),
|
| 80 |
);
|
| 81 |
$form['reset'] = array(
|
| 82 |
'#type' => 'submit',
|
| 83 |
'#value' => t('Reset to defaults'),
|
| 84 |
);
|
| 85 |
|
| 86 |
return $form;
|
| 87 |
}
|
| 88 |
|
| 89 |
/**
|
| 90 |
* Submit hook for the settings form.
|
| 91 |
*/
|
| 92 |
function password_policy_admin_settings_submit($form, &$form_state) {
|
| 93 |
$op = $form_state['clicked_button']['#value'];
|
| 94 |
switch ($op) {
|
| 95 |
case t('Save configuration'):
|
| 96 |
variable_set('password_policy_admin', $form_state['values']['password_policy_admin']);
|
| 97 |
variable_set('password_policy_begin', $form_state['values']['password_policy_begin']);
|
| 98 |
variable_set('password_policy_block', $form_state['values']['password_policy_block']);
|
| 99 |
variable_set('password_policy_show_restrictions', $form_state['values']['password_policy_show_restrictions']);
|
| 100 |
variable_set('password_policy_warning_subject', $form_state['values']['password_policy_warning_subject']);
|
| 101 |
variable_set('password_policy_warning_body', $form_state['values']['password_policy_warning_body']);
|
| 102 |
drupal_set_message(t('The configuration options have been saved.'));
|
| 103 |
break;
|
| 104 |
case t('Reset to defaults'):
|
| 105 |
variable_del('password_policy_admin');
|
| 106 |
variable_del('password_policy_begin');
|
| 107 |
variable_del('password_policy_block');
|
| 108 |
variable_del('password_policy_show_restrictions');
|
| 109 |
variable_del('password_policy_warning_subject');
|
| 110 |
variable_del('password_policy_warning_body');
|
| 111 |
drupal_set_message(t('The configuration options have been reset to their default values.'));
|
| 112 |
break;
|
| 113 |
}
|
| 114 |
}
|
| 115 |
|
| 116 |
/**
|
| 117 |
* The list of the password policies.
|
| 118 |
*/
|
| 119 |
function password_policy_admin_list() {
|
| 120 |
$header = array(t('Default'), array('data' => t('Name'), 'field' => 'name', 'sort' => 'asc'), t('Enabled'), array('data' => t('Operations'), 'colspan' => 3));
|
| 121 |
$form = array();
|
| 122 |
$options = array();
|
| 123 |
$result = db_query('SELECT pid, name, enabled, description, created FROM {password_policy}'. tablesort_sql($header));
|
| 124 |
while ($row = db_fetch_array($result)) {
|
| 125 |
$pid = $row['pid'];
|
| 126 |
$options[$pid] = '';
|
| 127 |
if ($row['enabled']) {
|
| 128 |
$default_pid = $pid;
|
| 129 |
$form[$pid]['created'] = array('#value' => format_date($row['created'], 'medium'));
|
| 130 |
}
|
| 131 |
$form[$pid]['name'] = array('#value' => $row['name']);
|
| 132 |
$form[$pid]['view'] = array('#value' => l(t('view'), 'admin/settings/password_policy/'. $pid));
|
| 133 |
$form[$pid]['edit'] = array('#value' => l(t('edit'), 'admin/settings/password_policy/'. $pid .'/edit'));
|
| 134 |
$form[$pid]['delete'] = array('#value' => l(t('delete'), 'admin/settings/password_policy/delete/'. $pid));
|
| 135 |
}
|
| 136 |
$form['default'] = array('#type' => 'radios', '#options' => $options, '#default_value' => isset($default_pid) ? $default_pid : 0);
|
| 137 |
$form['submit'] = array('#type' => 'submit', '#value' => t('Set default policy'));
|
| 138 |
$form['clear'] = array('#type' => 'submit', '#value' => t('Clear default policy'));
|
| 139 |
return $form;
|
| 140 |
}
|
| 141 |
|
| 142 |
/**
|
| 143 |
* Submit hook for the form on the default list view for the password policy module. From the
|
| 144 |
* default view, the user can set a new default password policy or clear the default so
|
| 145 |
* that no policy is active and the default drupal password mechanism takes affect.
|
| 146 |
*/
|
| 147 |
function password_policy_admin_list_submit($form, &$form_state) {
|
| 148 |
switch ($form_state['clicked_button']['#value']) {
|
| 149 |
case "Clear default policy":
|
| 150 |
_password_policy_admin_clear_default();
|
| 151 |
drupal_set_message(t('No policy is active, all user passwords will be accepted (Drupal default).'));
|
| 152 |
break;
|
| 153 |
case "Set default policy":
|
| 154 |
$pid = $form_state['values']['default'];
|
| 155 |
if (is_numeric($pid)) {
|
| 156 |
$policy = _password_policy_load_policy_by_pid($pid);
|
| 157 |
if ($policy) {
|
| 158 |
_password_policy_admin_clear_default();
|
| 159 |
$time = time();
|
| 160 |
db_query("UPDATE {password_policy} SET enabled = %d, created = %d WHERE pid = %d", 1, $time, $pid);
|
| 161 |
drupal_set_message(t('%name has been set as the default password policy.', array('%name' => $policy['name'])));
|
| 162 |
}
|
| 163 |
}
|
| 164 |
else {
|
| 165 |
drupal_set_message(t('Default password policy was not set.'), 'warning');
|
| 166 |
}
|
| 167 |
break;
|
| 168 |
}
|
| 169 |
}
|
| 170 |
|
| 171 |
/**
|
| 172 |
* Resets the enabled flag for all policies in the database to 0.
|
| 173 |
*/
|
| 174 |
function _password_policy_admin_clear_default() {
|
| 175 |
db_query("UPDATE {password_policy} SET enabled = %d WHERE enabled = %d", 0, 1);
|
| 176 |
}
|
| 177 |
|
| 178 |
/**
|
| 179 |
* The default view for the password policy.
|
| 180 |
*/
|
| 181 |
function password_policy_admin_view($policy) {
|
| 182 |
$output = check_plain($policy['description']);
|
| 183 |
$header = array(t('Name'), t('Constraint'));
|
| 184 |
$rows = array();
|
| 185 |
if (!empty($policy['expiration'])) {
|
| 186 |
$rows[] = array(t('Expiration'), $policy['expiration']);
|
| 187 |
}
|
| 188 |
if (!empty($policy['warning'])) {
|
| 189 |
$rows[] = array(t('Warning'), $policy['warning']);
|
| 190 |
}
|
| 191 |
foreach ($policy['policy'] as $key => $val) {
|
| 192 |
$desc = _password_policy_constraint_description($key);
|
| 193 |
$rows[] = array($desc['name'], $val);
|
| 194 |
}
|
| 195 |
if (empty($rows)) {
|
| 196 |
$rows[] = array(array('data' => t('No policies defined.'), 'colspan' => 2));
|
| 197 |
}
|
| 198 |
$output .= theme('table', $header, $rows);
|
| 199 |
|
| 200 |
return $output;
|
| 201 |
}
|
| 202 |
|
| 203 |
/**
|
| 204 |
* Form display for new or to be edited password policies.
|
| 205 |
*/
|
| 206 |
function password_policy_admin_form($form_state, $policy = NULL) {
|
| 207 |
$form['policy']['name'] = array(
|
| 208 |
'#type' => 'textfield',
|
| 209 |
'#title' => t('Name'),
|
| 210 |
'#default_value' => $policy['name'],
|
| 211 |
'#maxlength' => 64,
|
| 212 |
'#required' => TRUE,
|
| 213 |
);
|
| 214 |
$form['policy']['description'] = array(
|
| 215 |
'#type' => 'textarea',
|
| 216 |
'#title' => t('Description'),
|
| 217 |
'#default_value' => $policy['description'],
|
| 218 |
);
|
| 219 |
$form['expiration'] = array(
|
| 220 |
'#type' => 'fieldset',
|
| 221 |
'#title' => t('Expiration'),
|
| 222 |
'#collapsible' => TRUE,
|
| 223 |
'#collapsed' => FALSE
|
| 224 |
);
|
| 225 |
$form['expiration']['expiration'] = array('#type' => 'textfield',
|
| 226 |
'#title' => t('Password Expiration'),
|
| 227 |
'#default_value' => $policy['expiration'],
|
| 228 |
'#size' => 5,
|
| 229 |
'#maxlength' => 5,
|
| 230 |
'#description' => t('The passwords will expire after this number of days. The users with expired passwords will be blocked. Leaving this field empty won\'t put any password expiration constraints.'),
|
| 231 |
);
|
| 232 |
$form['expiration']['warning'] = array('#type' => 'textfield',
|
| 233 |
'#title' => t('Password Expiration Warning'),
|
| 234 |
'#default_value' => $policy['warning'],
|
| 235 |
'#size' => 10,
|
| 236 |
'#description' => t('The comma separated list of days. The warning about expiration of the password will be sent out on those days before the expiration. Leaving this field empty won\'t send out or display any warnings.'),
|
| 237 |
);
|
| 238 |
|
| 239 |
$form['constraints'] = array(
|
| 240 |
'#type' => 'fieldset',
|
| 241 |
'#title' => t('Constraints'),
|
| 242 |
'#collapsible' => TRUE,
|
| 243 |
'#collapsed' => FALSE
|
| 244 |
);
|
| 245 |
|
| 246 |
global $_password_policy;
|
| 247 |
foreach ($_password_policy as $constraint) {
|
| 248 |
$desc = _password_policy_constraint_description($constraint);
|
| 249 |
$form['constraints']['constraint_'. $constraint] = array(
|
| 250 |
'#type' => 'textfield',
|
| 251 |
'#size' => 5,
|
| 252 |
'#default_value' => isset($policy['policy'][$constraint]) ? $policy['policy'][$constraint] : NULL,
|
| 253 |
'#maxlength' => 2,
|
| 254 |
'#title' => $desc['name'],
|
| 255 |
'#description' => $desc['description'],
|
| 256 |
);
|
| 257 |
}
|
| 258 |
|
| 259 |
$form['submit'] = array(
|
| 260 |
'#type' => 'submit',
|
| 261 |
'#value' => is_array($policy) ? t('Save') : t('Create'),
|
| 262 |
);
|
| 263 |
|
| 264 |
if ($policy) {
|
| 265 |
$form['delete'] = array(
|
| 266 |
'#type' => 'submit',
|
| 267 |
'#value' => t('Delete'),
|
| 268 |
);
|
| 269 |
$form['pid'] = array(
|
| 270 |
'#type' => 'hidden',
|
| 271 |
'#value' => $policy['pid'],
|
| 272 |
);
|
| 273 |
}
|
| 274 |
|
| 275 |
return $form;
|
| 276 |
}
|
| 277 |
|
| 278 |
/**
|
| 279 |
* Form submission hook for new or edited password policies.
|
| 280 |
*/
|
| 281 |
function password_policy_admin_form_submit($form, &$form_state) {
|
| 282 |
$op = $form_state['values']['op'];
|
| 283 |
|
| 284 |
if ($op == t('Delete')) {
|
| 285 |
drupal_goto('admin/settings/password_policy/delete/'. $form_state['values']['pid']);
|
| 286 |
}
|
| 287 |
|
| 288 |
$policy = array();
|
| 289 |
foreach ($form_state['values'] as $key => $value) {
|
| 290 |
// If we have no form value, then we have no constraint to set.
|
| 291 |
$value = trim($value);
|
| 292 |
if ($value != "" && preg_match("/^constraint_/", $key)) {
|
| 293 |
$policy[substr($key, 11)] = $value;
|
| 294 |
}
|
| 295 |
}
|
| 296 |
|
| 297 |
// If we have an pid, update, else save.
|
| 298 |
if (isset($form_state['values']['pid'])) {
|
| 299 |
db_query("UPDATE {password_policy} SET name = '%s', description = '%s', policy = '%s', expiration = %d, warning = '%s' WHERE pid = %d", $form_state['values']['name'], $form_state['values']['description'], serialize($policy), trim($form_state['values']['expiration']), str_replace(' ', '', $form_state['values']['warning']), $form_state['values']['pid']);
|
| 300 |
drupal_set_message(t('Policy %name has been updated.', array('%name' => $form_state['values']['name'])));
|
| 301 |
watchdog('password_policy', 'Policy %name updated.', array('%name' => $form_state['values']['name']), WATCHDOG_NOTICE, l(t('edit'), 'admin/settings/password_policy/'. $form_state['values']['pid'] .'/edit'));
|
| 302 |
}
|
| 303 |
else {
|
| 304 |
db_query("INSERT INTO {password_policy} (name, description, enabled, policy, expiration, warning) VALUES ('%s', '%s', %d, '%s', %d, '%s')", $form_state['values']['name'], $form_state['values']['description'], 0, serialize($policy), trim($form_state['values']['expiration']), str_replace(' ', '', $form_state['values']['warning']));
|
| 305 |
$pid = db_last_insert_id('password_policy', 'pid');
|
| 306 |
drupal_set_message(t('Policy %name has been created.', array('%name' => $form_state['values']['name'])));
|
| 307 |
watchdog('password_policy', 'New policy %name created.', array('%name' => $form_state['values']['name']), WATCHDOG_NOTICE, l(t('edit'), 'admin/settings/password_policy/'. $pid .'/edit'));
|
| 308 |
}
|
| 309 |
|
| 310 |
drupal_goto('admin/settings/password_policy/list');
|
| 311 |
}
|
| 312 |
|
| 313 |
/**
|
| 314 |
* Confirmation form for the deletion of a password policy. Deletion takes place
|
| 315 |
* in password_policy_admin_delete_submit().
|
| 316 |
*/
|
| 317 |
function password_policy_admin_delete($form_state, $pid) {
|
| 318 |
if (is_numeric($pid) && $policy = _password_policy_load_policy_by_pid($pid)) {
|
| 319 |
$form['pid'] = array('#type' => 'hidden', '#value' => $policy['pid']);
|
| 320 |
|
| 321 |
return confirm_form($form, t('Are you sure you want to delete the policy %name?', array('%name' => $policy['name'])), 'admin/settings/password_policy/list', t('This action cannot be undone.'), t('Delete'), t('Cancel'));
|
| 322 |
}
|
| 323 |
else {
|
| 324 |
drupal_not_found();
|
| 325 |
exit;
|
| 326 |
}
|
| 327 |
}
|
| 328 |
|
| 329 |
/**
|
| 330 |
* Submit hook for the delete policy operation.
|
| 331 |
*/
|
| 332 |
function password_policy_admin_delete_submit($form, &$form_state) {
|
| 333 |
$pid = $form_state['values']['pid'];
|
| 334 |
$policy = _password_policy_load_policy_by_pid($pid);
|
| 335 |
|
| 336 |
db_query("DELETE FROM {password_policy} WHERE pid = %d", $pid);
|
| 337 |
drupal_set_message(t('Password policy %policy was deleted.', array('%policy' => $policy['name'])));
|
| 338 |
watchdog('password_policy', 'Policy %name was deleted.', array('%name' => $policy['name']), WATCHDOG_NOTICE);
|
| 339 |
drupal_goto('admin/settings/password_policy/list');
|
| 340 |
}
|
| 341 |
|