1 user.admin.inc user_admin_permissions($form, $form_state, $role_name = NULL)

Menu callback: administer permissions.

See also

user_admin_permissions_submit()

theme_user_admin_permissions()

Related topics

File

core/modules/user/user.admin.inc, line 419
Admin page callbacks for the User module.

Code

function user_admin_permissions($form, $form_state, $role_name = NULL) {

  // Retrieve role names for columns.
  $roles = user_roles(FALSE, NULL, TRUE);
  if (isset($roles[$role_name])) {
    $roles = array($role_name => $roles[$role_name]);
  }
  $form['roles'] = array(
    '#type' => 'value',
    '#value' => $roles,
  );

  // JS-only table filters.
  $form['filter'] = array(
    '#type' => 'container',
    '#attributes' => array(
      'class' => array('table-filter', 'js-show'),
    ),
  );
  $form['filter']['search'] = array(
    '#type' => 'textfield',
    '#title' => t('Filter'),
    '#size' => 30,
    '#placeholder' => t('Search...'),
    '#attributes' => array(
      'class' => array('table-filter-text'),
      'autocomplete' => 'off',
      'title' => t('Enter a part of the permission name, module name, or description to filter.'),
    ),
    '#default_value' => (isset($_GET['search'])) ? $_GET['search'] : '',
  );
  $form['filter']['reset'] = array(
    '#theme' => 'link',
    '#text' => t('Reset'),
    '#path' => $_GET['q'],
    '#options' => array(
      'attributes' => array(
        'class' => array('button', 'button-secondary', 'search-reset'),
      ),
    ),
  );

  // Render role/permission overview:
  $options = array();
  $module_info = system_get_info('module');

  // Get a list of all the modules implementing a hook_permission() and sort by
  // display name.
  $modules = array();
  foreach (module_implements('permission') as $module) {
    $modules[$module] = $module_info[$module]['name'];
  }
  asort($modules);

  foreach ($modules as $module => $display_name) {
    if ($permissions = module_invoke($module, 'permission')) {
      $form['permission'][] = array(
        '#markup' => $module_info[$module]['name'],
        '#id' => $module,
      );
      foreach ($permissions as $perm => $perm_item) {
        // Fill in default values for the permission.
        $perm_item += array(
          'description' => '',
          'restrict access' => FALSE,
          'warning' => '',
        );
        $options[$perm] = '';
        $form['permission'][$perm] = array(
          '#type' => 'item',
          '#markup' => $perm_item['title'],
          '#description' => theme('user_permission_description', array('permission_item' => $perm_item)),
        );
        foreach ($roles as $role_name => $role) {
          // Build arrays for checked boxes for each role.
          if (in_array($perm, $role->permissions)) {
            $status[$role_name][] = $perm;
          }
        }
      }
    }
  }

  // Build the checkboxes here after the role arrays are built.
  foreach ($roles as $role_name => $role) {
    $form['checkboxes'][$role_name] = array(
      '#type' => 'checkboxes',
      '#options' => $options,
      '#default_value' => isset($status[$role_name]) ? $status[$role_name] : array(),
      '#attributes' => array('class' => array('role-' . $role_name)),
    );
    $form['role_names'][$role_name] = array(
      '#markup' => check_plain($role->label),
      '#description' => filter_xss_admin($role->description),
      '#tree' => TRUE,
    );
  }

  $form['actions'] = array('#type' => 'actions');
  $form['actions']['submit'] = array('#type' => 'submit', '#value' => t('Save permissions'));

  $form['#attached']['js'][] = backdrop_get_path('module', 'user') . '/js/user.permissions.js';

  return $form;
}