Simpletest Coverage - modules/user/user.admin.inc

1 <?php
2 // $Id: user.admin.inc,v 1.67 2009/08/11 11:47:58 dries Exp $
3
4 /**
5 * @file
6 * Admin page callback file for the user module.
7 */
8
9 function user_admin($callback_arg = '') {
10 $op = isset($_POST['op']) ? $_POST['op'] : $callback_arg;
11
12 switch ($op) {
13 case t('Create new account'):
14 case 'create':
15 $build['user_register'] = drupal_get_form('user_register');
16 break;
17 default:
18 if (!empty($_POST['accounts']) && isset($_POST['operation']) && ($_POST['operation'] == 'cancel')) {
19 $build['user_multiple_cancel_confirm'] = drupal_get_form('user_multiple_cancel_confirm');
20 }
21 else {
22 $build['user_filter_form'] = drupal_get_form('user_filter_form');
23 $build['user_admin_account'] = drupal_get_form('user_admin_account');
24 }
25 }
26 return $build;
27 }
28
29 /**
30 * Form builder; Return form for user administration filters.
31 *
32 * @ingroup forms
33 * @see user_filter_form_submit()
34 */
35 function user_filter_form() {
36 $session = isset($_SESSION['user_overview_filter']) ? $_SESSION['user_overview_filter'] : array();
37 $filters = user_filters();
38
39 $i = 0;
40 $form['filters'] = array(
41 '#type' => 'fieldset',
42 '#title' => t('Show only users where'),
43 '#theme' => 'user_filters',
44 );
45 foreach ($session as $filter) {
46 list($type, $value) = $filter;
47 // Merge an array of arrays into one if necessary.
48 $options = $type == 'permission' ? call_user_func_array('array_merge', $filters[$type]['options']) : $filters[$type]['options'];
49 $params = array('%property' => $filters[$type]['title'] , '%value' => $options[$value]);
50 if ($i++ > 0) {
51 $form['filters']['current'][] = array('#markup' => t('<em>and</em> where <strong>%property</strong> is <strong>%value</strong>', $params));
52 }
53 else {
54 $form['filters']['current'][] = array('#markup' => t('<strong>%property</strong> is <strong>%value</strong>', $params));
55 }
56 }
57
58 foreach ($filters as $key => $filter) {
59 $names[$key] = $filter['title'];
60 $form['filters']['status'][$key] = array(
61 '#type' => 'select',
62 '#options' => $filter['options'],
63 );
64 }
65
66 $form['filters']['filter'] = array(
67 '#type' => 'radios',
68 '#options' => $names,
69 );
70 $form['filters']['buttons']['submit'] = array(
71 '#type' => 'submit',
72 '#value' => (count($session) ? t('Refine') : t('Filter')),
73 );
74 if (count($session)) {
75 $form['filters']['buttons']['undo'] = array(
76 '#type' => 'submit',
77 '#value' => t('Undo'),
78 );
79 $form['filters']['buttons']['reset'] = array(
80 '#type' => 'submit',
81 '#value' => t('Reset'),
82 );
83 }
84
85 drupal_add_js('misc/form.js');
86
87 return $form;
88 }
89
90 /**
91 * Process result from user administration filter form.
92 */
93 function user_filter_form_submit($form, &$form_state) {
94 $op = $form_state['values']['op'];
95 $filters = user_filters();
96 switch ($op) {
97 case t('Filter'): case t('Refine'):
98 if (isset($form_state['values']['filter'])) {
99 $filter = $form_state['values']['filter'];
100 // Merge an array of arrays into one if necessary.
101 $options = $filter == 'permission' ? call_user_func_array('array_merge', $filters[$filter]['options']) : $filters[$filter]['options'];
102 if (isset($options[$form_state['values'][$filter]])) {
103 $_SESSION['user_overview_filter'][] = array($filter, $form_state['values'][$filter]);
104 }
105 }
106 break;
107 case t('Undo'):
108 array_pop($_SESSION['user_overview_filter']);
109 break;
110 case t('Reset'):
111 $_SESSION['user_overview_filter'] = array();
112 break;
113 case t('Update'):
114 return;
115 }
116
117 $form_state['redirect'] = 'admin/people';
118 return;
119 }
120
121 /**
122 * Form builder; User administration page.
123 *
124 * @ingroup forms
125 * @see user_admin_account_validate()
126 * @see user_admin_account_submit()
127 */
128 function user_admin_account() {
129
130 $header = array(
131 array(),
132 array('data' => t('Username'), 'field' => 'u.name'),
133 array('data' => t('Status'), 'field' => 'u.status'),
134 t('Roles'),
135 array('data' => t('Member for'), 'field' => 'u.created', 'sort' => 'desc'),
136 array('data' => t('Last access'), 'field' => 'u.access'),
137 t('Operations')
138 );
139
140 $query = db_select('users', 'u');
141 $query->leftJoin('users_roles', 'ur', 'u.uid = ur.uid');
142 $query->condition('u.uid', 0, '<>');
143 user_build_filter_query($query);
144
145 $count_query = clone $query;
146 $count_query->addExpression('COUNT(DISTINCT u.uid)');
147
148 $query = $query->extend('PagerDefault')->extend('TableSort');
149 $query
150 ->fields('u', array('uid', 'name', 'status', 'created', 'access'))
151 ->limit(50)
152 ->orderByHeader($header)
153 ->setCountQuery($count_query);
154 $result = $query->execute();
155
156 $form['options'] = array(
157 '#type' => 'fieldset',
158 '#title' => t('Update options'),
159 '#prefix' => '<div class="container-inline">',
160 '#suffix' => '</div>',
161 );
162 $options = array();
163 foreach (module_invoke_all('user_operations') as $operation => $array) {
164 $options[$operation] = $array['label'];
165 }
166 $form['options']['operation'] = array(
167 '#type' => 'select',
168 '#options' => $options,
169 '#default_value' => 'unblock',
170 );
171 $form['options']['submit'] = array(
172 '#type' => 'submit',
173 '#value' => t('Update'),
174 );
175
176 $destination = drupal_get_destination();
177
178 $status = array(t('blocked'), t('active'));
179 $roles = user_roles(TRUE);
180 $accounts = array();
181 foreach ($result as $account) {
182 $accounts[$account->uid] = '';
183 $form['name'][$account->uid] = array('#markup' => theme('username', $account));
184 $form['status'][$account->uid] = array('#markup' => $status[$account->status]);
185 $users_roles = array();
186 $roles_result = db_query('SELECT rid FROM {users_roles} WHERE uid = :uid', array(':uid' => $account->uid));
187 foreach ($roles_result as $user_role) {
188 $users_roles[] = $roles[$user_role->rid];
189 }
190 asort($users_roles);
191 $form['roles'][$account->uid][0] = array('#markup' => theme('item_list', $users_roles));
192 $form['member_for'][$account->uid] = array('#markup' => format_interval(REQUEST_TIME - $account->created));
193 $form['last_access'][$account->uid] = array('#markup' => $account->access ? t('@time ago', array('@time' => format_interval(REQUEST_TIME - $account->access))) : t('never'));
194 $form['operations'][$account->uid] = array('#markup' => l(t('edit'), "user/$account->uid/edit", array('query' => $destination)));
195 }
196 $form['accounts'] = array(
197 '#type' => 'checkboxes',
198 '#options' => $accounts
199 );
200 $form['pager'] = array('#markup' => theme('pager', NULL));
201
202 return $form;
203 }
204
205 /**
206 * Submit the user administration update form.
207 */
208 function user_admin_account_submit($form, &$form_state) {
209 $operations = module_invoke_all('user_operations', $form_state);
210 $operation = $operations[$form_state['values']['operation']];
211 // Filter out unchecked accounts.
212 $accounts = array_filter($form_state['values']['accounts']);
213 if ($function = $operation['callback']) {
214 // Add in callback arguments if present.
215 if (isset($operation['callback arguments'])) {
216 $args = array_merge(array($accounts), $operation['callback arguments']);
217 }
218 else {
219 $args = array($accounts);
220 }
221 call_user_func_array($function, $args);
222
223 drupal_set_message(t('The update has been performed.'));
224 }
225 }
226
227 function user_admin_account_validate($form, &$form_state) {
228 $form_state['values']['accounts'] = array_filter($form_state['values']['accounts']);
229 if (count($form_state['values']['accounts']) == 0) {
230 form_set_error('', t('No users selected.'));
231 }
232 }
233
234 /**
235 * Form builder; Configure user settings for this site.
236 *
237 * @ingroup forms
238 * @see system_settings_form()
239 */
240 function user_admin_settings() {
241 // Settings for anonymous users.
242 $form['anonymous_settings'] = array(
243 '#type' => 'fieldset',
244 '#title' => t('Anonymous users'),
245 );
246 $form['anonymous_settings']['anonymous'] = array(
247 '#type' => 'textfield',
248 '#title' => t('Name'),
249 '#default_value' => variable_get('anonymous', t('Anonymous')),
250 '#description' => t('The name used to indicate anonymous users.'),
251 '#required' => TRUE,
252 );
253
254 // Administrative role option.
255 $form['admin_role'] = array(
256 '#type' => 'fieldset',
257 '#title' => t('Administrator role'),
258 );
259
260 // Do not allow users to set the anonymous or authenticated user roles as the
261 // administrator role.
262 $roles = user_roles();
263 unset($roles[DRUPAL_ANONYMOUS_RID]);
264 unset($roles[DRUPAL_AUTHENTICATED_RID]);
265 $roles[0] = t('disabled');
266
267 $form['admin_role']['user_admin_role'] = array(
268 '#type' => 'select',
269 '#title' => t('Administrator role'),
270 '#default_value' => variable_get('user_admin_role', 0),
271 '#options' => $roles,
272 '#description' => t('This role will be automatically assigned new permissions whenever a module is enabled. Changing this setting will not affect existing permissions.'),
273 );
274
275 // User registration settings.
276 $form['registration_cancellation'] = array(
277 '#type' => 'fieldset',
278 '#title' => t('Registration and cancellation'),
279 );
280 $form['registration_cancellation']['user_register'] = array(
281 '#type' => 'radios',
282 '#title' => t('Who can register accounts?'),
283 '#default_value' => variable_get('user_register', 1),
284 '#options' => array(
285 t('Administrators only'),
286 t('Visitors'),
287 t('Visitors, but administrator approval is required'),
288 )
289 );
290 $form['registration_cancellation']['user_email_verification'] = array(
291 '#type' => 'checkbox',
292 '#title' => t('Require e-mail verification when a visitor creates an account.'),
293 '#default_value' => variable_get('user_email_verification', TRUE),
294 '#description' => t('New users will be required to validate their e-mail address prior to logging into the site, and will be assigned a system-generated password. If disabled, users will be logged in immediately upon registering, and may select their own passwords during registration.')
295 );
296 module_load_include('inc', 'user', 'user.pages');
297 $form['registration_cancellation']['user_cancel_method'] = array(
298 '#type' => 'item',
299 '#title' => t('When cancelling a user account'),
300 '#description' => t('Users with the %select-cancel-method or %administer-users <a href="@permissions-url">permissions</a> can override this default method.', array('%select-cancel-method' => t('Select method for cancelling account'), '%administer-users' => t('Administer users'), '@permissions-url' => url('admin/settings/permissions'))),
301 );
302 $form['registration_cancellation']['user_cancel_method'] += user_cancel_methods();
303 foreach (element_children($form['registration_cancellation']['user_cancel_method']) as $element) {
304 // Remove all account cancellation methods that have #access defined, as
305 // those cannot be configured as default method.
306 if (isset($form['registration_cancellation']['user_cancel_method'][$element]['#access'])) {
307 $form['registration_cancellation']['user_cancel_method'][$element]['#access'] = FALSE;
308 }
309 // Remove the description (only displayed on the confirmation form).
310 else {
311 unset($form['registration_cancellation']['user_cancel_method'][$element]['#description']);
312 }
313 }
314
315 // Account settings.
316 $form['personalization'] = array(
317 '#type' => 'fieldset',
318 '#title' => t('Personalization'),
319 );
320 $form['personalization']['user_signatures'] = array(
321 '#type' => 'checkbox',
322 '#title' => t('Enable signatures.'),
323 '#default_value' => variable_get('user_signatures', 0),
324 );
325 // If picture support is enabled, check whether the picture directory exists.
326 if (variable_get('user_pictures', 0)) {
327 $picture_path = file_create_path(variable_get('user_picture_path', 'pictures'));
328 file_check_directory($picture_path, FILE_CREATE_DIRECTORY, 'user_picture_path');
329 }
330 $picture_support = variable_get('user_pictures', 0);
331 $form['personalization']['user_pictures'] = array(
332 '#type' => 'checkbox',
333 '#title' => t('Enable user pictures.'),
334 '#default_value' => $picture_support,
335 );
336 drupal_add_js(drupal_get_path('module', 'user') . '/user.js');
337 // If JS is enabled, and the checkbox defaults to off, hide all the settings
338 // on page load via CSS using the js-hide class so there's no flicker.
339 $css_class = 'user-admin-picture-settings';
340 if (!$picture_support) {
341 $css_class .= ' js-hide';
342 }
343 $form['personalization']['pictures'] = array(
344 '#prefix' => '<div class="' . $css_class . '">',
345 '#suffix' => '</div>',
346 );
347 $form['personalization']['pictures']['user_picture_path'] = array(
348 '#type' => 'textfield',
349 '#title' => t('Picture directory'),
350 '#default_value' => variable_get('user_picture_path', 'pictures'),
351 '#size' => 30,
352 '#maxlength' => 255,
353 '#description' => t('Subdirectory in the directory %dir where pictures will be stored.', array('%dir' => file_directory_path() . '/')),
354 );
355 $form['personalization']['pictures']['user_picture_default'] = array(
356 '#type' => 'textfield',
357 '#title' => t('Default picture'),
358 '#default_value' => variable_get('user_picture_default', ''),
359 '#size' => 30,
360 '#maxlength' => 255,
361 '#description' => t('URL of picture to display for users with no custom picture selected. Leave blank for none.'),
362 );
363 if (module_exists('image')) {
364 $form['personalization']['pictures']['settings']['user_picture_style'] = array(
365 '#type' => 'select',
366 '#title' => t('Picture display style'),
367 '#options' => image_style_options(TRUE),
368 '#default_value' => variable_get('user_picture_style', ''),
369 '#description' => t('The style selected will be used on display, while the original image is retained. Styles may be configured in the <a href="!url">Image styles</a> administration area.', array('!url' => url('admin/settings/image-styles'))),
370 );
371 }
372 $form['personalization']['pictures']['user_picture_dimensions'] = array(
373 '#type' => 'textfield',
374 '#title' => t('Picture upload dimensions'),
375 '#default_value' => variable_get('user_picture_dimensions', '85x85'),
376 '#size' => 10,
377 '#maxlength' => 10,
378 '#field_suffix' => ' ' . t('pixels'),
379 '#description' => t('Maximum allowed dimensions for uploaded pictures.'),
380 );
381 $form['personalization']['pictures']['user_picture_file_size'] = array(
382 '#type' => 'textfield',
383 '#title' => t('Picture upload file size'),
384 '#default_value' => variable_get('user_picture_file_size', '30'),
385 '#size' => 10,
386 '#maxlength' => 10,
387 '#field_suffix' => ' ' . t('KB'),
388 '#description' => t('Maximum allowed file size for uploaded pictures.'),
389 );
390 $form['personalization']['pictures']['user_picture_guidelines'] = array(
391 '#type' => 'textarea',
392 '#title' => t('Picture guidelines'),
393 '#default_value' => variable_get('user_picture_guidelines', ''),
394 '#description' => t("This text is displayed at the picture upload form in addition to the default guidelines. It's useful for helping or instructing your users."),
395 );
396
397 $form['email_title'] = array(
398 '#type' => 'item',
399 '#title' => t('E-mails'),
400 );
401 $form['email'] = array(
402 '#type' => 'vertical_tabs',
403 );
404 // These email tokens are shared for all settings, so just define
405 // the list once to help ensure they stay in sync.
406 $email_token_help = t('Available variables are:') . ' !username, !site, !password, !uri, !uri_brief, !mailto, !date, !login_uri, !edit_uri, !login_url, !cancel_url.';
407
408 $form['email_admin_created'] = array(
409 '#type' => 'fieldset',
410 '#title' => t('Welcome (new user created by administrator)'),
411 '#collapsible' => TRUE,
412 '#collapsed' => (variable_get('user_register', 1) != 0),
413 '#description' => t('Customize welcome e-mail messages sent to new member accounts created by an administrator.') . ' ' . $email_token_help,
414 '#group' => 'email',
415 );
416 $form['email_admin_created']['user_mail_register_admin_created_subject'] = array(
417 '#type' => 'textfield',
418 '#title' => t('Subject'),
419 '#default_value' => _user_mail_text('register_admin_created_subject'),
420 '#maxlength' => 180,
421 );
422 $form['email_admin_created']['user_mail_register_admin_created_body'] = array(
423 '#type' => 'textarea',
424 '#title' => t('Body'),
425 '#default_value' => _user_mail_text('register_admin_created_body'),
426 '#rows' => 15,
427 );
428
429 $form['email_pending_approval'] = array(
430 '#type' => 'fieldset',
431 '#title' => t('Welcome (awaiting approval)'),
432 '#collapsible' => TRUE,
433 '#collapsed' => (variable_get('user_register', 1) != 2),
434 '#description' => t('Customize welcome e-mail messages sent to new members upon registering, when administrative approval is required.') . ' ' . $email_token_help,
435 '#group' => 'email',
436 );
437 $form['email_pending_approval']['user_mail_register_pending_approval_subject'] = array(
438 '#type' => 'textfield',
439 '#title' => t('Subject'),
440 '#default_value' => _user_mail_text('register_pending_approval_subject'),
441 '#maxlength' => 180,
442 );
443 $form['email_pending_approval']['user_mail_register_pending_approval_body'] = array(
444 '#type' => 'textarea',
445 '#title' => t('Body'),
446 '#default_value' => _user_mail_text('register_pending_approval_body'),
447 '#rows' => 8,
448 );
449
450 $form['email_no_approval_required'] = array(
451 '#type' => 'fieldset',
452 '#title' => t('Welcome (no approval required)'),
453 '#collapsible' => TRUE,
454 '#collapsed' => (variable_get('user_register', 1) != 1),
455 '#description' => t('Customize welcome e-mail messages sent to new members upon registering, when no administrator approval is required.') . ' ' . $email_token_help,
456 '#group' => 'email',
457 );
458 $form['email_no_approval_required']['user_mail_register_no_approval_required_subject'] = array(
459 '#type' => 'textfield',
460 '#title' => t('Subject'),
461 '#default_value' => _user_mail_text('register_no_approval_required_subject'),
462 '#maxlength' => 180,
463 );
464 $form['email_no_approval_required']['user_mail_register_no_approval_required_body'] = array(
465 '#type' => 'textarea',
466 '#title' => t('Body'),
467 '#default_value' => _user_mail_text('register_no_approval_required_body'),
468 '#rows' => 15,
469 );
470
471 $form['email_password_reset'] = array(
472 '#type' => 'fieldset',
473 '#title' => t('Password recovery'),
474 '#collapsible' => TRUE,
475 '#collapsed' => TRUE,
476 '#description' => t('Customize e-mail messages sent to users who request a new password.') . ' ' . $email_token_help,
477 '#group' => 'email',
478 '#weight' => 10,
479 );
480 $form['email_password_reset']['user_mail_password_reset_subject'] = array(
481 '#type' => 'textfield',
482 '#title' => t('Subject'),
483 '#default_value' => _user_mail_text('password_reset_subject'),
484 '#maxlength' => 180,
485 );
486 $form['email_password_reset']['user_mail_password_reset_body'] = array(
487 '#type' => 'textarea',
488 '#title' => t('Body'),
489 '#default_value' => _user_mail_text('password_reset_body'),
490 '#rows' => 12,
491 );
492
493 $form['email_activated'] = array(
494 '#type' => 'fieldset',
495 '#title' => t('Account activation'),
496 '#collapsible' => TRUE,
497 '#collapsed' => TRUE,
498 '#description' => t('Enable and customize e-mail messages sent to users upon account activation (when an administrator activates an account of a user who has already registered, on a site where administrative approval is required).') . ' ' . $email_token_help,
499 '#group' => 'email',
500 );
501 $form['email_activated']['user_mail_status_activated_notify'] = array(
502 '#type' => 'checkbox',
503 '#title' => t('Notify user when account is activated.'),
504 '#default_value' => variable_get('user_mail_status_activated_notify', TRUE),
505 );
506 $form['email_activated']['user_mail_status_activated_subject'] = array(
507 '#type' => 'textfield',
508 '#title' => t('Subject'),
509 '#default_value' => _user_mail_text('status_activated_subject'),
510 '#maxlength' => 180,
511 );
512 $form['email_activated']['user_mail_status_activated_body'] = array(
513 '#type' => 'textarea',
514 '#title' => t('Body'),
515 '#default_value' => _user_mail_text('status_activated_body'),
516 '#rows' => 15,
517 );
518
519 $form['email_blocked'] = array(
520 '#type' => 'fieldset',
521 '#title' => t('Account blocked'),
522 '#collapsible' => TRUE,
523 '#collapsed' => TRUE,
524 '#description' => t('Enable and customize e-mail messages sent to users when their accounts are blocked.') . ' ' . $email_token_help,
525 '#group' => 'email',
526 );
527 $form['email_blocked']['user_mail_status_blocked_notify'] = array(
528 '#type' => 'checkbox',
529 '#title' => t('Notify user when account is blocked.'),
530 '#default_value' => variable_get('user_mail_status_blocked_notify', FALSE),
531 );
532 $form['email_blocked']['user_mail_status_blocked_subject'] = array(
533 '#type' => 'textfield',
534 '#title' => t('Subject'),
535 '#default_value' => _user_mail_text('status_blocked_subject'),
536 '#maxlength' => 180,
537 );
538 $form['email_blocked']['user_mail_status_blocked_body'] = array(
539 '#type' => 'textarea',
540 '#title' => t('Body'),
541 '#default_value' => _user_mail_text('status_blocked_body'),
542 '#rows' => 3,
543 );
544
545 $form['email_cancel_confirm'] = array(
546 '#type' => 'fieldset',
547 '#title' => t('Account cancellation confirmation'),
548 '#collapsible' => TRUE,
549 '#collapsed' => TRUE,
550 '#description' => t('Customize e-mail messages sent to users when they attempt to cancel their accounts.') . ' ' . $email_token_help,
551 '#group' => 'email',
552 );
553 $form['email_cancel_confirm']['user_mail_cancel_confirm_subject'] = array(
554 '#type' => 'textfield',
555 '#title' => t('Subject'),
556 '#default_value' => _user_mail_text('cancel_confirm_subject'),
557 '#maxlength' => 180,
558 );
559 $form['email_cancel_confirm']['user_mail_cancel_confirm_body'] = array(
560 '#type' => 'textarea',
561 '#title' => t('Body'),
562 '#default_value' => _user_mail_text('cancel_confirm_body'),
563 '#rows' => 3,
564 );
565
566 $form['email_canceled'] = array(
567 '#type' => 'fieldset',
568 '#title' => t('Account canceled'),
569 '#collapsible' => TRUE,
570 '#collapsed' => TRUE,
571 '#description' => t('Enable and customize e-mail messages sent to users when their accounts are canceled.') . ' ' . $email_token_help,
572 '#group' => 'email',
573 );
574 $form['email_canceled']['user_mail_status_canceled_notify'] = array(
575 '#type' => 'checkbox',
576 '#title' => t('Notify user when account is canceled.'),
577 '#default_value' => variable_get('user_mail_status_canceled_notify', FALSE),
578 );
579 $form['email_canceled']['user_mail_status_canceled_subject'] = array(
580 '#type' => 'textfield',
581 '#title' => t('Subject'),
582 '#default_value' => _user_mail_text('status_canceled_subject'),
583 '#maxlength' => 180,
584 );
585 $form['email_canceled']['user_mail_status_canceled_body'] = array(
586 '#type' => 'textarea',
587 '#title' => t('Body'),
588 '#default_value' => _user_mail_text('status_canceled_body'),
589 '#rows' => 3,
590 );
591
592 return system_settings_form($form, FALSE);
593 }
594
595 /**
596 * Menu callback: administer permissions.
597 *
598 * @ingroup forms
599 * @see user_admin_permissions_submit()
600 * @see theme_user_admin_permissions()
601 */
602 function user_admin_permissions($form_state, $rid = NULL) {
603
604 // Retrieve role names for columns.
605 $role_names = user_roles();
606 if (is_numeric($rid)) {
607 $role_names = array($rid => $role_names[$rid]);
608 }
609 // Fetch permissions for all roles or the one selected role.
610 $role_permissions = user_role_permissions($role_names);
611
612 // Store $role_names for use when saving the data.
613 $form['role_names'] = array(
614 '#type' => 'value',
615 '#value' => $role_names,
616 );
617 // Render role/permission overview:
618 $options = array();
619 $hide_descriptions = !system_admin_compact_mode();
620 foreach (module_implements('permission') as $module) {
621 if ($permissions = module_invoke($module, 'permission')) {
622 $info = drupal_parse_info_file(drupal_get_path('module', $module) . "/$module.info");
623 $form['permission'][] = array(
624 '#markup' => $info['name'],
625 '#id' => $module,
626 );
627 foreach ($permissions as $perm => $perm_item) {
628 $options[$perm] = '';
629 $form['permission'][$perm] = array(
630 '#type' => 'item',
631 '#markup' => $perm_item['title'],
632 '#description' => $hide_descriptions ? $perm_item['description'] : NULL,
633 );
634 foreach ($role_names as $rid => $name) {
635 // Builds arrays for checked boxes for each role
636 if (isset($role_permissions[$rid][$perm])) {
637 $status[$rid][] = $perm;
638 }
639 }
640 }
641 }
642 }
643
644 // Have to build checkboxes here after checkbox arrays are built
645 foreach ($role_names as $rid => $name) {
646 $form['checkboxes'][$rid] = array('#type' => 'checkboxes', '#options' => $options, '#default_value' => isset($status[$rid]) ? $status[$rid] : array());
647 $form['role_names'][$rid] = array('#markup' => $name, '#tree' => TRUE);
648 }
649 $form['submit'] = array('#type' => 'submit', '#value' => t('Save permissions'));
650
651 $form['#attached_js'] = array(drupal_get_path('module', 'user') . '/user.permissions.js');
652
653 return $form;
654 }
655
656 /**
657 * Save permissions selected on the administer permissions page.
658 *
659 * @see user_admin_permissions()
660 */
661 function user_admin_permissions_submit($form, &$form_state) {
662 foreach ($form_state['values']['role_names'] as $rid => $name) {
663 $checked = array_filter($form_state['values'][$rid]);
664 // Delete existing permissions for the role. This handles "unchecking" checkboxes.
665 db_delete('role_permission')
666 ->condition('rid', $rid)
667 ->execute();
668 $query = db_insert('role_permission')->fields(array('rid', 'permission'));
669 foreach ($checked as $permission) {
670 $query->values(array(
671 'rid' => $rid,
672 'permission' => $permission,
673 ));
674 }
675 $query->execute();
676 }
677
678 drupal_set_message(t('The changes have been saved.'));
679
680 // Clear the cached pages and blocks.
681 cache_clear_all();
682 }
683
684 /**
685 * Theme the administer permissions page.
686 *
687 * @ingroup themeable
688 */
689 function theme_user_admin_permissions($form) {
690 $roles = user_roles();
691 foreach (element_children($form['permission']) as $key) {
692 $row = array();
693 // Module name
694 if (is_numeric($key)) {
695 $row[] = array('data' => drupal_render($form['permission'][$key]), 'class' => 'module', 'id' => 'module-' . $form['permission'][$key]['#id'], 'colspan' => count($form['role_names']['#value']) + 1);
696 }
697 else {
698 // Permission row.
699 $row[] = array(
700 'data' => drupal_render($form['permission'][$key]),
701 'class' => 'permission',
702 );
703 foreach (element_children($form['checkboxes']) as $rid) {
704 $row[] = array('data' => drupal_render($form['checkboxes'][$rid][$key]), 'class' => 'checkbox', 'title' => $roles[$rid] . ' : ' . t($key));
705 }
706 }
707 $rows[] = $row;
708 }
709 $header[] = (t('Permission'));
710 foreach (element_children($form['role_names']) as $rid) {
711 $header[] = array('data' => drupal_render($form['role_names'][$rid]), 'class' => 'checkbox');
712 }
713 $output = theme('system_compact_link');
714 $output .= theme('table', $header, $rows, array('id' => 'permissions'));
715 $output .= drupal_render_children($form);
716 return $output;
717 }
718
719 /**
720 * Menu callback: administer roles.
721 *
722 * @ingroup forms
723 * @see user_admin_role_validate()
724 * @see user_admin_role_submit()
725 * @see theme_user_admin_new_role()
726 */
727 function user_admin_role() {
728 $rid = arg(4);
729 if ($rid) {
730 if ($rid == DRUPAL_ANONYMOUS_RID || $rid == DRUPAL_AUTHENTICATED_RID) {
731 drupal_goto('admin/settings/roles');
732 }
733 // Display the edit role form.
734 $role = db_query('SELECT * FROM {role} WHERE rid = :rid', array(':rid' => $rid))->fetchObject();
735 $form['name'] = array(
736 '#type' => 'textfield',
737 '#title' => t('Role name'),
738 '#default_value' => $role->name,
739 '#size' => 30,
740 '#required' => TRUE,
741 '#maxlength' => 64,
742 '#description' => t('The name for this role. Example: "moderator", "editorial board", "site architect".'),
743 );
744 $form['rid'] = array(
745 '#type' => 'value',
746 '#value' => $rid,
747 );
748 $form['submit'] = array(
749 '#type' => 'submit',
750 '#value' => t('Save role'),
751 );
752 $form['delete'] = array(
753 '#type' => 'submit',
754 '#value' => t('Delete role'),
755 );
756 }
757 else {
758 $form['name'] = array(
759 '#type' => 'textfield',
760 '#size' => 32,
761 '#maxlength' => 64,
762 );
763 $form['submit'] = array(
764 '#type' => 'submit',
765 '#value' => t('Add role'),
766 );
767 $form['#submit'][] = 'user_admin_role_submit';
768 $form['#validate'][] = 'user_admin_role_validate';
769 }
770 return $form;
771 }
772
773 function user_admin_role_validate($form, &$form_state) {
774 if ($form_state['values']['name']) {
775 if ($form_state['values']['op'] == t('Save role')) {
776 $existing_role = (bool) db_query_range("SELECT 1 FROM {role} WHERE name = :name AND rid <> :rid", array(':name' => $form_state['values']['name'], ':rid' => $form_state['values']['rid']), 0, 1)->fetchField();
777 if ($existing_role) {
778 form_set_error('name', t('The role name %name already exists. Please choose another role name.', array('%name' => $form_state['values']['name'])));
779 }
780 }
781 elseif ($form_state['values']['op'] == t('Add role')) {
782 if ((bool) db_query_range('SELECT 1 FROM {role} WHERE name = :name', array(':name' => $form_state['values']['name']), 0, 1)->fetchField()) {
783 form_set_error('name', t('The role name %name already exists. Please choose another role name.', array('%name' => $form_state['values']['name'])));
784 }
785 }
786 }
787 else {
788 form_set_error('name', t('You must specify a valid role name.'));
789 }
790 }
791
792 function user_admin_role_submit($form, &$form_state) {
793 if ($form_state['values']['op'] == t('Save role')) {
794 db_update('role')
795 ->fields(array('name' => $form_state['values']['name']))
796 ->condition('rid', $form_state['values']['rid'])
797 ->execute();
798 drupal_set_message(t('The role has been renamed.'));
799 }
800 elseif ($form_state['values']['op'] == t('Delete role')) {
801 db_delete('role')
802 ->condition('rid', $form_state['values']['rid'])
803 ->execute();
804 db_delete('role_permission')
805 ->condition('rid', $form_state['values']['rid'])
806 ->execute();
807 // Update the users who have this role set:
808 db_delete('users_roles')
809 ->condition('rid', $form_state['values']['rid'])
810 ->execute();
811
812 drupal_set_message(t('The role has been deleted.'));
813 }
814 elseif ($form_state['values']['op'] == t('Add role')) {
815 db_insert('role')
816 ->fields(array('name' => $form_state['values']['name']))
817 ->execute();
818 drupal_set_message(t('The role has been added.'));
819 }
820 $form_state['redirect'] = 'admin/settings/roles';
821 return;
822 }
823
824 /**
825 * Theme user administration overview.
826 *
827 * @ingroup themeable
828 */
829 function theme_user_admin_account($form) {
830 // Overview table:
831 $header = array(
832 theme('table_select_header_cell'),
833 array('data' => t('Username'), 'field' => 'u.name'),
834 array('data' => t('Status'), 'field' => 'u.status'),
835 t('Roles'),
836 array('data' => t('Member for'), 'field' => 'u.created', 'sort' => 'desc'),
837 array('data' => t('Last access'), 'field' => 'u.access'),
838 t('Operations')
839 );
840
841 $output = drupal_render($form['options']);
842 if (!empty($form['name'])) {
843 foreach (element_children($form['name']) as $key) {
844 $rows[] = array(
845 drupal_render($form['accounts'][$key]),
846 drupal_render($form['name'][$key]),
847 drupal_render($form['status'][$key]),
848 drupal_render($form['roles'][$key]),
849 drupal_render($form['member_for'][$key]),
850 drupal_render($form['last_access'][$key]),
851 drupal_render($form['operations'][$key]),
852 );
853 }
854 }
855 else {
856 $rows[] = array(array('data' => t('No users available.'), 'colspan' => '7'));
857 }
858
859 $output .= theme('table', $header, $rows);
860 if ($form['pager']['#markup']) {
861 $output .= drupal_render($form['pager']);
862 }
863
864 $output .= drupal_render_children($form);
865
866 return $output;
867 }
868
869 /**
870 * Theme the new-role form.
871 *
872 * @ingroup themeable
873 */
874 function theme_user_admin_new_role($form) {
875 $header = array(t('Name'), array('data' => t('Operations'), 'colspan' => 2));
876 foreach (user_roles() as $rid => $name) {
877 $edit_permissions = l(t('edit permissions'), 'admin/settings/permissions/' . $rid);
878 if (!in_array($rid, array(DRUPAL_ANONYMOUS_RID, DRUPAL_AUTHENTICATED_RID))) {
879 $rows[] = array($name, l(t('edit role'), 'admin/settings/roles/edit/' . $rid), $edit_permissions);
880 }
881 else {
882 $rows[] = array($name, t('locked'), $edit_permissions);
883 }
884 }
885 $rows[] = array(drupal_render($form['name']), array('data' => drupal_render($form['submit']), 'colspan' => 2));
886
887 $output = drupal_render_children($form);
888 $output .= theme('table', $header, $rows);
889
890 return $output;
891 }
892
893 /**
894 * Theme user administration filter form.
895 *
896 * @ingroup themeable
897 */
898 function theme_user_filter_form($form) {
899 $output = '<div id="user-admin-filter">';
900 $output .= drupal_render($form['filters']);
901 $output .= '</div>';
902 $output .= drupal_render_children($form);
903 return $output;
904 }
905
906 /**
907 * Theme user administration filter selector.
908 *
909 * @ingroup themeable
910 */
911 function theme_user_filters($form) {
912 $output = '<ul class="clearfix">';
913 if (!empty($form['current'])) {
914 foreach (element_children($form['current']) as $key) {
915 $output .= '<li>' . drupal_render($form['current'][$key]) . '</li>';
916 }
917 }
918
919 $output .= '<li><dl class="multiselect">' . (!empty($form['current']) ? '<dt><em>' . t('and') . '</em> ' . t('where') . '</dt>' : '') . '<dd class="a">';
920 foreach (element_children($form['filter']) as $key) {
921 $output .= drupal_render($form['filter'][$key]);
922 }
923 $output .= '</dd>';
924
925 $output .= '<dt>' . t('is') . '</dt><dd class="b">';
926
927 foreach (element_children($form['status']) as $key) {
928 $output .= drupal_render($form['status'][$key]);
929 }
930 $output .= '</dd>';
931
932 $output .= '</dl>';
933 $output .= '<div class="container-inline" id="user-admin-buttons">' . drupal_render($form['buttons']) . '</div>';
934 $output .= '</li></ul>';
935
936 return $output;
937 }
938
939 /**
940 * Implementation of hook_modules_installed().
941 */
942 function user_modules_installed($modules) {
943 // Assign all available permissions to the administrator role.
944 $rid = variable_get('user_admin_role', 0);
945 if ($rid) {
946 foreach ($modules as $module) {
947 if ($permissions = module_invoke($module, 'permission')) {
948 foreach (array_keys($permissions) as $permission) {
949 db_insert('role_permission')
950 ->fields(array(
951 'rid' => $rid,
952 'permission' => $permission,
953 ))->execute();
954 }
955 }
956 }
957 }
958 }
959
960 /**
961 * Implement hook_modules_uninstalled().
962 */
963 function user_modules_uninstalled($modules) {
964 $permissions = array();
965 foreach ($modules as $module) {
966 if (drupal_function_exists($module . '_permission')) {
967 $permissions = array_merge($permissions, array_keys(module_invoke($module, 'permission')));
968 }
969 }
970 if (!empty($permissions)) {
971 db_delete('role_permission')
972 ->condition('permission', $permissions, 'IN')
973 ->execute();
974 }
975 }
976

Legend

Missed
lines code that were not excersized during program execution.
Covered
lines code were excersized during program execution.
Comment/non executable
Comment or non-executable line of code.
Dead
lines of code that according to xdebug could not be executed. This is counted as coverage code because in almost all cases it is code that runnable.