I need a function for a plugin to check, if the shown account is the user's account or that from another one.
What the reported code is doing is to verify if the logged in user is the Drupal super user (aka the user #1). If that is what you really need, then there is no need to call user_load()
to load the user object of that user account. It's enough you use the following code:
global $user;
if ($user->uid == 1) {
$items['user/%user/classifieds'] = array(
'title' => 'Meine Kleinanzeigen',
'type' => MENU_LOCAL_TASK,
'page callback' => 'tpzclassified_user_page',
'page arguments' => array(1),
'access callback' => 'user_view_access',
'access arguments' => array(1),
'weight' => 4,
);
}
return $items;
In Drupal, there aren't two users with the same user ID, and 1 is the user ID for the Drupal super user (which is also called user #1 for the fact its user ID is 1).
The problem with this solution is that since Drupal 6, the menu callbacks are cached; conditionally adding menu callback doesn't have any effect, as the menu cache is cleared only when a new module is installed, or when a module is updated (and update.php is called). The only way to force Drupal to clear the menu cache is to use the following code:
if (!variable_get('menu_rebuild_needed', FALSE)) {
variable_set('menu_rebuild_needed', TRUE);
}
If you want to check if the current logged in user is accessing his own account you can use the following code:
function tpzclassified_menu() {
$items['user/%user/classifieds'] = array(
'title' => 'Meine Kleinanzeigen',
'type' => MENU_LOCAL_TASK,
'page callback' => 'tpzclassified_user_page',
'page arguments' => array(1),
'access callback' => 'user_view_access',
'access arguments' => array(1),
'weight' => 4,
);
return $items;
}
There is no reason to use a custom function, as user_view_access()
already checks if the current user is looking his own account.
function user_view_access($account) {
return $account && $account->uid &&
(
// Always let users view their own profile.
($GLOBALS['user']->uid == $account->uid) ||
// Administrators can view all accounts.
user_access('administer users') ||
// The user is not blocked and logged in at least once.
($account->access && $account->status && user_access('access user profiles'))
);
}