• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

External login without password?

#1
Hello,

i want to integrate XenForo in an Intranet-Site with its own user-managemant. Now i´m able to create the user in Xenforo using it´s key from the intranet as external userid.

But i have troubles to login the user in Xenforo. Till now i using this code an it doesn´t work. I think anything is missing... but what?

$session = XenForo_Application::get('session');
$session->changeUserId($UserID);
XenForo_Visitor::setup($UserID);
header("Location: forum/index.php");

The user is allready authenticated in the portal and Xenforo doesn´t allow new user registrations. So no password is provided for the login in xenforo. It should only work with his user-id!

Thank You
Eric
 
#2
Solved:

$session = XenForo_Application::get('session');
$session->changeUserId($UserID);
$session->save();
XenForo_Visitor::setup($UserID);
header("Location: forum/index.php");
 
#3
Hi EricB.
I'm currently making a project same as yours, but I don't know much about Xenforo. So could you give me the code to create, authenticate user and login from the Intranet-Site?
 

EricB

New member
#4
Hi, find below my code which is working for me. You have to do some auth-jobs in your script!

Regards Eric


define('XF_ROOT', '/www/docs/domain/forum'); // set this (absolute path)!
define('TIMENOW', time());
require_once(XF_ROOT . '/library/XenForo/Autoloader.php');

XenForo_Autoloader::getInstance()->setupAutoloader(XF_ROOT . '/library');
XenForo_Application::initialize(XF_ROOT . '/library', XF_ROOT);
XenForo_Application::set('page_start_time', TIMENOW);
XenForo_Application::disablePhpErrorHandler();
XenForo_Application::setDebugMode(true);
XenForo_Session::startPublicSession();
$GpID = '19763';
$visitor = XenForo_Visitor::getInstance();
$userModel = XenForo_Model::create('XenForo_Model_User');
$userExternalModel = XenForo_Model::create('XenForo_Model_UserExternal');
$UserInfo = $userExternalModel->getExternalAuthAssociation('pnet', $GpID);
if (!$UserInfo)
{
$data = array(
'username' => 'name to display',
'email' => 'usermail@mail.com',
'location' => 'home',
'dob_day' => '22', // Date of Birthday
'dob_month' => '09', // Month of Birthday
'dob_year' => '1958',
'timezone' => 'Europe/Amsterdam'
);
$auth = XenForo_Authentication_Abstract::create('XenForo_Authentication_NoPassword');
$writer = XenForo_DataWriter::create('XenForo_DataWriter_User');
$options = XenForo_Application::get('options');

$writer->bulkSet($options->registrationDefaults, array('ignoreInvalidFields' => true));
$writer->bulkSet($data);
$writer->set('scheme_class', $auth->getClassName());
$writer->set('data', $auth->generate(''), 'xf_user_authenticate');
$writer->set('user_group_id', XenForo_Model_User::$defaultRegisteredGroupId);
$writer->set('language_id', XenForo_Visitor::getInstance()->get('language_id'));
$writer->advanceRegistrationUserState(false);
$writer->save();
$user = $writer->getMergedData();
$UserID = $user['user_id'];

$userExternalModel->updateExternalAuthAssociation('pnet', $GpID, $UserID);
XenForo_Model_Ip::log($UserID, 'user', $UserID, 'register');

} else {
$UserID = $UserInfo['user_id'];
}
restore_error_handler();
restore_exception_handler();
$userModel->setUserRememberCookie($UserID);
$session = XenForo_Application::get('session');
$session->changeUserId($UserID);
XenForo_Visitor::setup($UserID);
header("Location: index.php");