Custom user groups

From Joomla! Documentation

Revision as of 15:45, 26 September 2011 by Chris Davenport (Talk | contribs)

Replacement filing cabinet.png
This Namespace has been archived - Please Do Not Edit or Create Pages in this namespace. Pages contain information for a Joomla! version which is no longer supported. It exists only as a historical reference, will not be improved and its content may be incomplete.

Joomla 1.5 Please note that this article only applies to Joomla 1.5. See Category:Access Control for articles on access control in other versions of Joomla.

I use custom user groups (user roles) within Joomla 1.5. For example under the registered users group I've added several subgroups:

  customer A admin
 product manager
  product manager admin

I use these groups within my extension to set the permissions.

When I add the groups into the jos_core_acl_aro_groups table and set the relations correctly the groups in the user management are not displayed correctly (e.q. the administrator groups disappear). For displaying the groups correctly you will have to change the code of : administrator/components/com_users/admin.users.php line 285:

(Not sure in what version this changed - but in J! 1.5.14 this change is no longer in the administrator/components/com_users/admin.users.php file. This change must now be made to administrator/components/com_users/views/user/view.html.php on line 113.)

if ( $userGroupName == $myGroupName && $myGroupName == 'administrator' )
      // administrators can't change each other
      $lists['gid'] = '<input type="hidden" name="gid" value="'. $user->get('gid') .'" /><strong>'. JText::_( 'Administrator' ) .'</strong>';
      $gtree = $acl->get_group_children_tree( null, 'USERS', false );


if ( $userGroupName == $myGroupName && $myGroupName == 'administrator' )
      // administrators can't change each other
      $lists['gid'] = '<input type="hidden" name="gid" value="'. $user->get('gid') .'" /><strong>'. JText::_( 'Administrator' ) .'</strong>';
      $gtree = $acl->get_group_children_tree( null, 'USERS', true);

If you like to add your self custom groups do the following:

Edit the jos_core_acl_aro_groups table and add your custom groups (for example with phpmyadmin). When you add a new group make sure that you assign the correct parent to the added group. For example: the joomla registered group has the ID 18, when you assign a subgroup to it make sure that the parent_id is 18. Dont assign the lft and rght fields yet but use the code below to rebuild the groups tree correctly:

// Put this code in a file in your Joomla root then run it. Don't forget to delete the file when you're done.
require 'configuration.php';
$config = new JConfig; 
$user     = $config->user;
$password = $config->password;
$db       = $config->db;
$host     = $config->host;
mysql_connect($config->host, $config->user, $config->password) or
        die("Could not connect: " . mysql_error());
// 0-> parent_id in Joomla this is the value of the parent_id field of the Root record
// 1-> start the left tree at 1
rebuild_tree (0, 1);
function rebuild_tree($parent_id, $left) {
        global $config;
        // the right value of this node is the left value + 1
        $right = $left + 1;
        // get all children of this node
        $result = mysql_query('SELECT id FROM ' . $config->dbprefix . 'core_acl_aro_groups WHERE parent_id = ' . $parent_id . ';')
                or die(mysql_error());
        while ($row = mysql_fetch_array($result)) {
                // recursive execution of this function for each
                // child of this node
                // $right is the current right value, which is
                // incremented by the rebuild_tree function
                $right = rebuild_tree($row['id'], $right);
        // we've got the left value, and now that we've processed
        // the children of this node we also know the right value
        mysql_query('UPDATE ' . $config->dbprefix . 'core_acl_aro_groups SET lft = ' . $left . ', rgt = ' . $right . ' WHERE id = ' . $parent_id . ';')
                or die(mysql_error());
        // return the right value of this node + 1
        return $right + 1;
echo 'Complete! Go check your Joomla User Admin!';

When you want to use the custom groups to assign to your articles so that for example only the customers can view specific articles you need to add these groups into the jos_groups, just assign the ID + name of the group whereby the name must be equal to the groups names you added into the jos_core_acl_aro_groups table.