Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Requirments needed to build this function.
- defined('_JEXEC') or die('Restricted access');
- require_once ( JPATH_BASE . DS . 'includes' . DS . 'defines.php' );
- require_once ( JPATH_BASE . DS . 'includes' . DS . 'framework.php' );
- require_once ( JPATH_BASE . DS . 'libraries' . DS . 'joomla' . DS . 'factory.php' );
- include_once JPATH_ROOT . '/components/com_community/libraries/core.php';
- include_once JPATH_ROOT . '/components/com_community/libraries/user.php';
- /**
- * Creates a new Joomla! user. This method throws errors so you have to
- * wrap the call to this method in a try/catch. It will also set a default
- * $name, $username and $password if none is provided.
- *
- * @param type $email REQUIRED
- * @param string $name
- * @param type $username
- * @param type $password
- * @return boolean
- * @throws Exception
- */
- function createUser($email = null, $name = null, $username = null, $password = null) {
- if (is_null($email)) {
- throw new Exception("Email is required to create new Joomla! user.");
- }
- $uniqid = uniqid();
- if (is_null($name)) {
- $name = 'member-' . $uniqid;
- }
- if (is_null($username)) {
- $username = $name;
- }
- if (is_null($password)) {
- $password = $uniqid;
- }
- try {
- // Create new user.
- jimport('joomla.application.component.helper');
- $params = 'com_users';
- $usersParams = JComponentHelper::getParams($params);
- $new_user = JFactory::getUser(0);
- $config = JComponentHelper::getParams('com_users');
- $defaultUserGroup = $config->get('new_usertype', 2);
- $jdata = array(
- "name" => $name,
- "username" => $username,
- "password" => $password,
- "password2" => $password,
- "email" => $email,
- "sendEmail" => 0,
- "groups" => array($defaultUserGroup)
- );
- // Automatically activate user. Comment this out if you use double
- // opt-in method.
- $useractivation = $usersParams->get('useractivation');
- if ($useractivation === 1) {
- jimport('joomla.user.helper');
- $jdata['activation'] = JUtility::getHash(JUserHelper::genRandomPassword());
- $jdata['block'] = 1; // block the user
- } else {
- $jdata['block'] = 0; // don't block the user
- }
- // Write new user to the Joomla! database.
- if (!$new_user->bind($jdata)) {
- throw new Exception("Could not bind data. Error: " . $new_user->getError());
- return false;
- }
- if (!$new_user->save()) {
- throw new Exception("Could not save user. Error: " . $new_user->getError());
- return false;
- }
- // Finally, you can return the new user Id.
- $cuser = CFactory::getUser($new_user->id);
- return $cuser;
- } catch (Exception $ex) {
- consoleLog("could not create user: " . $ex->getMessage());
- throw new Exception($ex->getMessage(), $ex->getCode(), $ex->getPrevious());
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement