You are here

permissions_lock.module in Permissions Lock 8

Same filename and directory in other branches
  1. 7 permissions_lock.module

Lock permissions on the permissions administration pages for certain roles

@author Sven Decabooter

File

permissions_lock.module
View source
<?php

/**
 * @file
 * Lock permissions on the permissions administration pages for certain roles
 *
 * @author Sven Decabooter
 */
use Drupal\Core\Routing\RouteMatchInterface;

/**
 * Implements hook_help().
 */
function permissions_lock_help($path, RouteMatchInterface $route_match) {
  switch ($path) {
    case 'help.page.permissions_lock':
      $output = '';
      $output .= '<p>' . t('Lock permissions on the permissions administration pages for certain roles.') . '</p>';
      break;
  }
  return $output;
}

/**
 * Implements hook_form_alter().
 */
function permissions_lock_form_alter(&$form, \Drupal\Core\Form\FormStateInterface &$form_state, $form_id) {
  if ($form_id == 'user_admin_permissions' || $form_id == 'user_permissions_profile_permissions_form') {

    // Get all available permissions in the form.
    $config = \Drupal::config('permissions_lock.settings');
    $default_roles = $config
      ->get('permissions_lock_locked_roles');
    $default_perms = $config
      ->get('permissions_lock_locked_perm');
    if (!\Drupal::currentUser()
      ->hasPermission('manage permissions unrestricted')) {

      // Get all locked roles from permission lock form.
      $locked_roles = array();
      foreach ($default_roles as $key => $value) {
        if ($value) {
          $locked_roles[$key] = $value;
        }
      }

      // Get all locked permissions from permission lock form.
      $locked_permissions = array();
      foreach ($default_perms as $key => $value) {
        if ($value) {
          $locked_permissions[$key] = $value;
        }
      }

      // Hide all the locked roles.
      foreach ($locked_roles as $key => $locked_rid) {
        foreach ($default_perms as $key1 => $value1) {
          unset($form['permissions'][$key1][$key]);
        }
        foreach ($form['permissions']['#header'] as $key2 => $value2) {
          if (is_array($value2)) {
            if ($value2['data'] == user_roles()[$key]
              ->label()) {
              unset($form['permissions']['#header'][$key2]);
            }
          }
        }
      }

      // Hide all the locked permissions.
      foreach ($locked_permissions as $key => $value) {
        unset($form['permissions'][$key]);
      }
    }
  }
}

Functions