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

Change User Group Via PHP

Discussion in 'XenForo Development Discussions' started by Retricide, Dec 16, 2012.

  1. Retricide

    Retricide Member


    I'm looking for a way to change an individual user's usergroup through a php function.

    I know I can modify their group in the xf_user table, but I was told it requires the cache to be rebuilt for the change to take effect - something I don't want to have to do every time the script is called.

    Is there anyway to promote/change a user's group that doesn't require the cache to be rebuilt?

    Thank you.
    mokujin likes this.
  2. Chris D

    Chris D XenForo Developer Staff Member

  3. Retricide

    Retricide Member

    Thank you, that looks perfect.

    One last question:
    Is there a function that accepts a username as a parameter and returns the userID?
    If not, I can easily query the DB but I imagine there probably is a function to do this.

    Thanks again.
  4. Chris D

    Chris D XenForo Developer Staff Member

    Yes, absolutely.

    You'd need to call the User Model...

    $name 'Retricide';
    $userModel XenForo_Model::create('XenForo_Model_User');
    $user $userModel->getUserByName($name);
    That will actually return the full information from xf_user in an array. The user ID would be $user['user_id'].

    There's a second parameter you can pass to the getUserByName function... you can use this if you need to join extra tables to get further information (such as full profile info, etc.).
    Jake Bunce and Retricide like this.
  5. Retricide

    Retricide Member


    Thanks for the help (again), Chris.
    Chris D likes this.
  6. Baffler

    Baffler Member

    Sorry to bump an old thread, but I found a different way to do this, but I noticed one issue, it won't set them to that group again if you remove them from it manually. I believe you have to do removeUserGroupChange with the same key. I'm not sure if this is a better way to do it or not, anyone know?

    $userModel $this->getModelFromCache('XenForo_Model_User');
    $userModel->addUserGroupChange($VisitorUserId"someKeyYouMakeUp?"6(group id));
    Dan Allen likes this.
  7. Chris D

    Chris D XenForo Developer Staff Member

    The DataWriter method detailed above is the best way to do this.

    The good thing about doing it with the DataWriter is it handles a lot of background stuff (such as rebuilding caches) automatically and typically as it is essentially data you're writing to the database, the DataWriter is ultimately the best approach.

    At first glance, I don't even know what that function is doing. The aim of changing a user's group is to update the user's record in xf_user with the correct user group IDs... this function doesn't seem to do that. If I ever work out what it's for I'll report back, but basically, the DataWriter method detailed above is the correct way to do this.
    Baffler likes this.
  8. Baffler

    Baffler Member

    I saw another addon using this, and it seemed simple enough and automatically updated. But now that I look in the Xenforo_Model_user file, it calls _applyUserGroupChanges function which seems to be doing basically what you mentioned with the DataWriter method but it's doing a lot of other things like checking old and new groups I guess.
  9. Jon W

    Jon W Well-Known Member

    As far as I remember, this function is used for temporary changes, for example in User Group Promotions. These changes don't want to be reflected in xf_user as an admin still wants the option to permanently make the same change while the temporary change is in place.
  10. LandNetwork

    LandNetwork Member

    $userGroupId = $this->_input->filterSingle('user_group_id' => XenForo_Input::UINT); // gets value of a field called 'user_group_id' from a template


    Parse error: syntax error, unexpected '=>' (T_DOUBLE_ARROW) in /usr/share/nginx/html/library/AddonName/Framework/groupfunctions.php on line 9

    PS: Sorry for the bumping of such an old thread...
  11. Chris D

    Chris D XenForo Developer Staff Member

    It's a typo. It would be:
    $userGroupId $this->_input->filterSingle('user_group_id'XenForo_Input::UINT); 
  12. LandNetwork

    LandNetwork Member

    Okay, but now I see:
    Fatal error: Using $this when not in object context in /usr/share/nginx/html/library/LandNetwork/Framework/groupfunctions.php on line 9
  13. Chris D

    Chris D XenForo Developer Staff Member

    The code example is predominantly for use in a XF controller.

    You'll likely need something like this:
    $request= new Zend_Controller_Request_Http();
    $input = new XenForo_Input($request);

    $userGroupId $input->filterSingle('user_group_id' => XenForo_Input::UINT); // gets value of a field called 'user_group_id' from a template
    $secondaryGroupIds $input->filterSingle('secondary_group_ids'XenForo_Input::UINT, array('array' => true)); // gets secondary Ids
  14. LandNetwork

    LandNetwork Member

    So what exactly do I need to change to get this working? I'd like to simply add a secondary group to the currently logged in user.
  15. LandNetwork

    LandNetwork Member

    Update: I found the following code to work for me. $webdb is my SQL connection.
    $webdb->query("UPDATE xf_user SET secondary_group_ids=".getIDFromRank($mcl['mcrank'])." WHERE user_id=".$visitor["user_id"].";");
  16. Daniel Hood

    Daniel Hood Well-Known Member

    Doing it the way @LandNetwork is doing it is going to result in some bad/invalid cached data. You either have to mimic all the stuff being done in the DataWriter, or better yet, use the DataWriter.
  17. Dan Allen

    Dan Allen Active Member

    Is there a code example for adding users to a group? Adding vs. changing. I need this for a variety of scenario, but they all have in common the need to put numbers of people into groups.

    Related question: why is XF put together in a manner that is so hard to deal with for basic data manipulations? It's as if there is a whole different world of programming for how XF does things instead of just normal. Normal would be, add records to the user_group-relation table and then people would be in groups. Instead, we have to use a Data Writer, whatever that is, etc. Xen Foro is beautiful sofware, so I figure it is my ignorance screaming out here. I really want to know why xf is so weird. Is it because it is done in Zend and is typical of Zend?

    Another example, no easy way to login and register from a remote app... that is a big bummer. It's like having a gorgeous car that can never be repaired. It has me wanting to put this system aside for 5 years while these basics get pulled together.
  18. HWS

    HWS Well-Known Member

    I don't know how it has to be done back in 2012, when Chris offered the solution above. But in most recent versions of XenForo the programatic addition (or removal) of a user group to a user account is very easy (assuming we are in a controller):

    $userModel = XenForo_Model::create('XenForo_Model_User');
    $userModel->addUserGroupChange($userId, 'any_id_you_like', $groupId);
    $userModel->removeUserGroupChange($userId, 'any_id_you_like');
    Regarding your comments @Dan Allen :
    It won't get better. Modern software is object orientated and follows the MVC model and data writers. It makes it in fact easier to expand the code and the create large applications with a very small bug rate (like XenForo).

    External login and registration is also very easy to implement in an add-on. Thanks to MVC and data writers. you should get comfortable with it.
    Last edited: Dec 13, 2015
    Sadik B, Xon and Dan Allen like this.
  19. Robust

    Robust Well-Known Member

    That's for the user group change table, for places like user group promotions.

    I still use the method Chris described, but a bit differently to make sure that there aren't any duplicates.
  20. HWS

    HWS Well-Known Member

    Of course, if you want to do things like changing the primary user group of a user, you need to work differently. But for adding a user group to a user, this code works perfectly. If you never use the remove function, the user stays in the new user group permanently (you can manually remove it).

    There really is no need to bloat up the code.

    And yes, this will also log the change into the user_group_change table. But there is absolutely no reason why you want to avoid this.

Share This Page