You are here

function search_restrict_db_rewrite_sql in Search Restrict 6.2

Same name and namespace in other branches
  1. 5 search_restrict.module \search_restrict_db_rewrite_sql()
  2. 6 search_restrict.module \search_restrict_db_rewrite_sql()

Implementation of hook_db_rewrite_sql().

File

./search_restrict.module, line 61

Code

function search_restrict_db_rewrite_sql($query, $primary_table, $primary_field, $args) {
  global $user;

  // User 1 can search everything.
  if ($user->uid == 1) {
    return;
  }
  $user_roles = $user->roles;
  $excluded_types = array();
  if ($query == '' && $primary_table == 'n' && $primary_field == 'nid' && empty($args)) {
    $types = node_get_types();
    foreach ($types as $type => $type_info) {
      $roles = variable_get('search_restrict_type_' . $type, array());
      $access = FALSE;
      $access_false = array();
      $access_true = array();

      // list included and excluded roles
      foreach ($roles as $role_id => $selected) {
        if (empty($selected)) {
          $access_false[] = $role_id;
        }
        else {
          $access_true[] = $role_id;
        }
      }

      // If no roles or all roles have been selected then everyone has access
      // skip this content type.
      if (!empty($access_true) && !empty($access_false)) {

        // If user has role in include list skip this content type.
        foreach ($access_true as $role_selected) {
          if (!empty($user_roles[$role_selected])) {
            $access = TRUE;
          }
        }

        // User doesn't have any roles that are allowed to search this content type.
        if (empty($access)) {
          $excluded_types[] = $type;
        }
      }
    }
    if (!empty($excluded_types)) {
      $where = " n.type NOT IN ('" . join("','", $excluded_types) . "') ";
      return array(
        'where' => $where,
      );
    }
  }
}