You are here

function taxonomy_access_fix_access in Taxonomy access fix 8

Same name and namespace in other branches
  1. 7.2 taxonomy_access_fix.module \taxonomy_access_fix_access()
  2. 7 taxonomy_access_fix.module \taxonomy_access_fix_access()

Access callback for common CUSTOM taxonomy operations.

4 calls to taxonomy_access_fix_access()
taxonomy_access_fix_form_taxonomy_overview_terms_alter in ./taxonomy_access_fix.module
Implements hook_form_FORM_ID_alter() for taxonomy_overview_terms().
taxonomy_access_fix_route_access in ./taxonomy_access_fix.module
Route access callback
VocabularyListBuilder::getOperations in src/VocabularyListBuilder.php
Override Drupal\Core\Entity\EntityListBuilder::getOperations().
VocabularyListBuilder::load in src/VocabularyListBuilder.php
Override Drupal\Core\Config\Entity\ConfigEntityListBuilder::load().

File

./taxonomy_access_fix.module, line 98

Code

function taxonomy_access_fix_access($op, $vocabulary = NULL) {

  // Admin: always.
  if (Drupal::currentUser()
    ->hasPermission('administer taxonomy')) {
    return TRUE;
  }
  if ($vocabulary && is_string($vocabulary)) {
    $vocabulary = Vocabulary::load($vocabulary);
  }

  // Others: well, that depends.
  switch ($op) {
    case 'index':

      // Allow access when the user has access to at least one vocabulary.
      foreach (Vocabulary::loadMultiple() as $vocabulary) {
        if (taxonomy_access_fix_access('list terms', $vocabulary)) {
          return TRUE;
        }
      }
      break;
    case 'list terms':
      if ($vocabulary) {
        $vid = $vocabulary
          ->id();
        $perm1 = sprintf('edit terms in %s', $vid);
        $perm2 = sprintf('delete terms in %s', $vid);
        $perm3 = sprintf('add terms in %s', $vid);
        $perm4 = sprintf('reorder terms in %s', $vid);
        if (Drupal::currentUser()
          ->hasPermission($perm1) || Drupal::currentUser()
          ->hasPermission($perm2) || Drupal::currentUser()
          ->hasPermission($perm3) || Drupal::currentUser()
          ->hasPermission($perm4)) {
          return TRUE;
        }
      }
      break;
    case 'reorder terms':
      if ($vocabulary) {
        if (Drupal::currentUser()
          ->hasPermission('reorder terms in ' . $vocabulary
          ->id())) {
          return TRUE;
        }
      }
      break;
    case 'add terms':
      if ($vocabulary) {
        if (Drupal::currentUser()
          ->hasPermission('add terms in ' . $vocabulary
          ->id())) {
          return TRUE;
        }
      }
      break;
    case 'edit terms':
      if ($vocabulary) {
        if (Drupal::currentUser()
          ->hasPermission('edit terms in ' . $vocabulary
          ->id())) {
          return TRUE;
        }
      }
      break;
    case 'delete terms':
      if ($vocabulary) {
        if (Drupal::currentUser()
          ->hasPermission('delete terms in ' . $vocabulary
          ->id())) {
          return TRUE;
        }
      }
      break;
  }
  return FALSE;
}