Drupal admin: displaying user roles on the account edit page

Submitted by Frederic Marand on

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 Drupalfunction 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:

<?phpglobal $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.

It's quite simple: just test for that string in the foreach loop:
<?phpglobal $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") ;?>

Due to the menu inheritance system, that list would basically be infinite, or very large, since any path with additional components to a valid path is also a valid path.

So you probably want to only display part of that list of paths (pages). The question then becomes : exactly which pages would you display in that list ? If it's nodes, it can still be a very long list, as you can have up to 4 billion nodes on a Drupal 6 site.

The question is much too wide: which part of that near-infinite number of do you want to display ? You just can not list every possible pages a user can access; the entire universe would not be enough to hold the display since any path beyond an allowed one is allowed too.

To understand why this list is practically infinite, imagine user is allowed to see example.com/foo and no specific code handles any path longer than example.com/foo (i.e. no menu entry for foo/bar, for example). On example.com he will then be allowed to access any path matching regex /^foo\/\.*/

Now, why is this not infinite ? Basically because you just cannot send an infinite length URL to a server. Being infinite, it could not exist itself within a finite universe. But you get an idea of the available number of available paths.