How to provision a new user via external script

blantonl

Member
I need to provision users in Xenforo 2.0 via an external registration system. Does anyone have any code examples or documentation on how to accomplish this?

I see in a previous thread that I can initialize the XF Framework and then access the user methods and objects, like below, but I don't documentation anywhere that tells us the attributes and methods to use to provision new user

Code:
<?php

// bootstrap framework
$dir = __DIR__;
require($dir . '/src/XF.php');

XF::start($dir);

// create user
/** @var \XF\Service\User\Registration $registration */
$registration = XF::service('XF:User\Registration');
.....

After this, I'm stuck. Has anyone does this before that can provide an example of creating a new user, setting their new password and email address, and assigning a user to default user groups?

Thanks!
 
I found code for doing this in XF 1.5 - but I need to do this for XF 2.0

Code:
$newusername = "Joseph";
$newpassword = "12345";
$newemail = "joseph@yahoo.com";

$fileDir = "/home1/myserver/public_html/mywebsite/forums";


require($fileDir.'/library/XenForo/Autoloader.php');
XenForo_Autoloader::getInstance()->setupAutoloader($fileDir . '/library');

$startTime = microtime(true);
XenForo_Application::initialize($fileDir . '/library', $fileDir);
XenForo_Application::set('page_start_time', $startTime);
XenForo_Application::disablePhpErrorHandler();

// create new user
$writer = XenForo_DataWriter::create('XenForo_DataWriter_User');
// set all the values 
$writer->set('username', $newusername);
$writer->set('email', $newemail);
$writer->setPassword($newpassword, $newpassword);
$writer->set('user_group_id', XenForo_Model_User::$defaultRegisteredGroupId);
// $writer->set('location', $location);
//$writer->set('signature', '[IMG]http://gtracer.net/userbanner/'.$username.'.png[/IMG]');
// save user
$writer->save();

Anyone know how to convert this to work for XF 2.0 ?
 
Well, I just made some guesses and stumbled into this which actually works:

Code:
<?php

// bootstrap framework
$dir = __DIR__;
require($dir . '/src/XF.php');

XF::start($dir);

// create user
/** @var \XF\Service\User\Registration $registration */
$registration = XF::service('XF:User\Registration');
$input['username'] = "testusername";
$input['email'] = "xxx@test123.com";
$input['password'] = "testpw123";
$registration->setFromInput($input);
$user = $registration->save();

This provisions a new user perfectly.

Now, I need to figure out how to change a user's password via an external script and add/remove usergroups. User group management I can probably tackle by hitting up the database directly. However, password management I'm going to need to use the XF framework because I don't have any visibility into the password management scheme used.

Any help would be appreciated.
 
Users are more or less just normal entity objects. Entities are pretty easy to wrap your head around and work with, I would read up on them:
https://xenforo.com/xf2-docs/dev/entities-finders-repositories/#the-entity-system

Generally, if you know where to look you, can just peak at how XF does things in the core to get a better idea how to do them elsewhere.

(Untested) examples:
PHP:
/** @var \XF\Entity\User $user */
$user = \XF::em()->find('XF:User', $userId); // or use the $user returned from the registration service

// adding usergroups
$user->secondary_group_ids = [1, 2, 3];
$user->save();

// setting passwords
/** @var \XF\Entity\UserAuth $userAuth */
$userAuth = $user->getRelationOrDefault('Auth');
$userAuth->setPassword('password');
$userAuth->save();
\XF::repository('XF:UserRemember')->clearUserRememberRecords($user->user_id); // clear out other sessions
 
Perfect Jeremy, thank you.

I just dumped out the user object to see all the attributes available and I think I'm set now.

Many thanks!
 
Thanks blantonl for the code. That helped and I have it working...sort of. It creates a user but throws an error

LogicException: The session key must be overridden. src/XF/App.php:624

How can I prevent this error so the script will keep processing and the server logs won't have unnecessary entries?
 
Top Bottom