alou.gr αρχική σελίδα
alou's portfolio
web marketing
Υπηρεσίες σχεδιασμού και κατασκευής ιστοσελίδων
Επικοινωνία Ανδρέας Λουκάκης
Joomla! 2.5 - Auto assign users in user group on registration
Παρασκευή, 22 Ιουνίου 2012 10:21

Since Joomla! nowdays (1.6+) has a powerful ACL and user groups management, assigning users in specific usergroups during registration is the logical next step. While searching for a guide on how to do this, I only found people asking about it... and since I implemented it some time ago I thought I'd share my findings.

Let's say we're making a community site for music artists, which could include guitar players, singers, bass players and drum players.

Step 1: Create the user groups

In Joomla! admin, Users->Groups->Add new group

 

adding user group in joomla


In our case, we need to make 4 user groups:

  1. Guitarist
  2. Singer
  3. Bass player
  4. Drummer

All of them, should have 'Registered' as the parent user group (it can get more complicated if you are using specific access levels but let's stick to registered for now).

adding user group in joomla

While creating the user groups, take a note of their id (right column in the user groups list), we're going to need that later.

  1. Guitarist - id:5
  2. Singer - id:6
  3. Bass player - id:7
  4. Drummer - id:8

Done with the usergroups, lets move on.

Step 2: Create access levels [optional]

Since we have our user groups, we'll probably want to control our content and display Jimmy Hendrix for the guitarists, Jaco Pastorius for the bass players etc. Thats done through creating access levels, connecting user groups to these access levels and creating categories / content / modules that can be accessed through specific access levels. I won't get any deeper on this since it's out of this guide's scope and should be easy to implement anyway. Ofcourse, I'll be glad to answer any possible questions if i can.

Step 3: User - profile plugin

It's already there, you only need to enable it. It adds some extra fields during registration, which we are going to use for group assignment.

Go to Plugin manager and you'll find it under 'user' type. Open it, have at least one field enabled (required if you MUST assign all users in a group, else optional) and dont forget to enable the plugin, too.

Step 4: registration fields

What we need to do now is:
a. create a field in registration form that will post a value (group id) depending on selected user group and
b. 'listen' for that value and write the group id in the database with the rest of the user data

In order to do that, we need to edit a couple of files:

a. Registration form:
joomla root/components/com_users/models/forms/registration.xml

I chose to create a hidden field and fill it's value with javascript depending on some other field input. You may want to create select options, radio buttons (if you allow multiple group assignment) or whatever you want.

Somewhere among the other fields, include a hidden input, named "assign":

<field name="assign" type="hidden"
field="assign"
filter="string"
label="group"
message="ignore"
required="true"
size="30"
/>

We need to have a value on that field (the group id we will assign this user to) and there are many ways to do that with javascript, depending on your needs.

Assuming you have an input field the user must fill, named "address1" (this is the name of the first field in user-profile plugin - you can change it's label to whatever you want either by editing plugins/user/profile/profiles/profile.xml or by overriding PLG_USER_PROFILE_FIELD_ADDRESS1_LABEL in language manager) with his interest in music, which can be either guitar, singing, bass or drums, i would do something like this with jQuery:

$('input#address1').change(function() { //whenever there is a change in this input...

var groupValue = $(this).val(); //get it's value
var userType = 0;
if ( groupValue == 'drums') {userType = 8;} //this guy is a drummer, we' put him in group with id=8 etc etc
else if ( groupValue == 'guitar' ) {userType = 5;
else if ( groupValue == 'bass' ) {userType = 7;}
else {userType = 6;}

})

There are much better ways to do that, ofcourse, depending on your specific setup. That's just to give you an idea, you should consider a lot more cases than i do in this example.

Now we only need to do one more thing...

Step 5: write user group in database

Since we post the group id during registration submission, we need to get this id somehow and write it. Thankfully, it's quite easy to do that but we need to edit one more file:
components/com_users/models/registration.php aprox. in line 322, you'll see:


$data['email']  = $data['email1'];
$data['password']  = $data['password1'];
$useractivation = $params->get('useractivation');

We need to add a group index with an assign value to the data array, so we change it to:

 
$data['email']  = $data['email1'];
$data['password']  = $data['password1'];
$useractivation = $params->get('useractivation');
$data['groups'][] = $data['assign'];

That's it!

A note for keeping notes: sooner or later, the core files we edited will be overriden during a joomla update. Since these files are not views / templates and cant be overriden somehow (afaik) its very important to keep a note of the edits you made, so that you can make the same changes after an update made all your effort dissapear :D

If you have some feedback, alternate ways or anything you'd like to share please use the comments or alou [at] alou [dot] gr.

 

Σχόλια  

 
#1 2012-08-09 15:11
hello,

thank you for your post.

where should i insert the exemple code $('input#address1').change(function () { //whenever there is a change in this input...

var groupValue = $(this).val(); //get it's value

var userType = 0;

if ( groupValue == 'drums') {userType = 8;} //this guy is a drummer, we' put him in group with id=8 etc etc
else if ( groupValue == 'guitar' ) {userType = 5;
else if ( groupValue == 'bass' ) {userType = 7;}
else {userType = 6;}})
?

thank you!
 
 
#2 2012-08-10 11:59
Hi mariana, there's no guarantee that this will work as it is because it will most probably need some customizations.

Since this is jQuery, you first need to include the jquery library in your index.php file (if not already included - check your source code first), by having this inside your template's index.php file:
(remove the extra spaces in script tags and replace k with c)
< skript src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" >< / script >

Then, somewhere AFTER that, put the js code more or less like that:

< skript type="text/javascript" >
jQuery(document ).ready(function( $) {

...the code goes here...

< skript >

If you're having trouble, post or send me a link so maybe i can help more.

Good luck ;)
 
 
#3 2012-09-28 08:49
We have created a plugin for Joomla 2.5 which allows multi groups registration.
Please visit www....

[edit by alou.gr: sorry, changing your link to JED entry of your plugin, i suggest everyone checks there first for safety / security / reviewing etc]
extensions.joomla.org/extensions/access-a-security/site-access/frontend-access-control/20450
 
 
#4 2013-02-08 09:40
We have created a plugin for Joomla 2.5 which allows multi groups registration.
Please visit:

http://extensions.joomla.org/extensions/access-a-security/site-access/frontend-access-control/20450
 
 
#5 2013-04-09 16:53
You save me! Congratulations man, thanks for your post.
 
 
#6 2013-04-13 09:26
Glad it helped :D
 
 
#7 2013-04-13 12:42
I do not understand too
$('input#address1').change(function () { //whenever there is a change in this input...

var groupValue = $(this).val(); //get it's value

var userType = 0;

if ( groupValue == 'drums') {userType = 8;} //this guy is a drummer, we' put him in group with id=8 etc etc
else if ( groupValue == 'guitar' ) {userType = 5;
else if ( groupValue == 'bass' ) {userType = 7;}
else {userType = 6;}

help me
 
 
#8 2013-04-16 18:33
This part is only an example of jQuery (javascript) code that woul help with assigning a value to a field.

I'm updating the post with an example as clear as it can be, regarding the javascript part, for further assistance i'll need a link for your registration page.
 
 
#9 2013-12-16 15:59
please can you provide me some help.i done the steps but never work..
 
 
#10 2013-12-19 01:54
Hi Tasos, how can I help? send me some info by email: alou [at] alou [dot] gr
 

Joomla Extensions

Custom HTML button

download

Τελευταία σχόλια

 

The eternal fight between...