You are here

function search_restrict_db_rewrite_sql in Search Restrict 5

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

File

./search_restrict.module, line 59

Code

function search_restrict_db_rewrite_sql($query, $primary_table, $primary_field, $args) {
  global $user;
  if ($user->uid == 1) {
    return;
  }
  $user_roles = $user->roles;
  $excluded_types = array();
  if ($query == '' && $primary_table == 'n' && $primary_field == 'nid' && empty($args)) {
    $content_type_restrictions = variable_get('search_restrict_content_type', array());
    foreach ($content_type_restrictions as $type => $roles) {
      $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,
      );
    }
  }
}