You are here

permissions_by_term.install in Permissions by Term 8.2

Same filename and directory in other branches
  1. 8 permissions_by_term.install
  2. 7 permissions_by_term.install

Install, update and uninstall functions for the permissions_by_term module.

File

permissions_by_term.install
View source
<?php

use Drupal\Core\Database\Connection;
use Drupal\taxonomy\Entity\Term;

/**
 * @file
 * Install, update and uninstall functions for the permissions_by_term module.
 */

/**
 * Implements hook_schema().
 */
function permissions_by_term_schema() {
  $schema = [];

  // Specifications for tabe 'permissions_by_term_user'.
  $schema['permissions_by_term_user'] = [
    'description' => "Stores the tid's to which a user has permission by his uid.",
    'fields' => [
      'tid' => [
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ],
      'uid' => [
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ],
      'langcode' => [
        'type' => 'varchar_ascii',
        'length' => 12,
        'not null' => TRUE,
        'default' => '',
      ],
    ],
    'primary key' => [
      'tid',
      'uid',
      'langcode',
    ],
  ];

  // Specifications for tabe 'permissions_by_term_role'.
  $schema['permissions_by_term_role'] = [
    'description' => "Stores the tid's to which user's are allowed to by rid.",
    'fields' => [
      'tid' => [
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ],
      'rid' => [
        'type' => 'varchar',
        'length' => 60,
        'not null' => TRUE,
      ],
      'langcode' => [
        'type' => 'varchar_ascii',
        'length' => 12,
        'not null' => TRUE,
        'default' => '',
      ],
    ],
    'primary key' => [
      'tid',
      'rid',
      'langcode',
    ],
  ];
  return $schema;
}

/**
 * Implements hook_requirements().
 */
function permissions_by_term_requirements(string $phase) {
  $requirements = [];
  if ($phase === 'update' || $phase === 'runtime') {
    if (!\Drupal::moduleHandler()
      ->moduleExists('dynamic_page_cache')) {
      $requirements['permissions_by_term_dynamic_page_cache_missing'] = [
        'severity' => REQUIREMENT_WARNING,
        'title' => t('Permissions by Term: dynamic page cache is not installed.'),
        'description' => t('The "Internal Dynamic Page Cache" module must be installed. Otherwise there cannot be run any permission check for your views pages. Ensure having the dynamic page cache module and page cache module installed.'),
        'value' => t('No dynamic page cache'),
      ];
    }
    if (!\Drupal::moduleHandler()
      ->moduleExists('page_cache')) {
      $requirements['permissions_by_term_page_cache_missing'] = [
        'severity' => REQUIREMENT_WARNING,
        'title' => t('Permissions by Term: page cache module is not installed.'),
        'description' => t("The 'Page Cache' module is not installed. Without this module your site's performance will be slow. Ensure having the dynamic page cache module and page cache module installed."),
        'value' => t('No page cache'),
      ];
    }
  }
  return $requirements;
}

/**
 * Implements hook_install().
 */
function permissions_by_term_install() {
  node_access_rebuild(TRUE);
}

/**
 * Implements hook_uninstall().
 */
function permissions_by_term_uninstall() {
  node_access_rebuild(TRUE);
}

/**
 * Mandatory initial run of node_access_rebuild() Drupal core function.
 */
function permissions_by_term_update_8113() {
  node_access_rebuild(TRUE);
}

/**
 * Force a node access rebuild to fix node access grants.
 */
function permissions_by_term_update_8114() {
  node_access_rebuild(TRUE);
}

/**
 * Force a node access rebuild to fix multilingual node access grants.
 */
function permissions_by_term_update_8142() {
  node_access_rebuild(TRUE);
}

/**
 * Add field for langcode in user and role permission tables.
 */
function permissions_by_term_update_8145() {
  $database = \Drupal::database();
  $schema = $database
    ->schema();
  $spec = [
    'type' => 'varchar_ascii',
    'length' => 12,
    'not null' => TRUE,
    'default' => '',
  ];
  $schema
    ->addField('permissions_by_term_role', 'langcode', $spec);
  $schema
    ->addField('permissions_by_term_user', 'langcode', $spec);
}

/**
 * Add langcode to each permission restriction dataset. Cleanup dangling datasets, with no relation to any taxonomy term.
 */
function permissions_by_term_update_8152() {

  /**
   * @var Connection $database
   */
  $database = \Drupal::service('database');
  $userTerms = $database
    ->query("SELECT tid FROM {permissions_by_term_user} WHERE langcode = '' OR langcode IS NULL")
    ->fetchAll();
  $roleTerms = $database
    ->query("SELECT tid FROM {permissions_by_term_role} WHERE langcode = '' OR langcode IS NULL")
    ->fetchAll();
  foreach ($roleTerms as $roleTerm) {
    if (!empty($term = Term::load($roleTerm->tid))) {
      $termLangcode = $term
        ->get('langcode')
        ->getLangcode();
      if (!empty($termLangcode)) {
        $database
          ->query("UPDATE {permissions_by_term_role} SET langcode = :langcode WHERE tid = :tid", [
          ':langcode' => $termLangcode,
          ':tid' => $roleTerm->tid,
        ]);
      }
    }
    else {
      $database
        ->query("DELETE FROM {permissions_by_term_role} WHERE tid = :tid", [
        ':tid' => $roleTerm->tid,
      ]);
    }
  }
  foreach ($userTerms as $userTerm) {
    if (!empty($term = Term::load($userTerm->tid))) {
      $termLangcode = $term
        ->get('langcode')
        ->getLangcode();
      if (!empty($termLangcode)) {
        $database
          ->query("UPDATE {permissions_by_term_user} SET langcode = :langcode WHERE tid = :tid", [
          ':langcode' => $termLangcode,
          ':tid' => $userTerm->tid,
        ]);
      }
    }
    else {
      $database
        ->query("DELETE FROM {permissions_by_term_user} WHERE tid = :tid", [
        ':tid' => $userTerm->tid,
      ]);
    }
  }
}

/**
 * Remove primary keys due language codes.
 */
function permissions_by_term_update_8153() {
  $database = \Drupal::database();
  $schema = $database
    ->schema();
  $schema
    ->dropPrimaryKey('permissions_by_term_role');
  $schema
    ->dropPrimaryKey('permissions_by_term_user');
}

/**
 * Add composite primary key via unique table column combinations.
 */
function permissions_by_term_update_8230() {
  $database = \Drupal::database();
  $schema = $database
    ->schema();
  $schema
    ->dropPrimaryKey('permissions_by_term_role');
  $schema
    ->addPrimaryKey('permissions_by_term_role', [
    'tid',
    'rid',
    'langcode',
  ]);
  $schema
    ->dropPrimaryKey('permissions_by_term_user');
  $schema
    ->addPrimaryKey('permissions_by_term_user', [
    'tid',
    'uid',
    'langcode',
  ]);
}

Functions

Namesort descending Description
permissions_by_term_install Implements hook_install().
permissions_by_term_requirements Implements hook_requirements().
permissions_by_term_schema Implements hook_schema().
permissions_by_term_uninstall Implements hook_uninstall().
permissions_by_term_update_8113 Mandatory initial run of node_access_rebuild() Drupal core function.
permissions_by_term_update_8114 Force a node access rebuild to fix node access grants.
permissions_by_term_update_8142 Force a node access rebuild to fix multilingual node access grants.
permissions_by_term_update_8145 Add field for langcode in user and role permission tables.
permissions_by_term_update_8152 Add langcode to each permission restriction dataset. Cleanup dangling datasets, with no relation to any taxonomy term.
permissions_by_term_update_8153 Remove primary keys due language codes.
permissions_by_term_update_8230 Add composite primary key via unique table column combinations.