If you call user_roles(TRUE), the function will not return the anonymous user role.
Drupal always sets the role ID for anonymous user role, and the authenticated user role to the value of the constants DRUPAL_ANONYMOUS_RID
, and DRUPAL_AUTHENTICATED_RID
.
The following code is executed by user_install() in Drupal 7, and similar code is executed by system_install() in Drupal 6.
// Built-in roles.
$rid_anonymous = db_insert('role')
->fields(array('name' => 'anonymous user', 'weight' => 0))
->execute();
$rid_authenticated = db_insert('role')
->fields(array('name' => 'authenticated user', 'weight' => 1))
->execute();
// Sanity check to ensure the anonymous and authenticated role IDs are the
// same as the drupal defined constants. In certain situations, this will
// not be true.
if ($rid_anonymous != DRUPAL_ANONYMOUS_RID) {
db_update('role')
->fields(array('rid' => DRUPAL_ANONYMOUS_RID))
->condition('rid', $rid_anonymous)
->execute();
}
if ($rid_authenticated != DRUPAL_AUTHENTICATED_RID) {
db_update('role')
->fields(array('rid' => DRUPAL_AUTHENTICATED_RID))
->condition('rid', $rid_authenticated)
->execute();
}
The following code works also in Drupal 5; the $roles
array will not contain information about the anonymous user, and the authenticated user roles.
$roles = user_roles(TRUE);
unset($roles[DRUPAL_AUTHENTICATED_RID]);