You are here

permissions_by_term.install in Permissions by Term 8

Same filename and directory in other branches
  1. 8.2 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' => [],
  ];

  // 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' => [],
  ];
  return $schema;
}

/**
 * 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');
}

Functions

Namesort descending Description
permissions_by_term_install Implements hook_install().
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.