I am using a script that automatically creates a Joomla 2.5 user. It works perfectly except that no user group is being assigned when the user gets created. I would like the user to be added to the "Registered" group when the user gets created. What am I missing/doing wrong?
Code:
<?php
// Makes available the joomla framework available in the script.
define( '_JEXEC', 1 );
define('JPATH_BASE', dirname(__FILE__) );//this is when we are in the root
define( 'DS', DIRECTORY_SEPARATOR );
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
require_once ( JPATH_BASE .DS.'plugins'.DS.'system'.DS.'kunena'.DS.'kunena.php' );
$mainframe =& JFactory::getApplication('site');
$mainframe->initialise();
// #1. Check for request forgeries
//JRequest::checkToken() or jexit( 'Invalid Token' );
if (JRequest::getVar('credential', '', 'post') != "Secret")
{
jexit("You said '".JRequest::getVar('credential', '', 'post')."'. This is not a valid credential");
}
// #2. Get required system objects
$pathway = & $mainframe->getPathway();
$config = & JFactory::getConfig();
$authorize = & JFactory::getACL();
$document = & JFactory::getDocument();
// Set the MIME type for JSON output.
$document->setMimeEncoding( 'application/json' );
// Check if user exists
$user = & JFactory::getUser(JRequest::getVar('username', 0, 'post'));
if ($user->id != 0)
{
$json = json_encode(array('result' => 'exists'));
echo $json;
return;
}
// Create a new user
$user = JFactory::getUser(0);
// #3. If user registration is not allowed, show 403 not authorized
$usersConfig = &JComponentHelper::getParams( 'com_users' );
//if ($usersConfig->get('allowUserRegistration') == '0')
//{
// JError::raiseError( 403, JText::_( 'Access Forbidden' ));
// return;
//}
// #4.Initialize new usertype setting
$newUsertype = $usersConfig->get( 'new_usertype' );
if (!$newUsertype)
{
$newUsertype = 'Registered';
}
// #5. Bind the post array to the user object
if (!$user->bind( JRequest::get('post'), 'usertype' ))
{
JError::raiseError( 500, $user->getError());
}
// #6. Set some initial user values
$user->set('id', 0);
$user->set('usertype', '');
$userGroups = $user->getAuthorisedGroups();
$date =& JFactory::getDate();
$user->set('registerDate', $date->toMySQL());
// #7. If user activation is turned on, we need to set the activation information(Not needed)
//$useractivation = $usersConfig->get( 'useractivation' );
//if ($useractivation == '1')
//{
// jimport('joomla.user.helper');
// $user->set('activation', md5( JUserHelper::genRandomPassword()) );
// $user->set('block', '1');
//}
// #8. Save the details of the user
if ($user->save())
{
$data = array('id' => $user->id, 'username' => $user->username);
plgSystemKunena::onAfterStoreUser($data, true, true, '');
$json = json_encode(array('result' => 'success', 'data' => $data));
}
else
{
$json = json_encode(array('result' => 'error', 'message' => $user->getError()));
}
echo $json;
?>