Issue #1479454 by Hugo Wetterberg, galooph, dawehner, andypost, marcingy, heyrocker...
[project/drupal.git] / core / modules / user / user.admin.inc
1 <?php
2
3 /**
4 * @file
5 * Admin page callback file for the user module.
6 */
7
8 /**
9 * Page callback: Generates the appropriate user administration form.
10 *
11 * This function generates the user registration, multiple user cancellation,
12 * or filtered user list admin form, depending on the argument and the POST
13 * form values.
14 *
15 * @param string $callback_arg
16 * (optional) Indicates which form to build. Defaults to '', which will
17 * trigger the user filter form. If the POST value 'op' is present, this
18 * function uses that value as the callback argument.
19 *
20 * @return string
21 * A renderable form array for the respective request.
22 */
23 function user_admin($callback_arg = '') {
24 $op = isset($_POST['op']) ? $_POST['op'] : $callback_arg;
25
26 switch ($op) {
27 case t('Create new account'):
28 case 'create':
29 $account = entity_create('user', array());
30 $build['user_register'] = entity_get_form($account, 'register');
31 break;
32 default:
33 if (!empty($_POST['accounts']) && isset($_POST['operation']) && ($_POST['operation'] == 'cancel')) {
34 $build['user_multiple_cancel_confirm'] = drupal_get_form('user_multiple_cancel_confirm');
35 }
36 else {
37 $build['user_filter_form'] = drupal_get_form('user_filter_form');
38 $build['user_admin_account'] = drupal_get_form('user_admin_account');
39 }
40 }
41 return $build;
42 }
43
44 /**
45 * Form builder; Return form for user administration filters.
46 *
47 * @ingroup forms
48 * @see user_filter_form_submit()
49 */
50 function user_filter_form() {
51 $session = isset($_SESSION['user_overview_filter']) ? $_SESSION['user_overview_filter'] : array();
52 $filters = user_filters();
53
54 $i = 0;
55 $form['filters'] = array(
56 '#type' => 'details',
57 '#title' => t('Show only users where'),
58 '#theme' => 'exposed_filters__user',
59 );
60 foreach ($session as $filter) {
61 list($type, $value) = $filter;
62 if ($type == 'permission') {
63 // Merge arrays of module permissions into one.
64 // Slice past the first element '[any]' whose value is not an array.
65 $options = call_user_func_array('array_merge', array_slice($filters[$type]['options'], 1));
66 $value = $options[$value];
67 }
68 else {
69 $value = $filters[$type]['options'][$value];
70 }
71 $t_args = array('%property' => $filters[$type]['title'], '%value' => $value);
72 if ($i++) {
73 $form['filters']['current'][] = array('#markup' => t('and where %property is %value', $t_args));
74 }
75 else {
76 $form['filters']['current'][] = array('#markup' => t('%property is %value', $t_args));
77 }
78 }
79
80 $form['filters']['status'] = array(
81 '#type' => 'container',
82 '#attributes' => array('class' => array('clearfix')),
83 '#prefix' => ($i ? '<div class="additional-filters">' . t('and where') . '</div>' : ''),
84 );
85 $form['filters']['status']['filters'] = array(
86 '#type' => 'container',
87 '#attributes' => array('class' => array('filters')),
88 );
89 foreach ($filters as $key => $filter) {
90 $form['filters']['status']['filters'][$key] = array(
91 '#type' => 'select',
92 '#options' => $filter['options'],
93 '#title' => $filter['title'],
94 '#default_value' => '[any]',
95 );
96 }
97
98 $form['filters']['status']['actions'] = array(
99 '#type' => 'actions',
100 '#attributes' => array('class' => array('container-inline')),
101 );
102 $form['filters']['status']['actions']['submit'] = array(
103 '#type' => 'submit',
104 '#value' => (count($session) ? t('Refine') : t('Filter')),
105 );
106 if (count($session)) {
107 $form['filters']['status']['actions']['undo'] = array(
108 '#type' => 'submit',
109 '#value' => t('Undo'),
110 );
111 $form['filters']['status']['actions']['reset'] = array(
112 '#type' => 'submit',
113 '#value' => t('Reset'),
114 );
115 }
116
117 drupal_add_library('system', 'drupal.form');
118
119 return $form;
120 }
121
122 /**
123 * Process result from user administration filter form.
124 */
125 function user_filter_form_submit($form, &$form_state) {
126 $op = $form_state['values']['op'];
127 $filters = user_filters();
128 switch ($op) {
129 case t('Filter'):
130 case t('Refine'):
131 // Apply every filter that has a choice selected other than 'any'.
132 foreach ($filters as $filter => $options) {
133 if (isset($form_state['values'][$filter]) && $form_state['values'][$filter] != '[any]') {
134 $_SESSION['user_overview_filter'][] = array($filter, $form_state['values'][$filter]);
135 }
136 }
137 break;
138 case t('Undo'):
139 array_pop($_SESSION['user_overview_filter']);
140 break;
141 case t('Reset'):
142 $_SESSION['user_overview_filter'] = array();
143 break;
144 case t('Update'):
145 return;
146 }
147
148 $form_state['redirect'] = 'admin/people';
149 return;
150 }
151
152 /**
153 * Form builder; User administration page.
154 *
155 * @ingroup forms
156 * @see user_admin_account_validate()
157 * @see user_admin_account_submit()
158 */
159 function user_admin_account() {
160
161 $header = array(
162 'username' => array('data' => t('Username'), 'field' => 'u.name'),
163 'status' => array('data' => t('Status'), 'field' => 'u.status', 'class' => array(RESPONSIVE_PRIORITY_LOW)),
164 'roles' => array('data' => t('Roles'), 'class' => array(RESPONSIVE_PRIORITY_LOW)),
165 'member_for' => array('data' => t('Member for'), 'field' => 'u.created', 'sort' => 'desc', 'class' => array(RESPONSIVE_PRIORITY_LOW)),
166 'access' => array('data' => t('Last access'), 'field' => 'u.access', 'class' => array(RESPONSIVE_PRIORITY_LOW)),
167 'operations' => t('Operations'),
168 );
169
170 $query = db_select('users', 'u');
171 $query->condition('u.uid', 0, '<>');
172 user_build_filter_query($query);
173
174 $count_query = clone $query;
175 $count_query->addExpression('COUNT(u.uid)');
176
177 $query = $query
178 ->extend('Drupal\Core\Database\Query\PagerSelectExtender')
179 ->extend('Drupal\Core\Database\Query\TableSortExtender');
180 $query
181 ->fields('u', array('uid', 'name', 'status', 'created', 'access'))
182 ->limit(50)
183 ->orderByHeader($header)
184 ->setCountQuery($count_query);
185 $result = $query->execute();
186
187 $form['options'] = array(
188 '#type' => 'details',
189 '#title' => t('Update options'),
190 '#attributes' => array('class' => array('container-inline')),
191 );
192 $options = array();
193 foreach (module_invoke_all('user_operations') as $operation => $array) {
194 $options[$operation] = $array['label'];
195 }
196 $form['options']['operation'] = array(
197 '#type' => 'select',
198 '#title' => t('Operation'),
199 '#title_display' => 'invisible',
200 '#options' => $options,
201 '#default_value' => 'unblock',
202 );
203 $options = array();
204 $form['options']['submit'] = array(
205 '#type' => 'submit',
206 '#value' => t('Update'),
207 );
208
209 $destination = drupal_get_destination();
210 $status = array(t('blocked'), t('active'));
211 $roles = array_map('check_plain', user_role_names(TRUE));
212 $accounts = array();
213 foreach ($result as $account) {
214 $account = user_load($account->uid);
215 $users_roles = array();
216 $roles_result = db_query('SELECT rid FROM {users_roles} WHERE uid = :uid', array(':uid' => $account->uid));
217 foreach ($roles_result as $user_role) {
218 $users_roles[] = $roles[$user_role->rid];
219 }
220 asort($users_roles);
221
222 $options[$account->uid] = array(
223 'username' => theme('username', array('account' => $account)),
224 'status' => $status[$account->status],
225 'roles' => theme('item_list', array('items' => $users_roles)),
226 'member_for' => format_interval(REQUEST_TIME - $account->created),
227 'access' => $account->access ? t('@time ago', array('@time' => format_interval(REQUEST_TIME - $account->access))) : t('never'),
228 );
229 $links = array();
230 $links['edit'] = array(
231 'title' => t('edit'),
232 'href' => 'user/' . $account->uid . '/edit',
233 'query' => $destination,
234 );
235 if (module_invoke('translation_entity', 'translate_access', $account)) {
236 $links['translate'] = array(
237 'title' => t('translate'),
238 'href' => 'user/' . $account->uid . '/translations',
239 'query' => $destination,
240 );
241 }
242 $options[$account->uid]['operations']['data'] = array(
243 '#type' => 'operations',
244 '#links' => $links,
245 );
246 }
247
248 $form['accounts'] = array(
249 '#type' => 'tableselect',
250 '#header' => $header,
251 '#options' => $options,
252 '#empty' => t('No people available.'),
253 );
254 $form['pager'] = array('#markup' => theme('pager'));
255
256 return $form;
257 }
258
259 /**
260 * Submit the user administration update form.
261 */
262 function user_admin_account_submit($form, &$form_state) {
263 $operations = module_invoke_all('user_operations', $form, $form_state);
264 $operation = $operations[$form_state['values']['operation']];
265 // Filter out unchecked accounts.
266 $accounts = array_filter($form_state['values']['accounts']);
267 if ($function = $operation['callback']) {
268 // Add in callback arguments if present.
269 if (isset($operation['callback arguments'])) {
270 $args = array_merge(array($accounts), $operation['callback arguments']);
271 }
272 else {
273 $args = array($accounts);
274 }
275 call_user_func_array($function, $args);
276
277 drupal_set_message(t('The update has been performed.'));
278 }
279 }
280
281 function user_admin_account_validate($form, &$form_state) {
282 $form_state['values']['accounts'] = array_filter($form_state['values']['accounts']);
283 if (count($form_state['values']['accounts']) == 0) {
284 form_set_error('', t('No users selected.'));
285 }
286 }
287
288 /**
289 * Form builder; Configure user settings for this site.
290 *
291 * @ingroup forms
292 * @see user_admin_settings_submit()
293 */
294 function user_admin_settings($form, &$form_state) {
295 $config = config('user.settings');
296 $mail_config = config('user.mail');
297
298 // Settings for anonymous users.
299 $form['anonymous_settings'] = array(
300 '#type' => 'details',
301 '#title' => t('Anonymous users'),
302 );
303 $form['anonymous_settings']['anonymous'] = array(
304 '#type' => 'textfield',
305 '#title' => t('Name'),
306 '#default_value' => $config->get('anonymous'),
307 '#description' => t('The name used to indicate anonymous users.'),
308 '#required' => TRUE,
309 );
310
311 // Administrative role option.
312 $form['admin_role'] = array(
313 '#type' => 'details',
314 '#title' => t('Administrator role'),
315 );
316
317 // Do not allow users to set the anonymous or authenticated user roles as the
318 // administrator role.
319 $roles = user_role_names();
320 unset($roles[DRUPAL_ANONYMOUS_RID]);
321 unset($roles[DRUPAL_AUTHENTICATED_RID]);
322 $roles[0] = t('disabled');
323
324 $form['admin_role']['user_admin_role'] = array(
325 '#type' => 'select',
326 '#title' => t('Administrator role'),
327 '#default_value' => $config->get('admin_role'),
328 '#options' => $roles,
329 '#description' => t('This role will be automatically assigned new permissions whenever a module is enabled. Changing this setting will not affect existing permissions.'),
330 );
331
332 // @todo Remove this check once language settings are generalized.
333 if (module_exists('translation_entity')) {
334 $form['language'] = array(
335 '#type' => 'details',
336 '#title' => t('Language settings'),
337 '#tree' => TRUE,
338 );
339 $form_state['translation_entity']['key'] = 'language';
340 $form['language'] += translation_entity_enable_widget('user', 'user', $form, $form_state);
341 }
342
343 // User registration settings.
344 $form['registration_cancellation'] = array(
345 '#type' => 'details',
346 '#title' => t('Registration and cancellation'),
347 );
348 $form['registration_cancellation']['user_register'] = array(
349 '#type' => 'radios',
350 '#title' => t('Who can register accounts?'),
351 '#default_value' => $config->get('register'),
352 '#options' => array(
353 USER_REGISTER_ADMINISTRATORS_ONLY => t('Administrators only'),
354 USER_REGISTER_VISITORS => t('Visitors'),
355 USER_REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL => t('Visitors, but administrator approval is required'),
356 )
357 );
358 $form['registration_cancellation']['user_email_verification'] = array(
359 '#type' => 'checkbox',
360 '#title' => t('Require e-mail verification when a visitor creates an account.'),
361 '#default_value' => $config->get('verify_mail'),
362 '#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. With this setting disabled, users will be logged in immediately upon registering, and may select their own passwords during registration.')
363 );
364 form_load_include($form_state, 'inc', 'user', 'user.pages');
365 $form['registration_cancellation']['user_cancel_method'] = array(
366 '#type' => 'radios',
367 '#title' => t('When cancelling a user account'),
368 '#default_value' => $config->get('cancel_method'),
369 '#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/people/permissions'))),
370 );
371 $form['registration_cancellation']['user_cancel_method'] += user_cancel_methods();
372 foreach (element_children($form['registration_cancellation']['user_cancel_method']) as $key) {
373 // All account cancellation methods that specify #access cannot be
374 // configured as default method.
375 // @see hook_user_cancel_methods_alter()
376 if (isset($form['registration_cancellation']['user_cancel_method'][$key]['#access'])) {
377 $form['registration_cancellation']['user_cancel_method'][$key]['#access'] = FALSE;
378 }
379 }
380
381 // Account settings.
382 $form['personalization'] = array(
383 '#type' => 'details',
384 '#title' => t('Personalization'),
385 );
386 $form['personalization']['user_signatures'] = array(
387 '#type' => 'checkbox',
388 '#title' => t('Enable signatures.'),
389 '#default_value' => $config->get('signatures'),
390 );
391
392 $form['email'] = array(
393 '#type' => 'vertical_tabs',
394 '#title' => t('E-mails'),
395 );
396 // These email tokens are shared for all settings, so just define
397 // the list once to help ensure they stay in sync.
398 $email_token_help = t('Available variables are: [site:name], [site:url], [user:name], [user:mail], [site:login-url], [site:url-brief], [user:edit-url], [user:one-time-login-url], [user:cancel-url].');
399
400 $form['email_admin_created'] = array(
401 '#type' => 'details',
402 '#title' => t('Welcome (new user created by administrator)'),
403 '#collapsible' => TRUE,
404 '#collapsed' => ($config->get('register') != USER_REGISTER_ADMINISTRATORS_ONLY),
405 '#description' => t('Edit the welcome e-mail messages sent to new member accounts created by an administrator.') . ' ' . $email_token_help,
406 '#group' => 'email',
407 );
408 $form['email_admin_created']['user_mail_register_admin_created_subject'] = array(
409 '#type' => 'textfield',
410 '#title' => t('Subject'),
411 '#default_value' => $mail_config->get('register_admin_created.subject'),
412 '#maxlength' => 180,
413 );
414 $form['email_admin_created']['user_mail_register_admin_created_body'] = array(
415 '#type' => 'textarea',
416 '#title' => t('Body'),
417 '#default_value' => $mail_config->get('register_admin_created.body'),
418 '#rows' => 15,
419 );
420
421 $form['email_pending_approval'] = array(
422 '#type' => 'details',
423 '#title' => t('Welcome (awaiting approval)'),
424 '#collapsible' => TRUE,
425 '#collapsed' => ($config->get('register') != USER_REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL),
426 '#description' => t('Edit the welcome e-mail messages sent to new members upon registering, when administrative approval is required.') . ' ' . $email_token_help,
427 '#group' => 'email',
428 );
429 $form['email_pending_approval']['user_mail_register_pending_approval_subject'] = array(
430 '#type' => 'textfield',
431 '#title' => t('Subject'),
432 '#default_value' => $mail_config->get('register_pending_approval.subject'),
433 '#maxlength' => 180,
434 );
435 $form['email_pending_approval']['user_mail_register_pending_approval_body'] = array(
436 '#type' => 'textarea',
437 '#title' => t('Body'),
438 '#default_value' => $mail_config->get('register_pending_approval.body'),
439 '#rows' => 8,
440 );
441
442 $form['email_no_approval_required'] = array(
443 '#type' => 'details',
444 '#title' => t('Welcome (no approval required)'),
445 '#collapsible' => TRUE,
446 '#collapsed' => ($config->get('register') != USER_REGISTER_VISITORS),
447 '#description' => t('Edit the welcome e-mail messages sent to new members upon registering, when no administrator approval is required.') . ' ' . $email_token_help,
448 '#group' => 'email',
449 );
450 $form['email_no_approval_required']['user_mail_register_no_approval_required_subject'] = array(
451 '#type' => 'textfield',
452 '#title' => t('Subject'),
453 '#default_value' => $mail_config->get('register_no_approval_required.subject'),
454 '#maxlength' => 180,
455 );
456 $form['email_no_approval_required']['user_mail_register_no_approval_required_body'] = array(
457 '#type' => 'textarea',
458 '#title' => t('Body'),
459 '#default_value' => $mail_config->get('register_no_approval_required.body'),
460 '#rows' => 15,
461 );
462
463 $form['email_password_reset'] = array(
464 '#type' => 'details',
465 '#title' => t('Password recovery'),
466 '#collapsible' => TRUE,
467 '#collapsed' => TRUE,
468 '#description' => t('Edit the e-mail messages sent to users who request a new password.') . ' ' . $email_token_help,
469 '#group' => 'email',
470 '#weight' => 10,
471 );
472 $form['email_password_reset']['user_mail_password_reset_subject'] = array(
473 '#type' => 'textfield',
474 '#title' => t('Subject'),
475 '#default_value' => $mail_config->get('password_reset.subject'),
476 '#maxlength' => 180,
477 );
478 $form['email_password_reset']['user_mail_password_reset_body'] = array(
479 '#type' => 'textarea',
480 '#title' => t('Body'),
481 '#default_value' => $mail_config->get('password_reset.body'),
482 '#rows' => 12,
483 );
484
485 $form['email_activated'] = array(
486 '#type' => 'details',
487 '#title' => t('Account activation'),
488 '#collapsible' => TRUE,
489 '#collapsed' => TRUE,
490 '#description' => t('Enable and edit 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,
491 '#group' => 'email',
492 );
493 $form['email_activated']['user_mail_status_activated_notify'] = array(
494 '#type' => 'checkbox',
495 '#title' => t('Notify user when account is activated.'),
496 '#default_value' => $config->get('notify.status_activated'),
497 );
498 $form['email_activated']['settings'] = array(
499 '#type' => 'container',
500 '#states' => array(
501 // Hide the additional settings when this email is disabled.
502 'invisible' => array(
503 'input[name="user_mail_status_activated_notify"]' => array('checked' => FALSE),
504 ),
505 ),
506 );
507 $form['email_activated']['settings']['user_mail_status_activated_subject'] = array(
508 '#type' => 'textfield',
509 '#title' => t('Subject'),
510 '#default_value' => $mail_config->get('status_activated.subject'),
511 '#maxlength' => 180,
512 );
513 $form['email_activated']['settings']['user_mail_status_activated_body'] = array(
514 '#type' => 'textarea',
515 '#title' => t('Body'),
516 '#default_value' => $mail_config->get('status_activated.body'),
517 '#rows' => 15,
518 );
519
520 $form['email_blocked'] = array(
521 '#type' => 'details',
522 '#title' => t('Account blocked'),
523 '#collapsible' => TRUE,
524 '#collapsed' => TRUE,
525 '#description' => t('Enable and edit e-mail messages sent to users when their accounts are blocked.') . ' ' . $email_token_help,
526 '#group' => 'email',
527 );
528 $form['email_blocked']['user_mail_status_blocked_notify'] = array(
529 '#type' => 'checkbox',
530 '#title' => t('Notify user when account is blocked.'),
531 '#default_value' => $config->get('notify.status_blocked'),
532 );
533 $form['email_blocked']['settings'] = array(
534 '#type' => 'container',
535 '#states' => array(
536 // Hide the additional settings when the blocked email is disabled.
537 'invisible' => array(
538 'input[name="user_mail_status_blocked_notify"]' => array('checked' => FALSE),
539 ),
540 ),
541 );
542 $form['email_blocked']['settings']['user_mail_status_blocked_subject'] = array(
543 '#type' => 'textfield',
544 '#title' => t('Subject'),
545 '#default_value' => $mail_config->get('status_blocked.subject'),
546 '#maxlength' => 180,
547 );
548 $form['email_blocked']['settings']['user_mail_status_blocked_body'] = array(
549 '#type' => 'textarea',
550 '#title' => t('Body'),
551 '#default_value' => $mail_config->get('status_blocked.body'),
552 '#rows' => 3,
553 );
554
555 $form['email_cancel_confirm'] = array(
556 '#type' => 'details',
557 '#title' => t('Account cancellation confirmation'),
558 '#collapsible' => TRUE,
559 '#collapsed' => TRUE,
560 '#description' => t('Edit the e-mail messages sent to users when they attempt to cancel their accounts.') . ' ' . $email_token_help,
561 '#group' => 'email',
562 );
563 $form['email_cancel_confirm']['user_mail_cancel_confirm_subject'] = array(
564 '#type' => 'textfield',
565 '#title' => t('Subject'),
566 '#default_value' => $mail_config->get('cancel_confirm.subject'),
567 '#maxlength' => 180,
568 );
569 $form['email_cancel_confirm']['user_mail_cancel_confirm_body'] = array(
570 '#type' => 'textarea',
571 '#title' => t('Body'),
572 '#default_value' => $mail_config->get('cancel_confirm.body'),
573 '#rows' => 3,
574 );
575
576 $form['email_canceled'] = array(
577 '#type' => 'details',
578 '#title' => t('Account canceled'),
579 '#collapsible' => TRUE,
580 '#collapsed' => TRUE,
581 '#description' => t('Enable and edit e-mail messages sent to users when their accounts are canceled.') . ' ' . $email_token_help,
582 '#group' => 'email',
583 );
584 $form['email_canceled']['user_mail_status_canceled_notify'] = array(
585 '#type' => 'checkbox',
586 '#title' => t('Notify user when account is canceled.'),
587 '#default_value' => $config->get('notify.status_canceled'),
588 );
589 $form['email_canceled']['settings'] = array(
590 '#type' => 'container',
591 '#states' => array(
592 // Hide the settings when the cancel notify checkbox is disabled.
593 'invisible' => array(
594 'input[name="user_mail_status_canceled_notify"]' => array('checked' => FALSE),
595 ),
596 ),
597 );
598 $form['email_canceled']['settings']['user_mail_status_canceled_subject'] = array(
599 '#type' => 'textfield',
600 '#title' => t('Subject'),
601 '#default_value' => $mail_config->get('status_canceled.subject'),
602 '#maxlength' => 180,
603 );
604 $form['email_canceled']['settings']['user_mail_status_canceled_body'] = array(
605 '#type' => 'textarea',
606 '#title' => t('Body'),
607 '#default_value' => $mail_config->get('status_canceled.body'),
608 '#rows' => 3,
609 );
610
611 return system_config_form($form, $form_state);
612 }
613
614 /**
615 * Form submission handler for user_admin_settings().
616 */
617 function user_admin_settings_submit($form, &$form_state) {
618 config('user.settings')
619 ->set('anonymous', $form_state['values']['anonymous'])
620 ->set('admin_role', $form_state['values']['user_admin_role'])
621 ->set('register', $form_state['values']['user_register'])
622 ->set('verify_mail', $form_state['values']['user_email_verification'])
623 ->set('signatures', $form_state['values']['user_signatures'])
624 ->set('cancel_method', $form_state['values']['user_cancel_method'])
625 ->set('notify.status_activated', $form_state['values']['user_mail_status_activated_notify'])
626 ->set('notify.status_blocked', $form_state['values']['user_mail_status_blocked_notify'])
627 ->set('notify.status_canceled', $form_state['values']['user_mail_status_canceled_notify'])
628 ->save();
629 config('user.mail')
630 ->set('cancel_confirm.body', $form_state['values']['user_mail_cancel_confirm_body'])
631 ->set('cancel_confirm.subject', $form_state['values']['user_mail_cancel_confirm_subject'])
632 ->set('password_reset.body', $form_state['values']['user_mail_password_reset_body'])
633 ->set('password_reset.subject', $form_state['values']['user_mail_password_reset_subject'])
634 ->set('register_admin_created.body', $form_state['values']['user_mail_register_admin_created_body'])
635 ->set('register_admin_created.subject', $form_state['values']['user_mail_register_admin_created_subject'])
636 ->set('register_no_approval_required.body', $form_state['values']['user_mail_register_no_approval_required_body'])
637 ->set('register_no_approval_required.subject', $form_state['values']['user_mail_register_no_approval_required_subject'])
638 ->set('register_pending_approval.body', $form_state['values']['user_mail_register_pending_approval_body'])
639 ->set('register_pending_approval.subject', $form_state['values']['user_mail_register_pending_approval_subject'])
640 ->set('status_activated.body', $form_state['values']['user_mail_status_activated_body'])
641 ->set('status_activated.subject', $form_state['values']['user_mail_status_activated_subject'])
642 ->set('status_blocked.body', $form_state['values']['user_mail_status_blocked_body'])
643 ->set('status_blocked.subject', $form_state['values']['user_mail_status_blocked_subject'])
644 ->set('status_canceled.body', $form_state['values']['user_mail_status_canceled_body'])
645 ->set('status_canceled.subject', $form_state['values']['user_mail_status_canceled_subject'])
646 ->save();
647 }
648
649 /**
650 * Menu callback: administer permissions.
651 *
652 * @ingroup forms
653 * @see user_admin_permissions_submit()
654 * @see theme_user_admin_permissions()
655 */
656 function user_admin_permissions($form, $form_state, $rid = NULL) {
657
658 // Retrieve role names for columns.
659 $role_names = user_role_names();
660 if (isset($rid)) {
661 $role_names = array($rid => $role_names[$rid]);
662 }
663 // Fetch permissions for all roles or the one selected role.
664 $role_permissions = user_role_permissions($role_names);
665
666 // Store $role_names for use when saving the data.
667 $form['role_names'] = array(
668 '#type' => 'value',
669 '#value' => $role_names,
670 );
671 // Render role/permission overview:
672 $options = array();
673 $module_info = system_get_info('module');
674 $hide_descriptions = system_admin_compact_mode();
675
676 // Get a list of all the modules implementing a hook_permission() and sort by
677 // display name.
678 $modules = array();
679 foreach (module_implements('permission') as $module) {
680 $modules[$module] = $module_info[$module]['name'];
681 }
682 asort($modules);
683
684 foreach ($modules as $module => $display_name) {
685 if ($permissions = module_invoke($module, 'permission')) {
686 $form['permission'][] = array(
687 '#markup' => $module_info[$module]['name'],
688 '#id' => $module,
689 );
690 foreach ($permissions as $perm => $perm_item) {
691 // Fill in default values for the permission.
692 $perm_item += array(
693 'description' => '',
694 'restrict access' => FALSE,
695 'warning' => !empty($perm_item['restrict access']) ? t('Warning: Give to trusted roles only; this permission has security implications.') : '',
696 );
697 $options[$perm] = '';
698 $form['permission'][$perm] = array(
699 '#type' => 'item',
700 '#markup' => $perm_item['title'],
701 '#description' => theme('user_permission_description', array('permission_item' => $perm_item, 'hide' => $hide_descriptions)),
702 );
703 foreach ($role_names as $rid => $name) {
704 // Builds arrays for checked boxes for each role
705 if (isset($role_permissions[$rid][$perm])) {
706 $status[$rid][] = $perm;
707 }
708 }
709 }
710 }
711 }
712
713 // Have to build checkboxes here after checkbox arrays are built
714 foreach ($role_names as $rid => $name) {
715 $form['checkboxes'][$rid] = array(
716 '#type' => 'checkboxes',
717 '#options' => $options,
718 '#default_value' => isset($status[$rid]) ? $status[$rid] : array(),
719 '#attributes' => array('class' => array('rid-' . $rid)),
720 );
721 $form['role_names'][$rid] = array('#markup' => check_plain($name), '#tree' => TRUE);
722 }
723
724 $form['actions'] = array('#type' => 'actions');
725 $form['actions']['submit'] = array('#type' => 'submit', '#value' => t('Save permissions'));
726
727 $form['#attached']['library'][] = array('user', 'drupal.user.permissions');
728
729 return $form;
730 }
731
732 /**
733 * Save permissions selected on the administer permissions page.
734 *
735 * @see user_admin_permissions()
736 */
737 function user_admin_permissions_submit($form, &$form_state) {
738 foreach ($form_state['values']['role_names'] as $rid => $name) {
739 user_role_change_permissions($rid, $form_state['values'][$rid]);
740 }
741
742 drupal_set_message(t('The changes have been saved.'));
743
744 // Clear the cached pages and blocks.
745 cache_invalidate_tags(array('content' => TRUE));
746 }
747
748 /**
749 * Returns HTML for the administer permissions page.
750 *
751 * @param $variables
752 * An associative array containing:
753 * - form: A render element representing the form.
754 *
755 * @ingroup themeable
756 */
757 function theme_user_admin_permissions($variables) {
758 $form = $variables['form'];
759
760 $roles = user_role_names();
761 foreach (element_children($form['permission']) as $key) {
762 $row = array();
763 // Module name
764 if (is_numeric($key)) {
765 $row[] = array('data' => drupal_render($form['permission'][$key]), 'class' => array('module'), 'id' => 'module-' . $form['permission'][$key]['#id'], 'colspan' => count($form['role_names']['#value']) + 1);
766 }
767 else {
768 // Permission row.
769 $row[] = array(
770 'data' => drupal_render($form['permission'][$key]),
771 'class' => array('permission'),
772 );
773 foreach (element_children($form['checkboxes']) as $rid) {
774 $form['checkboxes'][$rid][$key]['#title'] = $roles[$rid] . ': ' . $form['permission'][$key]['#markup'];
775 $form['checkboxes'][$rid][$key]['#title_display'] = 'invisible';
776 $row[] = array('data' => drupal_render($form['checkboxes'][$rid][$key]), 'class' => array('checkbox'));
777 }
778 }
779 $rows[] = $row;
780 }
781 $header[] = (t('Permission'));
782 foreach (element_children($form['role_names']) as $rid) {
783 $header[] = array('data' => drupal_render($form['role_names'][$rid]), 'class' => array('checkbox'));
784 }
785 $output = theme('system_compact_link');
786 $output .= theme('table', array('header' => $header, 'rows' => $rows, 'attributes' => array('id' => 'permissions')));
787 $output .= drupal_render_children($form);
788 return $output;
789 }
790
791 /**
792 * Returns HTML for an individual permission description.
793 *
794 * @param $variables
795 * An associative array containing:
796 * - permission_item: An associative array representing the permission whose
797 * description is being themed. Useful keys include:
798 * - description: The text of the permission description.
799 * - warning: A security-related warning message about the permission (if
800 * there is one).
801 * - hide: A boolean indicating whether or not the permission description was
802 * requested to be hidden rather than shown.
803 *
804 * @ingroup themeable
805 */
806 function theme_user_permission_description($variables) {
807 if (!$variables['hide']) {
808 $description = array();
809 $permission_item = $variables['permission_item'];
810 if (!empty($permission_item['description'])) {
811 $description[] = $permission_item['description'];
812 }
813 if (!empty($permission_item['warning'])) {
814 $description[] = '<em class="permission-warning">' . $permission_item['warning'] . '</em>';
815 }
816 if (!empty($description)) {
817 return implode(' ', $description);
818 }
819 }
820 }
821
822 /**
823 * Form to re-order roles or add a new one.
824 *
825 * @ingroup forms
826 * @see theme_user_admin_roles()
827 */
828 function user_admin_roles($form, $form_state) {
829 $roles = user_roles();
830
831 $form['roles'] = array(
832 '#tree' => TRUE,
833 );
834
835 foreach ($roles as $rid => $role) {
836 $form['roles'][$rid]['#role'] = $role;
837 $form['roles'][$rid]['#weight'] = $role->weight;
838 $form['roles'][$rid]['name'] = array(
839 '#markup' => check_plain($role->label()),
840 );
841 $form['roles'][$rid]['weight'] = array(
842 '#type' => 'textfield',
843 '#title' => t('Weight for @title', array('@title' => $role->label())),
844 '#title_display' => 'invisible',
845 '#size' => 4,
846 '#default_value' => $role->weight,
847 '#attributes' => array('class' => array('role-weight')),
848 );
849 $links['edit'] = array(
850 'title' => t('edit role'),
851 'href' => 'admin/people/roles/edit/' . $rid,
852 'weight' => 0,
853 );
854 $links['permissions'] = array(
855 'title' => t('edit permissions'),
856 'href' => 'admin/people/permissions/' . $rid,
857 'weight' => 5,
858 );
859 $form['roles'][$rid]['operations'] = array(
860 '#type' => 'operations',
861 '#links' => $links,
862 );
863 }
864
865 // Embed the role add form.
866 $add_role = entity_create('user_role', array(
867 'id' => NULL,
868 'label' => NULL,
869 ));
870 $add_form = user_admin_role(array(), $form_state, $add_role);
871 $add_form['actions']['submit']['#submit'] = array('user_admin_role_submit');
872 $add_form['role']['actions'] = $add_form['actions'];
873 unset($add_form['actions']);
874 $form += $add_form;
875
876 $form['actions']['#type'] = 'actions';
877 $form['actions']['submit'] = array(
878 '#type' => 'submit',
879 '#value' => t('Save order'),
880 // Do not validate the add form when saving the order.
881 '#limit_validation_errors' => array(array('roles')),
882 '#submit' => array('user_admin_roles_order_submit'),
883 );
884
885 return $form;
886 }
887
888 /**
889 * Form submit function. Update the role weights.
890 */
891 function user_admin_roles_order_submit($form, &$form_state) {
892 foreach ($form_state['values']['roles'] as $rid => $role_values) {
893 $role = $form['roles'][$rid]['#role'];
894 $role->weight = $role_values['weight'];
895 $role->save();
896 }
897 drupal_set_message(t('The role settings have been updated.'));
898 }
899
900 /**
901 * Returns HTML for the role order and new role form.
902 *
903 * @param $variables
904 * An associative array containing:
905 * - form: A render element representing the form.
906 *
907 * @ingroup themeable
908 */
909 function theme_user_admin_roles($variables) {
910 $form = $variables['form'];
911
912 $header = array(t('Name'), t('Weight'), t('Operations'));
913 foreach (element_children($form['roles']) as $rid) {
914 $row = array();
915 foreach (element_children($form['roles'][$rid]) as $column) {
916 $row[] = drupal_render($form['roles'][$rid][$column]);
917 }
918 $rows[] = array('data' => $row, 'class' => array('draggable'));
919 }
920
921 // Distribute the role add form into table columns.
922 $form['role']['name']['#title_display'] = 'invisible';
923 unset($form['role']['name']['#description']);
924 unset($form['role']['rid']['#description']);
925
926 $actions = $form['role']['actions'];
927 unset($form['role']['actions']);
928 unset($form['role']['weight']);
929 $row = array();
930 $row[] = drupal_render($form['role']);
931 // Empty placeholder for the weight column.
932 $row[] = '';
933 $row[] = array('data' => drupal_render($actions), 'colspan' => 2);
934 $rows[] = array('data' => $row);
935
936 drupal_add_tabledrag('user-roles', 'order', 'sibling', 'role-weight');
937
938 $output = theme('table', array('header' => $header, 'rows' => $rows, 'attributes' => array('id' => 'user-roles')));
939 $output .= drupal_render_children($form);
940
941 return $output;
942 }
943
944 /**
945 * Form to configure a single role.
946 *
947 * @ingroup forms
948 * @see user_admin_role_submit()
949 *
950 * @todo Move into a RoleFormController.
951 */
952 function user_admin_role($form, $form_state, $role) {
953 $form['role'] = array(
954 '#tree' => TRUE,
955 '#parents' => array('role'),
956 );
957 $form['role']['label'] = array(
958 '#type' => 'textfield',
959 '#title' => t('Role name'),
960 '#default_value' => $role->label(),
961 '#size' => 30,
962 '#required' => TRUE,
963 '#maxlength' => 64,
964 '#description' => t('The name for this role. Example: "Moderator", "Editorial board", "Site architect".'),
965 );
966 $form['role']['id'] = array(
967 '#type' => 'machine_name',
968 '#default_value' => $role->id(),
969 '#required' => TRUE,
970 '#disabled' => !$role->isNew(),
971 '#size' => 30,
972 '#maxlength' => 64,
973 '#machine_name' => array(
974 'exists' => 'user_role_load',
975 'source' => array('role', 'label'),
976 ),
977 );
978 $form['role']['weight'] = array(
979 '#type' => 'value',
980 '#value' => $role->weight,
981 );
982 $form['actions'] = array('#type' => 'actions');
983 $form['actions']['submit'] = array(
984 '#type' => 'submit',
985 '#value' => !$role->isNew() ? t('Save role') : t('Add role'),
986 );
987 $form['actions']['delete'] = array(
988 '#type' => 'submit',
989 '#value' => t('Delete role'),
990 '#access' => !$role->isNew() && !in_array($role->id(), array(DRUPAL_ANONYMOUS_RID, DRUPAL_AUTHENTICATED_RID)),
991 '#submit' => array('user_admin_role_delete_submit'),
992 );
993
994 return $form;
995 }
996
997 /**
998 * Form submit handler for the user_admin_role() form.
999 */
1000 function user_admin_role_submit($form, &$form_state) {
1001 // Prevent leading and trailing spaces in role names.
1002 $form_state['values']['role']['label'] = trim($form_state['values']['role']['label']);
1003
1004 $role = entity_create('user_role', $form_state['values']['role']);
1005 if ($role->save() == SAVED_UPDATED) {
1006 drupal_set_message(t('The role has been renamed.'));
1007 }
1008 else {
1009 drupal_set_message(t('The role has been added.'));
1010 }
1011 $form_state['redirect'] = 'admin/people/roles';
1012 }
1013
1014 /**
1015 * Form submit handler for the user_admin_role() form.
1016 */
1017 function user_admin_role_delete_submit($form, &$form_state) {
1018 $form_state['redirect'] = 'admin/people/roles/delete/' . $form_state['values']['role']['id'];
1019 }
1020
1021 /**
1022 * Form to confirm role delete operation.
1023 */
1024 function user_admin_role_delete_confirm($form, &$form_state, $role) {
1025 $form['id'] = array(
1026 '#type' => 'value',
1027 '#value' => $role->id(),
1028 );
1029 return confirm_form($form, t('Are you sure you want to delete the role %name ?', array('%name' => $role->label())), 'admin/people/roles', t('This action cannot be undone.'), t('Delete'));
1030 }
1031
1032 /**
1033 * Form submit handler for user_admin_role_delete_confirm().
1034 */
1035 function user_admin_role_delete_confirm_submit($form, &$form_state) {
1036 entity_delete_multiple('user_role', array($form_state['values']['id']));
1037 drupal_set_message(t('The role has been deleted.'));
1038 $form_state['redirect'] = 'admin/people/roles';
1039 }