You are here

module_grants_test_base.module in Module Grants 7

All dummy modules will assign grants based on whether node's title containing a particular keyword and user's permission. All function takes $grants, which is an array keyed by realm, and value is another array of keyword => gid, keyword should be unique across all test modules, since it will be used as permission.

File

tests/module_grants_test_base.module
View source
<?php

/**
 * @file
 * All dummy modules will assign grants based on whether node's title containing a particular
 * keyword and user's permission.
 * All function takes $grants, which is an array keyed by realm, and value is another array of
 * keyword => gid, keyword should be unique across all test modules, since it will be used as
 * permission.
 */

/**
 * Used for implement hook_permission().
 */
function _module_grants_test_get_permissions($grants) {
  $permissions = array();
  foreach ($grants as $realm => $gids) {
    foreach ($gids as $keyword => $gid) {
      $permissions[$keyword] = array(
        'title' => 'Module grants ' . $keyword . ' permission for realm ' . $realm,
      );
    }
    $permissions["view all {$realm}"] = array(
      'title' => 'Module grants permission for view all in realm ' . $realm,
    );
  }
  return $permissions;
}

/**
 * Used for implement hook_node_grants().
 */
function _module_grants_test_get_node_grants($grants, $account, $op) {
  if ($op != 'view') {
    return array();
  }
  $results = array();
  foreach ($grants as $realm => $gids) {
    foreach ($gids as $keyword => $gid) {
      if (user_access($keyword, $account)) {
        $results[$realm][] = $gid;
      }
    }
    $view_all_gid = _module_grants_test_get_view_all_gid($realm);
    if ($view_all_gid && user_access("view all {$realm}", $account)) {
      $results[$realm][] = $view_all_gid;
    }
  }
  return $results;
}

/**
 * Used for implement hook_node_access_records().
 */
function _module_grants_test_get_node_access_records($grants, $node) {
  $results = array();
  foreach ($grants as $realm => $gids) {
    foreach ($gids as $keyword => $gid) {
      if (strpos(strtolower($node->title), $keyword) !== false) {
        $results[] = array(
          'realm' => $realm,
          'gid' => $gid,
          'grant_view' => 1,
          'grant_update' => 0,
          'grant_delete' => 0,
          'priority' => 0,
        );
      }
    }
  }
  return $results;
}

/**
 * Add view all records
 */
function _module_grants_test_add_view_all($grants, $view_all_gid) {
  foreach ($grants as $realm => $gids) {
    $record = array(
      'nid' => 0,
      'gid' => $view_all_gid,
      'realm' => $realm,
      'grant_view' => 1,
      'grant_update' => 0,
      'grant_delete' => 0,
    );
    drupal_write_record('node_access', $record);
  }
}

/**
 * Get view all record
 */
function _module_grants_test_get_view_all_gid($realm) {
  $result = db_query("select gid from {node_access} where nid=0 and realm=:realm and grant_view=1", array(
    ':realm' => $realm,
  ));
  return $result
    ->fetchField();
}