× Here you can get community support related to Auto Group.

Auto assign users in user group on registration

  • Picture
  • Topic Author
  • Offline
  • New Member
  • New Member
More
8 years 8 months ago - 8 years 8 months ago #4923 by Picture
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.
EX: Mysite : tranh da quy Ngoc Kim Long

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:

Guitarist
Singer
Bass player
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.

Guitarist - id:5
Singer - id:6
Bass player - id:7
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 = $data;
$data = $data;
$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 = $data;
$data = $data;
$useractivation = $params->get('useractivation');
$data[] = $data;
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.
Last edit: 8 years 8 months ago by Picture.

Please Log in or Create an account to join the conversation.

More
8 years 8 months ago #4926 by admin
Thanks for sharing your experience here on this forum. :)

Please Log in or Create an account to join the conversation.

Time to create page: 0.125 seconds