You are here

taxonomy_tools_role_access.module in Taxonomy Tools 8

Same filename and directory in other branches
  1. 7 taxonomy_tools_role_access/taxonomy_tools_role_access.module

Drupal hooks and functions to work with taxonomy terms.

Access to a specific taxonomy term can be set for each user role separately.

File

taxonomy_tools_role_access/taxonomy_tools_role_access.module
View source
<?php

/**
 * @file
 * Drupal hooks and functions to work with taxonomy terms.
 *
 * Access to a specific taxonomy term can be set for each user role separately.
 */

/**
 * Implements hook_menu().
 */
function taxonomy_tools_role_access_menu() {
  $items['admin/config/taxonomy-tools/role-access'] = array(
    'title' => 'Taxonomy Role Access',
    'description' => 'Configure which vocabularies will use Taxonomy Role Access and which user roles will be controlled.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'taxonomy_tools_role_access_admin_form',
    ),
    'access arguments' => array(
      'administer taxonomy role access configuration',
    ),
    'file' => 'taxonomy_tools_role_access.admin.inc',
    'file path' => drupal_get_path('module', 'taxonomy_tools_role_access'),
  );
  $items['taxonomy-role-access/nojs/%taxonomy_vocabulary_machine_name'] = array(
    'page callback' => 'taxonomy_tools_role_access_change_access',
    'page arguments' => array(
      1,
      2,
      3,
      4,
      5,
      6,
    ),
    'access arguments' => array(
      'administer taxonomy role access',
    ),
    'type' => MENU_CALLBACK,
  );
  $items['taxonomy-role-access/ajax/%taxonomy_vocabulary_machine_name'] = array(
    'delivery callback' => 'ajax_deliver',
  ) + $items['taxonomy-role-access/nojs/%taxonomy_vocabulary_machine_name'];
  return $items;
}

/**
 * Implements hook_admin_paths().
 */
function taxonomy_tools_role_access_admin_paths() {
  $paths = array(
    'taxonomy-role-access/ajax/*' => TRUE,
  );
  return $paths;
}

/**
 * Implements hook_permission().
 */
function taxonomy_tools_role_access_permission() {
  return array(
    'administer taxonomy role access' => array(
      'title' => t('Administer Taxonomy Role Access'),
      'description' => t('User is able to manage user role access to taxonomy terms.'),
    ),
    'bypass taxonomy role access' => array(
      'title' => t('Bypass Taxonomy Role Access restrictions'),
      'description' => t('User is able to view taxonomy terms although the access is restricted by Taxonomy Role Access module.'),
    ),
    'administer taxonomy role access configuration' => array(
      'title' => t('Administer Taxonomy Role Access configuration'),
      'description' => t('User is able to access and use Taxonomy Role Access configuration page.'),
    ),
  );
}

/**
 * Ajax callback for processing changes in taxonomy term access rules.
 *
 * @param string $ajax
 *   String representing if ajax is used;
 *   "ajax" or "nojs".
 * @param stdClass $vocabulary
 *   An object representing vocabulary.
 * @param int $tid
 *   Taxonomy term ID.
 * @param int $rid
 *   User role ID.
 * @param string $access
 *   String representing how access is being changed;
 *   "true" or "false".
 * @param int $parent
 *   Parent taxonomy term ID.
 *
 * @return array
 *   An array containing ajax commands.
 */
function taxonomy_tools_role_access_change_access($ajax, $vocabulary, $tid, $rid, $access, $parent = 0) {
  $is_ajax = $ajax === 'ajax';
  $changed = FALSE;
  if ($access == 'false') {

    // Access is being changed to "deny".
    $query = db_insert('taxonomy_tools_role_access');
    $query
      ->fields(array(
      'tid' => $tid,
      'rid' => $rid,
    ));
    $query
      ->execute();
    $changed = TRUE;
  }
  else {

    // Access is being changed to "allow".
    $query = db_delete('taxonomy_tools_role_access');
    $query
      ->condition(db_and()
      ->condition('tid', $tid)
      ->condition('rid', $rid));
    $result = $query
      ->execute();
    if ($result) {
      $changed = TRUE;
    }
  }

  // Only if access settings have really changed.
  if ($changed) {

    // Rebuild node access records.
    taxonomy_tools_rebuild_access_grants($tid);

    // Allow other modules to act on term access change.
    module_invoke_all('taxonomy_tools_role_access_term_access_change', $tid);
  }
  if ($is_ajax) {
    $commands = array();
    if ($changed) {
      $attributes = array(
        'class' => array(
          'use-ajax',
          'access_' . $tid . '_' . $rid,
          $access,
        ),
        'title' => t('change'),
      );
      $opposite = $access === 'true' ? 'false' : 'true';

      // Replace the link.
      $link = 'taxonomy-role-access/nojs/' . $vocabulary->machine_name . '/' . $tid . '/' . $rid . '/' . $opposite;
      if ($parent > 0) {
        $link .= '/' . $parent;
      }
      $element = array(
        '#type' => 'link',
        '#title' => '',
        '#href' => $link,
        '#attributes' => $attributes,
        '#access' => user_access('administer taxonomy role access'),
      );
      $commands[] = ajax_command_replace('a.access_' . $tid . '_' . $rid, drupal_render($element));
    }
    return array(
      '#type' => 'ajax',
      '#commands' => $commands,
    );
  }
  else {

    // Redirect back to overview page.
    $path = 'admin/structure/taxonomy/' . $vocabulary->machine_name . '/overview';
    if ($parent > 0) {
      $path .= '/' . $parent;
    }
    drupal_goto($path);
  }
}

/**
 * Checks the taxonomy term availability to specified user role.
 *
 * @param string $tid
 *   Taxonomy term identifcator
 * @param string $rid
 *   User role identificator
 *
 * @return bool
 *   TRUE if the taxonomy term is available to the user role,
 *   otherwise - FALSE.
 */
function taxonomy_tools_role_access_get_access($tid, $rid) {
  $access = TRUE;
  $query = db_select('taxonomy_tools_role_access', 'foo');
  $query
    ->fields('foo');
  $query
    ->condition(db_and()
    ->condition('foo.tid', $tid)
    ->condition('foo.rid', $rid));
  $result = $query
    ->execute()
    ->fetchObject();
  if ($result) {
    $access = FALSE;
  }
  if ($access) {

    // Check parent access.
    $query = db_select('taxonomy_term_hierarchy', 'foo');
    $query
      ->addField('foo', 'parent');
    $query
      ->condition('foo.tid', $tid);
    $parent = $query
      ->execute()
      ->fetchField();
    if ($parent) {
      $access = taxonomy_tools_role_access_get_access($parent, $rid);
    }
  }
  return $access;
}

/**
 * Implements hook_user_role_delete().
 */
function taxonomy_tools_role_access_user_role_delete($role) {
  $r_config = variable_get('taxonomy_tools_role_access_role_config', array());
  if (!empty($r_config) && in_array($role->rid, $r_config)) {

    // The deleted role must removed from Taxonomy Role Access settings.
    $v_config = array_filter(variable_get('taxonomy_tools_role_access_vocab_config', array()));
    $form_state = array();
    foreach ($v_config as $machine_name) {
      $form_state['values']['taxonomy_tools_role_access_vocab_config'][$machine_name] = $machine_name;
    }
    unset($r_config[$role->rid]);
    foreach ($r_config as $rid) {
      if ($rid > 0) {
        $form_state['values']['taxonomy_tools_role_access_role_config'][$rid] = $rid;
      }
    }
    require_once drupal_get_path('module', 'taxonomy_tools_role_access') . '/taxonomy_tools_role_access.admin.inc';
    drupal_form_submit('taxonomy_tools_role_access_admin_form', $form_state);
  }
}

Functions

Namesort descending Description
taxonomy_tools_role_access_admin_paths Implements hook_admin_paths().
taxonomy_tools_role_access_change_access Ajax callback for processing changes in taxonomy term access rules.
taxonomy_tools_role_access_get_access Checks the taxonomy term availability to specified user role.
taxonomy_tools_role_access_menu Implements hook_menu().
taxonomy_tools_role_access_permission Implements hook_permission().
taxonomy_tools_role_access_user_role_delete Implements hook_user_role_delete().