|
To create a new user in a component or other extension I have compiled the following instructions from several sources:
This example uses a Component Model Class to insert a new user:
<?php//No direct accessdefined( '_JEXEC' ) or die ( 'Restricted access' ) ;
// Load the base JModel jimport( 'joomla.application.component.model');
class ClientpageModelClientpage extends JModel{
function createNewUser( $email, $fname, $lname, $domain ){
$me = JFactory::getUser();
// 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 your 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'] = $fname.' '.$lname; // add first- and lastname $username = substr($fname, 0, 1) . $lname; $data['username'] = $username; // add username $data['email'] = $email; // add email $data['gid'] = $acl->get_group_id( '', $usertype, 'ARO' ); // generate the gid from the usertype
/* no need to add the usertype, it will be generated automaticaly from the gid */ $password = $this->getPswd(); $data['password'] = $password; // set the password $data['password2'] = $password; // 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
}
if (!$user->bind($data)) { // now bind the data to the JUser Object, if it not works....
JError::raiseWarning('', JText::_( $user->getError())); // ...raise an Warning return false; // if you're in a method/function return false
}
if (!$user->save()) { // if the user is NOT saved...
JError::raiseWarning('', JText::_( $user->getError())); // ...raise an Warning return false; // if you're in a method/function return false
}else{
// send email $subject = JText::_($ini_array['NEW_USER_MESSAGE_SUBJECT']); $message = sprintf ( JText::_("Hello %s,\n\nThank you for registering at %s. " . "Your account is created and must be activated before you can use it.\n" . "To activate the account click on the following link or copy-paste it in your browser:\n" . "%s\n\nAfter activation you may login to %s using the following username and password:" . "\n\nUsername: %s\nPassword: %s") , $data['name'] , $SiteName, JURI::root()."index.php?option=com_user&task=activate&activation=".$user->get('activation') , JURI::root() , $data['username'] , $data['password'] ); JUtility::sendMail( $me->email, $me->name, $data['email'], $subject, $message );
}
return $user; // else return the new JUser object
Important lessons from this is the importance of the JUser & JUserHelper to not only get the user created properly, but to sent the ever important Activation email.
|