You are here

vppr.module in Vocabulary Permissions Per Role 8

Same filename and directory in other branches
  1. 6 vppr.module
  2. 7 vppr.module

Vocabulary Permissions Per Role.

Allows adding to/editing terms of/removing terms from vocabularies per role.

File

vppr.module
View source
<?php

/**
 * @file
 * Vocabulary Permissions Per Role.
 *
 * Allows adding to/editing terms of/removing terms from vocabularies per role.
 */
use Drupal\taxonomy\Entity\Vocabulary;
use Symfony\Component\Routing\Route;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\Core\Access\AccessResult;
use Drupal\vppr\VpprPermissions;
use Drupal\Core\Entity\EntityInterface;

/**
 * Implements hook_entity_type_alter().
 */
function vppr_entity_type_alter(array &$entity_types) {
  $entity_types['taxonomy_vocabulary']
    ->setHandlerClass('list_builder', 'Drupal\\vppr\\VocabularyListBuilder');
}

/**
 * Route access callback.
 */
function vppr_route_access(Route $route, RouteMatchInterface $match, AccountProxyInterface $proxy) {
  $op = $route
    ->getOption('op');
  $vocabulary = $match
    ->getParameter('taxonomy_vocabulary');
  $term_id = $match
    ->getRawParameters()
    ->getDigits('taxonomy_term');
  if (!$vocabulary && is_numeric($term_id)) {

    // Case: Add/Edit/Delete term ids.
    $vocabulary_id = \Drupal::database()
      ->select('taxonomy_term_data', 't')
      ->fields('t', [
      'vid',
    ])
      ->condition('tid', $term_id)
      ->execute()
      ->fetchField();
  }
  elseif ($vocabulary && is_string($vocabulary)) {
    $vocabulary = Vocabulary::load($vocabulary);
    $vocabulary_id = $vocabulary
      ->id();
  }
  elseif (!$vocabulary) {

    // Do nothing when vocab is null.
    $vocabulary_id = NULL;
  }
  else {
    $vocabulary_id = $vocabulary
      ->id();
  }
  if (vppr_access($op, $vocabulary_id)) {
    return AccessResult::allowed();
  }
  return AccessResult::forbidden();
}

/**
 * Access callback for common VPPR taxonomy operations.
 */
function vppr_access($op = NULL, $vocabulary_id = NULL) {
  $perm = VpprPermissions::permissions();

  // Admin: always.
  if (Drupal::currentUser()
    ->hasPermission('administer taxonomy')) {
    return TRUE;
  }
  if ($op == 'index') {

    // Vocabulary list page.
    foreach ($perm as $key => $value) {

      // Check permission for each individual vocab so that to display
      // in the list or not.
      if (Drupal::currentUser()
        ->hasPermission($key)) {
        return TRUE;
      }
    }
  }
  else {

    // If option is not set i.e all the other options except vocabulary listing.
    if ($vocabulary_id) {
      if (Drupal::currentUser()
        ->hasPermission('administer ' . $vocabulary_id . ' vocabulary terms')) {
        return TRUE;
      }
    }
  }
  return FALSE;
}

/**
 *  Implements hook_entity_access()
 */
function vppr_entity_access(EntityInterface $entity) {
  $entity_type_id = $entity
    ->getEntityTypeId();
  if ($entity_type_id == 'taxonomy_vocabulary' && vppr_access('', $entity
    ->getConfigTarget()) || $entity_type_id == 'taxonomy_term') {
    return AccessResult::allowed();
  }
  return AccessResult::neutral();
}

Functions

Namesort descending Description
vppr_access Access callback for common VPPR taxonomy operations.
vppr_entity_access Implements hook_entity_access()
vppr_entity_type_alter Implements hook_entity_type_alter().
vppr_route_access Route access callback.