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

User Class

Discussion in 'XenForo Development Discussions' started by Daniel Hood, Jun 9, 2011.

  1. Daniel Hood

    Daniel Hood Well-Known Member

    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.
     
  2. Jeremy P

    Jeremy P Well-Known Member

    Try..
    PHP:
    $user XenForo_Model::create('XenForo_Model_User')->getUserById($userId);
    ..?
     
  3. Daniel Hood

    Daniel Hood Well-Known Member

    Nice, thank you.
    Is there an easy way to get the information from the profile and permissions tables?
     
  4. Rjs37

    Rjs37 Member

    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.
     
  5. Daniel Hood

    Daniel Hood Well-Known Member

    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. Rjs37

    Rjs37 Member

    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.
     
  7. ragtek

    ragtek Guest

    exactly
    use the datawriter to create the user
     
  8. Daniel Hood

    Daniel Hood Well-Known Member

    I'm new to developing for xenforo. Could you tell me how to establish the connection to the datawriter?
    Code:
    XenForo_Applicaton::DataWriter?
     
  9. Daniel Hood

    Daniel Hood Well-Known Member

    nvm, I found out it's XenForo_DataWriter::create('XenForo_DataWriter_User'); can't find the function to actually process the registration though.
     
  10. Jeremy P

    Jeremy P Well-Known Member

    PHP:
    $dw->set('field name''field value'); // for as many fields as you need to set
    $dw->save();
     
  11. Kier

    Kier XenForo Developer Staff Member

    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));
     
  12. Daniel Hood

    Daniel Hood Well-Known Member

    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?
     
  13. Jeremy P

    Jeremy P Well-Known Member

    No.
    Take a look at the _getFields() method in the datawriter. You will need to set an appropriate value for each required field.
     
  14. Daniel Hood

    Daniel Hood Well-Known Member

    alright. What's the function to set the password string or does it do it automatically?
     
  15. Daniel Hood

    Daniel Hood Well-Known Member

    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();
    
     
  16. ragtek

    ragtek Guest

    default group id = XenForo_Model_User::$defaultRegisteredGroupId
     
  17. Rjs37

    Rjs37 Member

    Fair enough, thanks for the correction.
     
  18. ragtek

    ragtek Guest

    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/ ;)
     
    A1ex likes this.
  19. Daniel Hood

    Daniel Hood Well-Known Member

  20. Rjs37

    Rjs37 Member

    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).
     

Share This Page