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

User Class

Daniel Hood

Well-known member
#1
Is there a way to get User info for user's you're not logged in as through an external script?

To be more clear: I know how to get the user's info of the user logged in by doing this

Code:
require ($this->fileDir . '/library/XenForo/Autoloader.php');
XenForo_Autoloader::getInstance()->setupAutoloader($this->fileDir . '/library');
/**
* initialize
*/
XenForo_Application::initialize($this->fileDir . '/library', $this->fileDir);
XenForo_Session::startPublicSession();
$this->xfUser = XenForo_Visitor::getInstance();
if (is_numeric($this->xfUser->getUserId()) && $this->xfUser->getUserId() > 0)
{
$this->get_info($this->xfUser->getUserId());
return true;
}
else
{
return false;
}
But I'm trying to make the get_info function get it based on user id instead of just current visitor.
 
#4
There's an additional array parameter that you can use in that function to tell it which tables to join to:

PHP:
$userinfo = XenForo_Model::create('XenForo_Model_User')->
                    getUserById($userid,array('join' => XenForo_Model_User::FETCH_USER_PROFILE + XenForo_Model_User::FETCH_LAST_ACTIVITY));
There's other properties that you can use as well as join but I've not tried using anything else yet.

The 17 tells it to join to the profile and activity tables (so I can see whether the user is online (other users not the person that the script is running for) as last_activity doesn't get updated throughout that user's visit). The number to use is worked out by adding the numbers listed below (goes up in power of two's) - at least that was my understanding of it. So the number you'd probably need would be 9 for just profile and permissions.

User Profile = 1
User Options = 2
User Privacy = 4
User Permissions = 8
Last Activity (Session Activity) = 16

I've been posting about this and a few other semi-related things in this thread: http://xenforo.com/community/threads/using-xenforo-permission-outside-of-xenforo.7585/page-3
And that was in the latest post I'd made.
 

Daniel Hood

Well-known member
#5
Thanks,
I've attempted to look around the files I thought it would be in but do you happen to know if there's like a register function. Something like UserRegister($username, $plain_text_password, $email)?
 
#6
I think for things like registering and creating threads etc you're meant to use the data writers which is something I've not looked into yet.

Something else I've not tried yet is logging in from the site (i.e. not posting to the forum login.php). Both of those items are on my to-do list though.
 

Daniel Hood

Well-known member
#8
I'm new to developing for xenforo. Could you tell me how to establish the connection to the datawriter?
Code:
XenForo_Applicaton::DataWriter?
 

Daniel Hood

Well-known member
#9
nvm, I found out it's XenForo_DataWriter::create('XenForo_DataWriter_User'); can't find the function to actually process the registration though.
 

Kier

XenForo Developer
Staff member
#11
There's an additional array parameter that you can use in that function to tell it which tables to join to:

PHP:
$userinfo = XenForo_Model::create('XenForo_Model_User')->
                    getUserById($userid,array('join' => 17));
There's other properties that you can use as well as join but I've not tried using anything else yet.

The 17 tells it to join to the profile and activity tables (so I can see whether the user is online (other users not the person that the script is running for) as last_activity doesn't get updated throughout that user's visit). The number to use is worked out by adding the numbers listed below (goes up in power of two's) - at least that was my understanding of it. So the number you'd probably need would be 9 for just profile and permissions.

User Profile = 1
User Options = 2
User Privacy = 4
User Permissions = 8
Last Activity (Session Activity) = 16

I've been posting about this and a few other semi-related things in this thread: http://xenforo.com/community/threads/using-xenforo-permission-outside-of-xenforo.7585/page-3
And that was in the latest post I'd made.
Better to use the constants defined within the user model than use 'magic numbers', like this:
PHP:
$user = XenForo_Model::create('XenForo_Model_User')
	->getUserById($userId, array('join' =>
		XenForo_Model_User::FETCH_USER_PROFILE + XenForo_Model_User::FETCH_LAST_ACTIVITY));
 

Daniel Hood

Well-known member
#12
So:

Code:
$dw = XenForo_DataWriter::create('XenForo_DataWriter_User');
$dw->set('username', $username);
$dw->set('password', $password);
$dw->save();
is all I need to make my user?
 

Daniel Hood

Well-known member
#15
I figured it out.. You actually have to do $dw->setPassword($password); for the password one.
For future reference. What you need to register an account (I believe this is the bare minimum):
Code:
$xen_dw = XenForo_DataWriter::create('XenForo_DataWriter_User');
$xen_dw->set('username', $username);
$xen_dw->setPassword($password);
$xen_dw->set('email', $email);
$xen_dw->set('user_group_id', 2); // not sure how to obtain the default group id yet and you can't leave it empty
$xen_dw->save();
 
#17
Better to use the constants defined within the user model than use 'magic numbers', like this:
PHP:
$user = XenForo_Model::create('XenForo_Model_User')
->getUserById($userId, array('join' =>
XenForo_Model_User::FETCH_USER_PROFILE + XenForo_Model_User::FETCH_LAST_ACTIVITY));
Fair enough, thanks for the correction.
 
R

ragtek

Guest
#18
this is my helper method to create user
PHP:
    /**
    * Helper for XenForo User Datawriter to create easy an user
    * @param <type> $userName
    * @param <type> $userMail
    * @param <type> $userPassword
    * @param array $additionalData        (  usage: 'gender' => 'male'
    * @return <array>  userinfo
    */
    static public function createUser($userName, $userMail, $userPassword, array $additionalData = array())
    {
        /** @var $writer XenForo_DataWriter_User */
        $writer = XenForo_DataWriter::create('XenForo_DataWriter_User');
        $writer->set('username', $userName);
        $writer->set('email', $userMail);
        $writer->setPassword($userPassword);
        $writer->set('user_group_id', XenForo_Model_User::$defaultRegisteredGroupId);
        $writer->set('user_state', 'valid');

        foreach ($additionalData AS $data => $key)
        {
            $writer->set($data, $key);
        }

        $writer->save();
        return $writer->getMergedData();
    }
http://xenforo.com/community/threads/ragtek-add-ons-helper-framework.15163/ ;)
 
#20
Yea looks good, I guess for me that would be part of a Component (for the controller) rather than a Helper (for the view) but that's just CakePHP's terminology lol.

What about logging in? Has anyone tried to do that from the site (i.e. not posting to xen's login.php file).