The Joomla! Forum ™






Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: Thu Jul 12, 2012 8:31 pm 
Joomla! Apprentice
Joomla! Apprentice

Joined: Fri Jan 13, 2006 9:04 pm
Posts: 24
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;
?>


Top
 Profile  
 
PostPosted: Mon Sep 10, 2012 11:13 pm 
Joomla! Fledgling
Joomla! Fledgling

Joined: Mon Sep 10, 2012 7:18 pm
Posts: 3
I don't know much about Joomla but have you tried to add an appropriate record in the table user_usergroup_map when you are storing the user? You just have to connect the id of the user that you just stored with the id of the usergroup that you want.


Top
 Profile  
 
PostPosted: Tue Sep 25, 2012 10:56 pm 
Joomla! Apprentice
Joomla! Apprentice

Joined: Thu Aug 02, 2012 5:52 pm
Posts: 5
Hello,

I do this in a similar way. 'groups' is an array of the groups associated to the user so what you have to assign is an array. The 'Registered' group id is 2 so you could try:

Code:
// Array that contains the groups' ids that you want to associate to the user. 2 is for Registered.
$userGroups = array(2);

$user->set('groups', $userGroups);


If it doesn't work, here is my code where I register a user, it's not the same as yours but it's similar:

Code:

function registerUser (){
   $firstName = "Mario";
   $lastName = "Alvarez";
   $username = "marioaae@outlook.com";
   $email = "marioaae@outlook.com";
   $password1 = "MiComida";
   $password2 = "MiComida";
   
   
   // Get the ACL
   $ACL =& JFactory::getACL();

   // Get the com_user params
   jimport('joomla.application.component.helper'); // Include libraries/application/component/helper.php
   $usersParams =& JComponentHelper::getParams( 'com_users' ); // Load the Params

   // "Generate" a new JUser object
   $user = JFactory::getUser(0); // It's important to set the "0" otherwise the admin user information will be loaded

   $data = array(); // Array for all user settings

   // Get the default usertype
   $usertype = $usersParams->get( 'new_usertype' );
   if (!$usertype) {
      $usertype = 'Registered';
   }

   // Set up the "main" user information
   $data['name'] = $firstName.' '.$lastName;
   $data['username'] = $email;
   $data['email'] = $email;
   $data['groups'] = array(2);//$ACL->get_group_id( '', $usertype, 'ARO' );  // Generate the gid from the usertype
   $data['password'] = $password1; // Set the password
   $data['password2'] = $password2; // Confirm the password
   $data['sendEmail'] = 1; // Should the user receive system mails?

   // Now we can decide, if the user will need an activation
   $userActivation = $usersParams->get( 'useractivation' ); // in this example, we load the config-setting
   if ($userActivation == 1) { // Yeah we want an activation
      jimport('joomla.user.helper'); // Include libraries/user/helper.php
      $data['block'] = 1; // Block the User
      $data['activation'] = JUtility::getHash( JUserHelper::genRandomPassword() ); // Set activation hash (don't forget to send an activation email)
   }
   else { // No, we need no activation
      $data['block'] = 0; // Don't block the user
   }

   // Now bind the data to the JUser Object
   if (!$user->bind($data)) { // If it doesn't work we show a warning
      JError::raiseWarning('', JText::_( $user->getError()));
      return FALSE;
   }

   // Save the user
   if (!$user->save()) { // If the user is not saved we show a warning
      JError::raiseWarning('', JText::_( $user->getError())); // ...raise an Warning
      return FALSE; // if you're in a method/function return false
   }

   return TRUE;
}



Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 



Who is online

Users browsing this forum: No registered users and 5 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group