Drupal admin: displaying user roles on the account edit page

Some sites may wish to display to users what their roles are. Here is a small module allowing just this.

Drupal user roles are typically seen only by the administrator when editing a user's account. As of 4.6.3, there seems to be no simple way to have them displayed to user when they access their own information in a theme- and theme-engine-independent way (a PHPtemplate-dependent trick is explained on drupal.org).

So the simplest way appears to be a module implementing hook_user. This can be added to an existing module, or coded on its own. In this case the code is supposed to be implemented as csroles.module, meaning it is in a module of its own that has to be activated from http://www.example.com/admin/modules

<?php
// $Id: csroles.module,v 0.1 2005/08/24 00:41:00 FG Marand $
// Sample code due for the upcoming OSInet helpdesk support module for Drupal
function csroles_help ($section = '')
  {
 
$output = '' ;
  switch (
$section)
    {
   
// The description found on the admin/system/modules page.
   
case "admin/modules#description":
     
$output = t ("Allows for the display of an user's roles on their profile page") ;
      break;

   
// The module's help text, displayed on the admin/help page
    // and through the module's individual help link.
   
case "admin/help#customersupport":
     
$output = t ("Allows for the display of an user's roles on their profile page") ;
      break ;
    }
  return
$output;
  }

function
csroles_user ($op, &$edit, &$user, $category = NULL)
  {
 
$ret = NULL ;
  switch (
$op)
    {
    case
'view':
      {
     
$ret =  array ('account' => t('Personal information'))     ;
      break ;
      }
    case
'categories':
      {
     
$ret = array
        (
        array (
         
"name"   => "csroles",
         
"title"  => "user roles",
         
"weight" => 1)
          ) ;
      break ;
      }
    case
'form':
      {
     
// When user tries to edit his own data:
     
if ($category == 'csroles') // default: account
       
{
       
$ret = array
          (
          array
            (
           
'title' => 'You have been assigned roles:',
           
'data'  => '<ul><li>' . implode ('</li><li>', $edit ['roles'])
                     .
'</li></ul>',
           
'weight'=> 0,
            )
          ) ;
        }
      }
    }
  return
$ret ;
  }
?>

For site admins wishes to display the same information in a block without adding a module to an already overloaded site, there is a shorter way: create a custom block in http://www.example.com/admin/block/add, setting the input filter to PHP code, and type:

<?php
global $user ;

print (
"<ul>\r\n") ;
foreach (
$user->roles as $role)
  {
  print (
"  <li>$role</li>\r\n") ;
  }
if (
$user->uid == 1)
  print (
"  <li><em>sysop</em></li>\r\n") ;
print (
"  </ul>\r\n") ;
?>

Tagged for , , and in Technorati.

Is it possible to display

Is it possible to display the user's role in a block but not display "authenticated user"?

Don't display 'authenticated user'

It's quite simple: just test for that string in the foreach loop:
<?php
global $user ;

print (
"<ul>\r\n") ;
foreach (
$user->roles as $role)
  {
  if (
$role != 'authenticated user') // mask that role
   
{
    print (
"  <li>$role</li>\r\n") ;
    }
  }
if (
$user->uid == 1)
  {
  print (
"  <li><em>sysop</em></li>\r\n") ;
  }
print (
"  </ul>\r\n") ;
?>