We\'re creating an XML API for Joomla that allows partner sites to create new accounts for their users on our website.
We\'ve created a standalone PHP script that
You should use Joomla internal classes, like JUser, since there a lot of internal logic such as password salting. Create a custom script that uses the values from the API request and saves the users in the database using methods from Joomla User Classes.
Two ways to add joomla users using your custom code is a wonderful tutorial. The approach works. I've used this approach in some projects.
If you have to access Joomla Framework outside Joomla, check this resource instead.
Based on the answer from waitinforatrain, which is not properly working for logged-in users (actually dangerous if you are using it in the back-end), I have modified it a bit and here it is, fully working for me. Please note that this is for Joomla 2.5.6, while this thread was originally for 1.5, hence the answers above:
function addJoomlaUser($name, $username, $password, $email) {
jimport('joomla.user.helper');
$data = array(
"name"=>$name,
"username"=>$username,
"password"=>$password,
"password2"=>$password,
"email"=>$email,
"block"=>0,
"groups"=>array("1","2")
);
$user = new JUser;
//Write to database
if(!$user->bind($data)) {
throw new Exception("Could not bind data. Error: " . $user->getError());
}
if (!$user->save()) {
throw new Exception("Could not save user. Error: " . $user->getError());
}
return $user->id;
}
I have made an ajax call and then just passing the variables to this script and it worked for me.
define('_JEXEC', 1);
define('JPATH_BASE', __DIR__);
define('DS', DIRECTORY_SEPARATOR);
/* Required Files */
require_once(JPATH_BASE . DS . 'includes' . DS . 'defines.php');
require_once(JPATH_BASE . DS . 'includes' . DS . 'framework.php');
$app = JFactory::getApplication('site');
$app->initialise();
require_once(JPATH_BASE . DS . 'components' . DS . 'com_users' . DS . 'models' . DS . 'registration.php');
$model = new UsersModelRegistration();
jimport('joomla.mail.helper');
jimport('joomla.user.helper');
$language = JFactory::getLanguage();
$language->load('com_users', JPATH_SITE);
$type = 0;
$username = JRequest::getVar('username');
$password = JRequest::getVar('password');
$name = JRequest::getVar('name');
$mobile = JRequest::getVar('mobile');
$email = JRequest::getVar('email');
$alias = strtr($name, array(' ' => '-'));
$sendEmail = 1;
$activation = 0;
$data = array('username' => $username,
'name' => $name,
'email1' => $email,
'password1' => $password, // First password field
'password2' => $password, // Confirm password field
'sendEmail' => $sendEmail,
'activation' => $activation,
'block' => "0",
'mobile' => $mobile,
'groups' => array("2", "10"));
$response = $model->register($data);
echo $data['name'] . " saved!";
$model->register($data);
Only user is not getting activated automatically.
I am passing 'block' => "0"
for activating user but it is not working :(
But rest of the code works fine.
In my case (Joomla 3.4.3) the user was added to the session, so there was a buggy behaviour when trying to activate the account.
Just add this line, after $user->save():
JFactory::getSession()->clear('user', "default");
This will remove the newly created user from the session.
Valid for Joomla 3.9.xx
If you are working with a separate 3rd party MySQL DB (other that the current DB that Joomla is running in), then you can use the following SQl. Its a bit crude but will get the job of "creating users" done.
INSERT INTO `datph_users` (`id`, `name`, `username`, `email`, `password`, `block`, `sendEmail`, `registerDate`, `lastvisitDate`, `activation`, `params`, `lastResetTime`, `resetCount`, `otpKey`, `otep`, `requireReset`) VALUES (NULL, 'New Super User', 'newsuperuser', 'newsuperuser@mailinator.com', MD5('newsuperuser'), '0', '1', '2019-09-03 11:59:51', '2020-09-15 15:01:28', '0', '{\"update_cache_list\":1,\"admin_style\":\"\",\"admin_language\":\"\",\"language\":\"\",\"editor\":\"\",\"helpsite\":\"\",\"timezone\":\"\"}', '0000-00-00 00:00:00', '0', '', '', '1');
INSERT INTO `datph_user_usergroup_map` (`user_id`, `group_id`) VALUES (LAST_INSERT_ID(), '8');
Super Administrator
, you can set to what ever you wish to register the user to.Update: oh I ddin't see you wanted 1.5 but you could do similar but wth the 1.5 API instead.
This is part of something I was usng for anther purpose but you would need to use the default group instead until an issue with using JUserHelper from the command line is fixed or make it a web application.
<?php
/**
*
* @copyright Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
if (!defined('_JEXEC'))
{
// Initialize Joomla framework
define('_JEXEC', 1);
}
@ini_set('zend.ze1_compatibility_mode', '0');
error_reporting(E_ALL);
ini_set('display_errors', 1);
// Load system defines
if (file_exists(dirname(__DIR__) . '/defines.php'))
{
require_once dirname(__DIR__) . '/defines.php';
}
if (!defined('JPATH_BASE'))
{
define('JPATH_BASE', dirname(__DIR__));
}
if (!defined('_JDEFINES'))
{
require_once JPATH_BASE . '/includes/defines.php';
}
// Get the framework.
require_once JPATH_LIBRARIES . '/import.php';
/**
* Add user
*
* @package Joomla.Shell
*
* @since 1.0
*/
class Adduser extends JApplicationCli
{
/**
* Entry point for the script
*
* @return void
*
* @since 1.0
*/
public function doExecute()
{
// username, name, email, groups are required values.
// password is optional
// Groups is the array of groups
// Long args
$username = $this->input->get('username', null,'STRING');
$name = $this->input->get('name');
$email = $this->input->get('email', '', 'EMAIL');
$groups = $this->input->get('groups', null, 'STRING');
// Short args
if (!$username)
{
$username = $this->input->get('u', null, 'STRING');
}
if (!$name)
{
$name = $this->input->get('n');
}
if (!$email)
{
$email = $this->input->get('e', null, 'EMAIL');
}
if (!$groups)
{
$groups = $this->input->get('g', null, 'STRING');
}
$user = new JUser();
$array = array();
$array['username'] = $username;
$array['name'] = $name;
$array['email'] = $email;
$user->bind($array);
$user->save();
$grouparray = explode(',', $groups);
JUserHelper::setUserGroups($user->id, $grouparray);
foreach ($grouparray as $groupId)
{
JUserHelper::addUserToGroup($user->id, $groupId);
}
$this->out('User Created');
$this->out();
}
}
if (!defined('JSHELL'))
{
JApplicationCli::getInstance('Adduser')->execute();
}