Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I create a new Joomla user account from within a script?

Tags:

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 processes and validates the API request, but now we need to actually create the new accounts. We were originally thinking about just doing a CURL call to submit the signup form, but we realized that there is an issue with the user token. Is there another clean way to create a user account without going into the guts of Joomla? If we do have to do some surgery, what is the best way to approach it?

like image 776
user77413 Avatar asked Dec 15 '09 02:12

user77413


People also ask

How to create a new user in Joomla?

To create a new user on your Joomla website via the User Manager, you need to log into your website as administrator. Navigate to the Users tab from the top-menu and then User Manager > Add New User.

How do I find my Joomla user ID?

To get the user object for the currently logged-on user do: use Joomla\CMS\Factory; $user = Factory::getUser(); These two lines are equivalent to the following in the old class naming convention: $user = JFactory::getUser();


2 Answers

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.

like image 125
GmonC Avatar answered Sep 22 '22 01:09

GmonC


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;  } 
like image 43
mavrosxristoforos Avatar answered Sep 21 '22 01:09

mavrosxristoforos