You are here

tft_taxonomy_access.module in Taxonomy File Tree 7.2

Taxonomy Access integration.

File

modules/tft_taxonomy_access/tft_taxonomy_access.module
View source
<?php

/**
 * @file
 * Taxonomy Access integration.
 */

/**
 * Implements hook_menu().
 */
function tft_taxonomy_access_menu() {
  return array(
    'tft/term/access/%' => array(
      'title' => "Access control",
      'page callback' => 'drupal_get_form',
      'page arguments' => array(
        'tft_taxonomy_access_access_form',
        3,
      ),
      'access callback' => array(
        'tft_taxonomy_access_access',
      ),
      'access arguments' => array(
        3,
      ),
      'type' => MENU_CALLBACK,
    ),
  );
}

/**
 * Implements hook_permission().
 */
function tft_taxonomy_access_permission() {
  return array(
    'tft manage access control' => array(
      'title' => t("Manage folder access control"),
      'description' => t("Manage access rules for folders in the tree."),
    ),
  );
}

/**
 * Implements hook_og_permission().
 */
function tft_taxonomy_access_og_permission() {
  return tft_taxonomy_access_permission();
}

/**
 * Implements hook_tft_folder_menu_links_alter().
 */
function tft_taxonomy_access_tft_folder_menu_links_alter(&$links, $tid) {
  if ($tid && tft_taxonomy_access_access($tid)) {
    $links['control_item_access'] = array(
      'title' => t("manage access"),
      'href' => "tft/term/access/{$tid}",
      'attributes' => array(
        'id' => 'manage-access',
        'class' => array(
          'folder-menu-ops-link',
        ),
      ),
      'query' => array(
        'destination' => (!empty($_SESSION['tft']['q']) ? $_SESSION['tft']['q'] : '') . "#tft/{$tid}",
      ),
    );
  }
}

/**
 * Implements hook_theme().
 */
function tft_taxonomy_access_theme() {
  return array(
    'tft_taxonomy_access_access_form' => array(
      'render element' => 'form',
    ),
  );
}

/**
 * Implements hook_tft_term_access().
 */
function tft_taxonomy_access_tft_term_access($tid, $account = NULL, $op = 'view') {
  if (!isset($account)) {
    global $user;
    $account = $user;
  }
  if ($op == 'edit' || $op == 'add-file') {
    $op = 'update';
  }
  foreach ($account->roles as $rid => $role) {
    $result = tft_taxonomy_access_load_term_grant($op, $tid, $rid);
    if (isset($result)) {
      return $result;
    }
  }
}

/**
 * Access callback: check if user has access to manage this term access.
 */
function tft_taxonomy_access_access($tid) {
  if (!module_exists('tft_og')) {
    return user_access('tft manage access control');
  }
  else {
    if ($gid = tft_og_get_og_nid($tid)) {
      return og_user_access('node', $gid, 'tft manage access control');
    }
    else {
      return user_access('tft manage access control');
    }
  }
}

/**
 * Form callback: access control form.
 */
function tft_taxonomy_access_access_form($form, $form_state, $tid) {
  $parts = explode('#', str_replace('%23', '#', !empty($_GET['destination']) ? $_GET['destination'] : ''));
  $form['actions']['cancel'] = array(
    '#markup' => l(t("cancel"), $parts[0], array(
      'attributes' => array(
        'class' => array(
          'tft-cancel-button',
        ),
      ),
      'fragment' => isset($parts[1]) ? $parts[1] : '',
    )),
  );
  if (!$tid) {
    drupal_set_message(t("You cannot set any access rules for the root folder. If you want to prevent people to access any files, remove the <em>Access file tree</em> permission."));
    return $form;
  }
  else {
    $form['tid'] = array(
      '#type' => 'value',
      '#value' => $tid,
    );
    $form['grants']['#tree'] = TRUE;
    $roles = _taxonomy_access_user_roles();
    $active_rids = db_query('SELECT rid FROM {taxonomy_access_default} WHERE vid = 0')
      ->fetchCol();
    foreach ($active_rids as $rid) {
      $form['grants'][$rid]['#tree'] = TRUE;
      $form['grants'][$rid]['view'] = array(
        '#title' => t("Allow %role to view folder", array(
          '%role' => $roles[$rid],
        )),
        '#type' => 'checkbox',
        '#default_value' => tft_taxonomy_access_load_term_grant('view', $tid, $rid),
      );
      $form['grants'][$rid]['update'] = array(
        '#title' => t("Allow %role to edit folder", array(
          '%role' => $roles[$rid],
        )),
        '#type' => 'checkbox',
        '#default_value' => tft_taxonomy_access_load_term_grant('update', $tid, $rid),
      );
      $form['grants'][$rid]['delete'] = array(
        '#title' => t("Allow %role to delete folder", array(
          '%role' => $roles[$rid],
        )),
        '#type' => 'checkbox',
        '#default_value' => tft_taxonomy_access_load_term_grant('delete', $tid, $rid),
      );
    }
    $form['actions']['#weight'] = 100;
    $form['actions']['submit'] = array(
      '#type' => 'submit',
      '#value' => t("Save"),
      '#weight' => -10,
    );
    return $form;
  }
}

/**
 * Submit callback for tft_taxonomy_access_access_form.
 */
function tft_taxonomy_access_access_form_submit($form, $form_state) {

  // We map 0 to 'Deny' (2) and 1 to 'Allow' (1). We don't do 'Ignore'.
  foreach ($form_state['values']['grants'] as $rid => $values) {
    taxonomy_access_set_term_grants(array(
      $form_state['values']['tid'] => (object) array(
        'tid' => $form_state['values']['tid'],
        'rid' => $rid,
        'grant_view' => $values['view'] == 0 ? 2 : 1,
        'grant_update' => $values['update'] == 0 ? 2 : 1,
        'grant_delete' => $values['delete'] == 0 ? 2 : 1,
        'grant_create' => 1,
        'grant_list' => 1,
      ),
    ));
  }
}

/**
 * Theme callback for tft_taxonomy_access_access_form.
 */
function theme_tft_taxonomy_access_access_form($vars) {
  $form = $vars['form'];
  drupal_add_css(drupal_get_path('module', 'tft_taxonomy_access') . '/css/tft_taxonomy_access.css');
  $header = array(
    t("Role"),
    t("View"),
    t("Update"),
    t("Delete"),
  );
  $rows = array();
  $roles = _taxonomy_access_user_roles();
  foreach ($form['grants'] as $rid => &$items) {
    if (is_numeric($rid)) {
      unset($items['view']['#title'], $items['update']['#title'], $items['delete']['#title']);
      $rows[] = array(
        $roles[$rid],
        drupal_render($items['view']),
        drupal_render($items['update']),
        drupal_render($items['delete']),
      );
    }
  }
  return theme('table', array(
    'header' => $header,
    'rows' => $rows,
  )) . drupal_render_children($form);
}

/**
 * Helper function to load a specific grant for a term.
 *
 * @param string $op
 * @param int $tid
 * @param int $rid
 *
 * @return int|null
 */
function tft_taxonomy_access_load_term_grant($op, $tid, $rid) {
  $term = taxonomy_term_load($tid);
  $vid = variable_get('tft_vocabulary_vid', 0);
  if ($term->vid != $vid) {
    return NULL;
  }
  $default_grants = db_query('SELECT * FROM {taxonomy_access_default} WHERE rid = :rid', array(
    ':rid' => $rid,
  ))
    ->fetchAllAssoc('vid', PDO::FETCH_ASSOC);
  $grants = db_query('SELECT * FROM {taxonomy_access_term} WHERE rid = :rid AND tid = :tid', array(
    ':rid' => $rid,
    ':tid' => $tid,
  ))
    ->fetchAllAssoc('tid', PDO::FETCH_ASSOC);
  $default_grants = !empty($default_grants[$vid]) ? $default_grants[$vid] : $default_grants[0];
  $default_grant = in_array($default_grants["grant_{$op}"], array(
    0,
    2,
  )) ? 0 : 1;
  return !empty($grants[$tid]["grant_{$op}"]) ? in_array($grants[$tid]["grant_{$op}"], array(
    0,
    2,
  )) ? 0 : 1 : $default_grant;
}

Functions

Namesort descending Description
tft_taxonomy_access_access Access callback: check if user has access to manage this term access.
tft_taxonomy_access_access_form Form callback: access control form.
tft_taxonomy_access_access_form_submit Submit callback for tft_taxonomy_access_access_form.
tft_taxonomy_access_load_term_grant Helper function to load a specific grant for a term.
tft_taxonomy_access_menu Implements hook_menu().
tft_taxonomy_access_og_permission Implements hook_og_permission().
tft_taxonomy_access_permission Implements hook_permission().
tft_taxonomy_access_tft_folder_menu_links_alter Implements hook_tft_folder_menu_links_alter().
tft_taxonomy_access_tft_term_access Implements hook_tft_term_access().
tft_taxonomy_access_theme Implements hook_theme().
theme_tft_taxonomy_access_access_form Theme callback for tft_taxonomy_access_access_form.