joomla

custom user groups in joomla

Access to your database (with PHPmyAdmin, in my case). Find user groups table (its name is « jos_core_acl_aro_groups »). Default prefix for tables is « jos », but check what is your prefixe and replace « jos » by your table prefix if it’s different. In all examples, I’ll use « jos » as prefix, but don’t forget to change it on every code you’ll copy and paste from this tutorial.

Insérer groupes personnalisés 

Then, In PHPmyAdmin, I found « jos_core_acl_aro_groups » table. This table contains all my user groups. We will insert two new groups : one for employees and one other for employers (or what ever you want). You must correctly specify as parent_id the id of the parent user group. In our case, it’ll be simply Registered (id : 18). We add these records, specifying only the parent_id value.

Our groups are now inserted, but their level values is wrong, because this table uses a category level system with a left and right value (integers), that, according to some calculation, allow to specify which group is embed in which group, but we inserted no value for this. No panic : we have a script to regenerate the good values for both new groups.

Create a file named rebuild.php, place-it at Joomla’s root with this inside :

 

  1. <?php
  2. mysql_connect(“localhost”, “NOM_DUTILISATEUR_BD”, “MOT_DE_PASSE_BD”) or
  3. die(“Could not connect: ” . mysql_error());
  4. mysql_select_db(“NOM_DELA_BD”);
  5. // 0-> parent_id in Joomla this is the value of the parent_id field of the Root record
  6. // 1-> start the left tree at 1
  7. rebuild_tree ( 0 , 1);
  8. function rebuild_tree($parent_id, $left) {
  9. // the right value of this node is the left value + 1
  10. $right = $left+1;
  11. // get all children of this node
  12. $result = mysql_query(‘SELECT id FROM jos_core_acl_aro_groups ‘.
  13. ‘WHERE parent_id=”‘.$parent_id.'”;’);
  14. while ($row = mysql_fetch_array($result)) {
  15. // recursive execution of this function for each
  16. // child of this node
  17. // $right is the current right value, which is
  18. // incremented by the rebuild_tree function
  19. $right = rebuild_tree($row[‘id’], $right);
  20. }
  21. // we’ve got the left value, and now that we’ve processed
  22. // the children of this node we also know the right value
  23. mysql_query(‘UPDATE jos_core_acl_aro_groups SET lft=’.$left.’, rgt=’.
  24. $right.’ WHERE id=”‘.$parent_id.'”;’);
  25. // return the right value of this node + 1
  26. return $right+1;
  27. }
  28. ?>

…don’t forget to change « USERNAME_DB », « PASSWORD_DB » et « NAME_DB » for the username, the password and the name of the database.

Run the script in your browser, by opening rebuild.php page. Normally, you should not notice something; the script doesn’t write any confirmation (so you get a blank page). Go to check what it does look like in the database, if you understand a little bit how’s working this category level system… else, try to go on the next step, it should be okay.

Configurer bornes correctes pour les groupes

Hint : your two new groups should now own a new value (higher than zero), for columns « lft » and « rgt ». Else, you have probably specified wrong informations for database connection in the script above, or you forgot to change table names (prefix) in queries.

Configurer le groupe de l'utilisateur

Another good way to check if everything is working fine is : go to Users administration (Administration Joomla!), select a user and check that you can set one of the new groups as group for this user.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s