Change User Group Via PHP

Retricide

Member
Hello,

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.
 
The script would look something like this:
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.
 
Yes, absolutely.

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

PHP:
$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.).
 
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?

PHP:
$userModel = $this->getModelFromCache('XenForo_Model_User');
$userModel->addUserGroupChange($VisitorUserId, "someKeyYouMakeUp?", 6(group id));
 
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.
 
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.

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.
 
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.
 
$userGroupId = $this->_input->filterSingle('user_group_id' => XenForo_Input::UINT); // gets value of a field called 'user_group_id' from a template

Error:

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...
 
It's a typo. It would be:
PHP:
$userGroupId = $this->_input->filterSingle('user_group_id', XenForo_Input::UINT);
 
It's a typo. It would be:
PHP:
$userGroupId = $this->_input->filterSingle('user_group_id', XenForo_Input::UINT);
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
 
The code example is predominantly for use in a XF controller.

You'll likely need something like this:
PHP:
$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
 
The code example is predominantly for use in a XF controller.

You'll likely need something like this:
PHP:
$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
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.
 
Update: I found the following code to work for me. $webdb is my SQL connection.
PHP:
$webdb->query("UPDATE xf_user SET secondary_group_ids=".getIDFromRank($mcl['mcrank'])." WHERE user_id=".$visitor["user_id"].";");
 
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.
Update: I found the following code to work for me. $webdb is my SQL connection.
PHP:
$webdb->query("UPDATE xf_user SET secondary_group_ids=".getIDFromRank($mcl['mcrank'])." WHERE user_id=".$visitor["user_id"].";");
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.
 
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.
 
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):

Code:
$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:
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.
 
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.

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.
 
Top Bottom