Drupal 6: Restricting access to users-search

The core search in drupal 6 adds a “Users” search tab in the search page. Although it has permissions settings for content search and for advanced search, there seems to be no built-in option to restrict access to the users search.
One way of achieving this is by overriding the access callback of the search/user URL in hook_menu_alter.
For example, the following code replaces the access callback with user_access() and its argument with ‘administer users’, so only roles with this permission get access to the users search. As a side effect the users search tab is removed if the user doesn’t have this permission. The user/autocomplete path is also handled in the same way.

function mymodule_menu_alter(&$items) {
  if (isset($items['search/user/%menu_tail'])) {
    $menu_item =& $items['search/user/%menu_tail'];
 
    $menu_item['access callback'] = 'user_access';
    $menu_item['access arguments'] = array('administer users');
  }
 
  if (isset($items['user/autocomplete'])) {
    $menu_item =& $items['user/autocomplete'];
 
    $menu_item['access callback'] = 'user_access';
    $menu_item['access arguments'] = array('administer users');
  }
}

Other ways to achieve the same are discussed here.

Tested in Drupal 6.16 with core search.

This entry was posted in Drupal and tagged , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *