Drupal PHP Cross Reference Content Management Systems

Source: /modules/node/tests/node_access_test.module - 226 lines - 6200 bytes - Summary - Text - Print

   1  <?php
   2  
   3  /**
   4   * @file
   5   * Dummy module implementing node access related hooks to test API interaction
   6   * with the Node module. This module restricts view permission to those with
   7   * a special 'node test view' permission.
   8   */
   9  
  10  /**
  11   * Implements hook_node_grants().
  12   */
  13  function node_access_test_node_grants($account, $op) {
  14    $grants = array();
  15    // First grant a grant to the author for own content.
  16    $grants['node_access_test_author'] = array($account->uid);
  17    if ($op == 'view' && user_access('node test view', $account)) {
  18      $grants['node_access_test'] = array(8888, 8889);
  19    }
  20    if ($op == 'view' && $account->uid == variable_get('node_test_node_access_all_uid', 0)) {
  21      $grants['node_access_all'] = array(0);
  22    }
  23    return $grants;
  24  }
  25  
  26  /**
  27   * Implements hook_node_access_records().
  28   */
  29  function node_access_test_node_access_records($node) {
  30    $grants = array();
  31    // For NodeAccessBaseTableTestCase, only set records for private nodes.
  32    if (!variable_get('node_access_test_private') || $node->private) {
  33      $grants[] = array(
  34        'realm' => 'node_access_test',
  35        'gid' => 8888,
  36        'grant_view' => 1,
  37        'grant_update' => 0,
  38        'grant_delete' => 0,
  39        'priority' => 0,
  40      );
  41      $grants[] = array(
  42        'realm' => 'node_access_test',
  43        'gid' => 8889,
  44        'grant_view' => 1,
  45        'grant_update' => 0,
  46        'grant_delete' => 0,
  47        'priority' => 0,
  48      );
  49      // For the author realm, the GID is equivalent to a UID, which
  50      // means there are many many groups of just 1 user.
  51      $grants[] = array(
  52        'realm' => 'node_access_test_author',
  53        'gid' => $node->uid,
  54        'grant_view' => 1,
  55        'grant_update' => 1,
  56        'grant_delete' => 1,
  57        'priority' => 0,
  58      );
  59    }
  60  
  61    return $grants;
  62  }
  63  
  64  /**
  65   * Implements hook_permission().
  66   *
  67   * Sets up permissions for this module.
  68   */
  69  function node_access_test_permission() {
  70    return array('node test view' => array('title' => 'View content'));
  71  }
  72  
  73  /**
  74   * Implements hook_menu().
  75   *
  76   * Sets up a page that lists nodes.
  77   */
  78  function node_access_test_menu() {
  79    $items = array();
  80    $items['node_access_test_page'] = array(
  81      'title' => 'Node access test',
  82      'page callback' => 'node_access_test_page',
  83      'access arguments' => array('access content'),
  84      'type' => MENU_SUGGESTED_ITEM,
  85    );
  86    $items['node_access_entity_test_page'] = array(
  87      'title' => 'Node access test',
  88      'page callback' => 'node_access_entity_test_page',
  89      'access arguments' => array('access content'),
  90      'type' => MENU_SUGGESTED_ITEM,
  91    );
  92    return $items;
  93  }
  94  
  95  /**
  96   * Page callback for node access test page.
  97   *
  98   * Page should say "No nodes" if there are no nodes, and "Yes, # nodes" (with
  99   * the number filled in) if there were nodes the user could access. Also, the
 100   * database query is shown, and a list of the node IDs, for debugging purposes.
 101   * And if there is a query exception, the page says "Exception" and gives the
 102   * error.
 103   */
 104  function node_access_test_page() {
 105    $output = '';
 106  
 107    try {
 108      $query = db_select('node', 'mytab')
 109        ->fields('mytab');
 110      $query->addTag('node_access');
 111      $result = $query->execute()->fetchAll();
 112  
 113      if (count($result)) {
 114        $output .= '<p>Yes, ' . count($result) . ' nodes</p>';
 115        $output .= '<ul>';
 116        foreach ($result as $item) {
 117          $output .= '<li>' . $item->nid . '</li>';
 118        }
 119        $output .= '</ul>';
 120      }
 121      else {
 122        $output .= '<p>No nodes</p>';
 123      }
 124  
 125      $output .= '<p>' . ((string) $query ) . '</p>';
 126    }
 127    catch (Exception $e) {
 128      $output = '<p>Exception</p>';
 129      $output .= '<p>' . $e->getMessage() . '</p>';
 130    }
 131  
 132    return $output;
 133  }
 134  
 135  /**
 136   * Page callback for node access entity test page.
 137   *
 138   * Page should say "No nodes" if there are no nodes, and "Yes, # nodes" (with
 139   * the number filled in) if there were nodes the user could access. Also, the
 140   * database query is shown, and a list of the node IDs, for debugging purposes.
 141   * And if there is a query exception, the page says "Exception" and gives the
 142   * error.
 143   */
 144  function node_access_entity_test_page() {
 145    $output = '';
 146    try {
 147      $query = new EntityFieldQuery;
 148      $result = $query->fieldCondition('body', 'value', 'A', 'STARTS_WITH')->execute();
 149      if (!empty($result['node'])) {
 150        $output .= '<p>Yes, ' . count($result['node']) . ' nodes</p>';
 151        $output .= '<ul>';
 152        foreach ($result['node'] as $nid => $v) {
 153          $output .= '<li>' . $nid . '</li>';
 154        }
 155        $output .= '</ul>';
 156      }
 157      else {
 158        $output .= '<p>No nodes</p>';
 159      }
 160    }
 161    catch (Exception $e) {
 162      $output = '<p>Exception</p>';
 163      $output .= '<p>' . $e->getMessage() . '</p>';
 164    }
 165  
 166    return $output;
 167  }
 168  
 169  /**
 170   * Implements hook_form_BASE_FORM_ID_alter().
 171   */
 172  function node_access_test_form_node_form_alter(&$form, $form_state) {
 173    // Only show this checkbox for NodeAccessBaseTableTestCase.
 174    if (variable_get('node_access_test_private')) {
 175      $form['private'] = array(
 176        '#type' => 'checkbox',
 177        '#title' => t('Private'),
 178        '#description' => t('Check here if this content should be set private and only shown to privileged users.'),
 179        '#default_value' => isset($form['#node']->private) ? $form['#node']->private : FALSE,
 180      );
 181    }
 182  }
 183  
 184  /**
 185   * Implements hook_node_load().
 186   */
 187  function node_access_test_node_load($nodes, $types) {
 188    $result = db_query('SELECT nid, private FROM {node_access_test} WHERE nid IN(:nids)', array(':nids' => array_keys($nodes)));
 189    foreach ($result as $record) {
 190      $nodes[$record->nid]->private = $record->private;
 191    }
 192  }
 193  
 194  /**
 195   * Implements hook_node_delete().
 196   */
 197  
 198  function node_access_test_node_delete($node) {
 199    db_delete('node_access_test')->condition('nid', $node->nid)->execute();
 200  }
 201  
 202  /**
 203   * Implements hook_node_insert().
 204   */
 205  function node_access_test_node_insert($node) {
 206    _node_access_test_node_write($node);
 207  }
 208  
 209  /**
 210   * Implements hook_nodeapi_update().
 211   */
 212  function node_access_test_node_update($node) {
 213    _node_access_test_node_write($node);
 214  }
 215  
 216  /**
 217   * Helper for node insert/update.
 218   */
 219  function _node_access_test_node_write($node) {
 220    if (isset($node->private)) {
 221      db_merge('node_access_test')
 222        ->key(array('nid' => $node->nid))
 223        ->fields(array('private' => (int) $node->private))
 224        ->execute();
 225    }
 226  }

title

Description

title

Description

title

Description

title

title

Body