Restrict user access to several actions in function I have a function, that returns statistics for the user:

function module_menu() {
  $items = array();
  $items["statistics"] = array(
    'page callback' =&gt; 'module_statistics',
    'access callback' =&gt; 'user_access',
    'access arguments' =&gt; array('access content'),
    'type' =&gt; MENU_CALLBACK,
  );
  return $items;
}

function module_statistics($group_id, $user_id = NULL) {
  global $user;
  if ($user_id == NULL) {
    $user_id = $user-&gt;uid;
  }
  $result = module_statistics_table($group_id, $user_id);
  print $result;
}


I want only the administrator to have access to this function with $user_id != NULL. So the user will be unable to watch someone elses statistics. I have an access argument 'admin tasks' for the admin. How to do the else statement:

if ($user_id == NULL) {
  $user_id = $user-&gt;uid;
}
else {
  if($user has no access to 'admin tasks') {
    return HTTP 403 Forbidden status code;
    exit from function;
  }
}
In your hook_menu call you can use different access arguments, to make sure only administrators access the callback function. For example,

'access arguments' =&gt; array('administer users'), // usually Administrators have this permissions


If users with some other roles try to access the menu, they'll get the Access Denied message by default.

If you want your users to access the function and restrict only certain part, you can check the user role in that case:

if ($user_id == NULL) {
  $user_id = $user-&gt;uid;
}
else {
  if(in_array('administrator', array_values($user-&gt;roles))) {
    //apply your exit function logic here
  }
}