The Drupal 6 user object functions as an easy and quick reference to information about the given user. The $user object is a global variable accessible within Drupal and carries quite a bit of information about the present user including name, ID, email and more. The following attributes are accessible through the global $user variable:
- uid – user ID
- name – user name
- mail – email address
- theme – user defined theme
- signature – the signature defined by the user
- created – Unix timestamp of user creation date
- access – Unix timestamp of last user access
- login – Unix timestamp of last user login
- timezone – user’s local time zone
- picture – the user picture
- roles – array of user roles
- sid – session ID
There are a few additional values which are far less useful to common practices.
The values of the $user object may be accessed directly by the following means:
global $user; // current user logged in or anonymous
print $user->uid; // prints the current user's ID
print $user->mail; // prints the current user's email address
While these values are extremely useful to the Drupal developer, it may at some point become necessary or simply convenient to store additional values within the user object. Adding attributes to the collection is straightforward and will be carried forward through the proceeding operations. Adding more information to the user object may be achieved as follows:
// add further information
global $user;
$further_info = array('fav_tv' => t('Daily Show')); // create an associative array to hold the required values
user_save($user, $further_info); // store the additional values in the user object
print $user->fav_tv; // will print 'Daily Show'
Two methods which may be easily implemented in order to test user authentication are user_is_logged_in() and it’s complementary method user_is_anonymous(). These methods return a Boolean value dependent upon the truth of the method in question. You may achieve a similar result by testing against the $user->uid which will return FALSE to an if() statement if the user is unauthenticated. This may be accomplished as follows:
if ($user->uid) {
// do some cool stuff as the user is logged in
} else {
// the user is anonymous so do less cool stuff
}
Drupal also provides developers with numerous user related functions which assist in working with users and $user objects. One such function is user_access() which tests the permissions of the user against the value passed. The signature of the user_access() function is
user_access($string, $account = NULL, $reset = FALSE)
In this signature, the $string argument represents the permission in question. For instance, if you wished to test the user’s permission to create an image, you would pass the argument as ‘create images’ and the return value would be the Boolean value representing the truth of the statement. The $account argument is not necessary to pass and if omitted will automatically test the current user. The $reset argument is also optional and represents the clearing of the user’s permissions cache.
$ex_array = array(‘mail’ => ‘wheels@foo.com’); // associative array
$wheels_user = user_load($ex_array); // user object returned to $wheels_user
* It is worth noting that the signature for this function will be changed in Drupal 7 to user_load($uid, $reset = FALSE). The $uid argument represents a particular user ID and the optional $reset argument may be used to reset the internal cache. This function will be supplemented by the function user_load_multiple($uids = array(), $conditions = array(), $reset = FALSE).
