You are here

user_permissions.module in User Permissions 8

Module file for user_permissions.

Defines a new constant for the dummy role _user_role_0, which will not be given any permissions.

File

user_permissions.module
View source
<?php

/**
 * @file
 * Module file for user_permissions.
 *
 * Defines a new constant for the dummy role _user_role_0,
 * which will not be given any permissions.
 */
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element;
use Drupal\Core\Routing\RouteMatchInterface;
define("USER_PERMISSIONS_NO_ROLE", '_user_role_0');

/**
 * The pattern that matches a User Permissions related role.
 */
define('USER_PERMISSIONS_ROLE_REGEX', '/^_user_role_\\d+$/');

/**
 * Implements hook_help().
 */
function user_permissions_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {
    case 'help.page.user_permissions':
      $output = '';
      $output .= '<p>' . t("User Permissions provides an interface for giving additional permissions to individual users without the need to assign them to a special role. When this module is enabled, users with the 'administer permissions' permission can access the 'User Permissions' tab on each user's account.") . '</p>';
      return [
        '#markup' => $output,
      ];
  }
}

/**
 * Implements hook_user_delete().
 */
function user_permissions_user_cancel($edit, $account, $method) {

  // Set user specific role name.
  $role_name = '_user_role_' . $account->uid;

  // Check for the existence of this role.
  $role = Drupal::entityQuery('user_role')
    ->condition('name', $role_name)
    ->execute();
  if ($role) {
    Role::load($role)
      ->delete();
  }
}

/**
 * Implements hook_form_alter().
 *
 * Alter user list page.
 */
function user_permissions_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  if (strpos($form_id, 'views_form_user_admin_people_page') !== FALSE) {

    // Remove _user_role_N roles from the Add/Remove role to the selected users.
    // @link /admin/people
    if (!empty($form['header']['user_bulk_form']['action']['#options'])) {
      $options =& $form['header']['user_bulk_form']['action']['#options'];
      $operations = [
        'user_add_role_action.',
        'user_remove_role_action.',
      ];
      foreach ($operations as $operation) {
        if ($opts = preg_grep('/.*?(_user_role_\\d+$)/', array_keys($options))) {
          foreach ($opts as $action) {
            unset($options[$action]);
          }
        }
      }
    }
  }

  // Remove _user_role_N roles from exposed filter on user list.
  // @link /admin/people
  if (strpos($form_id, 'views_exposed_form') !== FALSE) {
    if (is_array($form['role']['#options'])) {

      // Removes _user_role_N roles from the roles user filter.
      // @link /admin/people page.
      user_permissions_array_filter_roles($form['role']['#options']);
    }
  }
}

/**
 * Implements hook_form_FORM_ID_alter().
 *
 * Removes options related to _user_role_N roles from the edit form.
 *
 * @link /user/{UID}/edit
 *
 * @see user_profile_form()
 */
function user_permissions_form_user_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  foreach (user_roles() as $rid => $role) {
    $default_value = $form['account']['roles']['#default_value'];
    if (preg_match(USER_PERMISSIONS_ROLE_REGEX, $rid) && !in_array($rid, $default_value)) {
      unset($form['account']['roles']['#options'][$rid]);
    }
  }
}

/**
 * Implements hook_form_FORM_ID_alter().
 *
 * Removes the elements related to _user_role_N roles from the Roles form.
 *
 * @link admin/people/create
 *
 * @see user_register_form()
 */
function user_permissions_form_user_register_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  if (isset($form['account']['roles'])) {
    user_permissions_array_filter_roles($form['account']['roles']['#options']);
    if (empty($form['account']['roles']['#options'])) {
      $form['account']['roles']['#access'] = FALSE;
    }
  }
}

/**
 * Implements hook_form_FORM_ID_alter().
 *
 * Alters user_admin_permissions.
 */
function user_permissions_form_user_admin_permissions_alter(&$form, FormStateInterface $form_state, $form_id) {

  // If viewing a user permissions page, this returns early so
  // it doesn't modify the form.
  if (count($form['role_names']['#value']) == 1) {
    $names = array_values($form['role_names']['#value']);
    if (preg_match(USER_PERMISSIONS_ROLE_REGEX, array_shift($names))) {
      return;
    }
  }
  $role_id_filter = [];

  // Creates an array of role IDs to use as a filter for removing
  // all _user_role_N related elements from the Permissions form.
  foreach (user_roles() as $rid => $name) {
    if (preg_match(USER_PERMISSIONS_ROLE_REGEX, $rid)) {
      $role_id_filter[] = $rid;
    }
  }

  // Removes the checkboxes from the Permissions form.
  // @link /admin/people/permissions
  foreach (Element::children($form['permissions']) as $key => $value) {
    $element_keys = Element::children($form['permissions'][$value]);
    foreach ($element_keys as $elem_key => $elem_value) {
      if (!is_numeric($elem_value) && in_array($elem_value, $role_id_filter)) {
        unset($form['permissions'][$value][$elem_value]);
      }
    }
  }

  // Removes the columns for the _user_role_N roles,
  // from the Permissions form on the /admin/people/permissions page.
  foreach ($form['role_names'] as $key) {
    if (in_array($key, $role_id_filter)) {
      unset($form['role_names']['#value'][$key]);
    }
  }

  // Removes the header for the _user_role_N roles,
  // from the Permissions form on the /admin/people/permissions page.
  foreach ($form['permissions']['#header'] as $key => $value) {
    if (is_array($value) && in_array($value['data'], $role_id_filter)) {
      unset($form['permissions']['#header'][$key]);
    }
  }
}

/**
 * Implements hook_form_FORM_ID_alter().
 *
 * Alters user_admin_roles_form.
 */
function user_permissions_form_user_admin_roles_form_alter(&$form, FormStateInterface $form_state, $form_id) {

  // Removes the elements related to _user_role_N roles from the Roles form.
  // The form can be found at /admin/people/permissions/roles.
  foreach (user_roles() as $rid => $role) {
    if (preg_match(USER_PERMISSIONS_ROLE_REGEX, $rid)) {
      unset($form['entities'][$rid]);
    }
  }
}

/**
 * Remove the User Permissions module related roles from an array.
 */
function user_permissions_array_filter_roles(&$array) {
  $array = array_filter(preg_replace(USER_PERMISSIONS_ROLE_REGEX, '', $array));
}