• 1

Read this first!

We do not monitor these forums. The forum is provided to exchange information and experience with other users ONLY. Forum responses are not guaranteed.

However, please submit a ticket if you have an active subscription and wish to receive support. Our ticketing system is the only way of getting in touch with RSJoomla! and receiving the official RSJoomla! Customer Support.

For more information, the Support Policy is located here.

Thank you!

TOPIC: Create Joomla-user with form and assign ACL-Group

Create Joomla-user with form and assign ACL-Group 9 years 6 months ago #28900

  • Gerard.Verheij
  • Gerard.Verheij's Avatar
  • OFFLINE
  • Fresh Boarder
  • Posts: 12
  • Thank you received: 4
Hi,

My customer wanted to create Joomla-users to his website in a simple way. So I created a form with RS!Form Pro and the Joomla Registration Plugin, as described here.

Also, he wanted to assign the correct Access-right to this new users. In this website, lots of specific functionality (menu-items, modules, forms, articles etc) is only available (with View-Access-Levels) when you are in the correct ACL-group.

So, I use a dropdown-field in the user-creation-form to select the user-group the new user should be in and with a piece of PHP=script, the correct ACL-Group is assigned automatically to the new Joomla-user:

Fieldname of the dropdown-field: aclgroup
Items:
9|Participant Usergroup A
10|Usergroup B
15|Usergroup C
17|Usergroup D

The number before the |-sign is the group-ID of the ACL-group. These ID's are in the right column in your website, here: <your website>/administrator/index.php?option=com_users&view=groups

PHP Script (Script called after form has been processed)
// 1. Find the patient in Joomla-database, table: #__users
// We need: user_id of the just created patient
 
$patname = $db->escape($_POST['form']['Accountnaam']);
 
// retrieve user_id of newly created user
 
$db =& JFactory::getDBO();
$query = $db->getQuery(true);
$query
->select('id')
->from($db->quoteName('#__users'))
->where($db->quoteName('username')." = ".$db->quote($patname));  
 
$db->setQuery($query);
$patuser_id = $db->loadResult();
 
// 2. Read the ACL-group from the dropdown-field of the patient/disease-groep
// The doctor chooses the diagnosis-area and this choice determines which forms are displayed when the new patient logs in. 
// Therefor, the patient (Joomla-user) must be in the correct ACL-group.
// In this website, the ACL-group's for patients are configured like this:
// group_id=9 | Deelnemers_Onderzoek
// group_id=10 | Bekkenbodem-spreekuur
// group_id=15 | Seksuologie-spreekuur
 
$patgroup_id = intval($_POST['form']['aclgroup'][0]);
 
// 3. Now create a record in table #__user_usergroup_map
// Fill in two fields: user_id en group_id
// Fill in user_id = patuser_id, retrieved in step 1
// Fill in group_id = patgroup_id, retrieved in step 2
 
$query = $db->getQuery(true);
 
$columns = array('user_id', 'group_id');
$values = array($patuser_id, $patgroup_id);
$query
  ->insert($db->quoteName('#__user_usergroup_map'))
  ->columns($db->quoteName($columns))
  ->values(implode(',', $values));
 
$db->setQuery($query);
$db->query();
 
// 4. Show the Thank-You message.
// In there display the important variables of this script.
 
$thankYouMessage .= '<h2>Test voor Script called after form has been processed: </h2>'.'<p>Patientname is : '.$patname.'</p><p>User-ID is : '.$patuser_id.'</p><p>ACL-Group-no: '.$patgroup_id.'</p>'.'<h3>SQL-query : </h3><p>'.$query.'</p>';

WARNING! Take care you do not assign the ID of a manager/super user ACLGroup to this newly created Joomla-user...

Hope you can re-use this piece of PHP-code for one of your projects. Please write your improvements to this script in this forum. Have fun with Joomla and RS!Form Pro.

Kind Regards,

Gerard Verheij
The Netherlands
The administrator has disabled public write access.
The following user(s) said Thank You: djseleco, barry2

Create Joomla-user with form and assign ACL-Group 9 years 6 months ago #28968

  • barry2
  • barry2's Avatar
  • OFFLINE
  • Fresh Boarder
  • Posts: 1
This is great Gerard, thanks for this!

I tried to implement but form submission came back with this error.

Error: 1064
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '9)' at line 3 SQL=INSERT INTO `nbw94_user_usergroup_map` (`user_id`,`group_id`) VALUES (,9)

I'd also be happy making unique registration forms that automatically dump the user into different ACL groups depending on the form if anyone knows an easy way to do that?
The administrator has disabled public write access.

Create Joomla-user with form and assign ACL-Group 9 years 6 months ago #29070

  • Gerard.Verheij
  • Gerard.Verheij's Avatar
  • OFFLINE
  • Fresh Boarder
  • Posts: 12
  • Thank you received: 4
It seems something is wrong with getting the user-ID, because the SQL-statement should have two values, like this:

SQL=INSERT INTO `nbw94_user_usergroup_map` (`user_id`,`group_id`) VALUES (XXXX,9)

Perhaps you try to add a user which is allready registered and has the same e-mail address? Joomla does not allow that...

Also, I did not test the script in Joomla 1.5 and Joomla 3.x
The administrator has disabled public write access.

Create Joomla-user with form and assign ACL-Group 9 years 5 months ago #29217

  • djseleco
  • djseleco's Avatar
  • OFFLINE
  • Senior Boarder
  • Posts: 47
  • Thank you received: 10
Thanks Gerard.Verheij your script works fine, but i have only a problem; with this script the users after that have submitted the form have 2 group levels; "Registered" (automatically assigned by joomla) and my custom user group. You know a solution to leave only my custom group?
Last Edit: 9 years 5 months ago by djseleco.
The administrator has disabled public write access.

Create Joomla-user with form and assign ACL-Group 9 years 5 months ago #29236

  • cosmin.cristea
  • cosmin.cristea's Avatar
  • OFFLINE
  • Platinum Boarder
  • Posts: 756
  • Thank you received: 144
You will need to grab the user id first :
$user = JFactory::getUser();
$usr_id = $user->get('id');

Assigning a user to a group can be done like this:
JUserHelper::addUserToGroup($usr_id, insert_id_of_group_to_be_added);

Removing a user from a group:
JUserHelper::removeUserFromGroup($usr_id, insert_id_of_group_to_be_removed);

Although this is not a complete solution, you can use these examples to build your scenario.
My help is not official customer support. To receive your support, submit a ticket by clicking here
The administrator has disabled public write access.

Create Joomla-user with form and assign ACL-Group 9 years 5 months ago #29279

  • djseleco
  • djseleco's Avatar
  • OFFLINE
  • Senior Boarder
  • Posts: 47
  • Thank you received: 10
The administrator has disabled public write access.
  • 1

Read this first!

We do not monitor these forums. The forum is provided to exchange information and experience with other users ONLY. Forum responses are not guaranteed.

However, please submit a ticket if you have an active subscription and wish to receive support. Our ticketing system is the only way of getting in touch with RSJoomla! and receiving the official RSJoomla! Customer Support.

For more information, the Support Policy is located here.

Thank you!