PDA

View Full Version : Change primary usergroup via PHP


razec
01-22-2010, 04:13 AM
I'm just curious as to what the best practice is for changing a user's primary usergroup through code. I tried searching the vB4 forum here for information on it but I didn't find anything. Is there a function that handles it, or a guide that I can reference with more information?

Can I just do it with something like this? Mind you this could be completely off which is why I haven't even tried it yet.

$userdata =& datamanager_init('User', $vbulletin, ERRTYPE_ARRAY);
$vbulletin->userinfo['usergroupid'] = $new_group = $userdata->save();


This is based on observations I made in register.php. I'm pretty sure it's not correct because I'm probably interpreting what's going on wrong. The point of this change is to allow the user to set what type of user they are, and in doing so this will change their primary group to match their selection. This takes place after the registration is complete, but I referred to register.php because I knew that's a place where the user's group (and other information) would be set/changed in code.

ForumsMods
01-22-2010, 10:08 AM
$userdata =& datamanager_init('User', $vbulletin, ERRTYPE_ARRAY);
$userdata->set('usergroupid', $new_group);
$userdata->save();

razec
01-22-2010, 08:46 PM
I get an error when this code runs:

Fatal error:

* A required field called username is missing or has an invalid value.

Unable to proceed with save while $errors array is not empty in class vB_DataManager_User in [path]/includes/class_dm.php on line 849

The code I'm using is:

$userdata =& datamanager_init('User', $vbulletin, ERRTYPE_ARRAY);
$userdata->set('usergroupid', $new_group);

$userdata->save();

which is what I copied from your post. Any idea as to why it's throwing that error?

Adrian Schneider
01-22-2010, 08:47 PM
You should have something like this before calling set,

$userdata->set_existing(fetch_userinfo($userid));

razec
01-23-2010, 04:06 AM
Ahh, got it. That did the trick! Thank you.

khininger
02-29-2016, 04:52 AM
huge thank you for this thread (idk why im not able to thank the posts directly).

i was looking for this for years. using it for the "delete my account" issues, switching them to another usergroup that hides them from members list and locks them out of forums, giving error message that they have deactivated their account, but can reactivate if they changed their mind. thank you.