PDA

View Full Version : Upon Email Verification, Choose Which Usergroup User Is Placed In?


RedTurtle
11-10-2012, 05:41 AM
Is it possible to have a plugin that hooks into the vBulletin email verification system and does the following:

Based upon matching a word in a user's User Title field, upon the user verifying their email address, instead of the user being placed into the Registered Users usergroup, being placed instead in a different usergroup?

For example if I have a user's User Title is "Deactivated", I'd like the person to be moved to a certain usergroup after they have finished verifying their email rather than going to Registered Users.

I found this post by the awesome kh99 from about a year ago here: https://vborg.vbsupport.ru/showpost.php?p=2241176&postcount=2 but I'm not sure how I could adapt this code to only work when a user has user title "Deactivated".

if(!$vbulletin->options['verifyemail'])
{
// do some check to figure out usergroupid (X)
$newusergroupid = X;
$vbulletin->db->query_write("UPDATE " . TABLE_PREFIX . "useractivation
SET usergroupid = '".$newusergroupid."'
WHERE activationid = '".$activateid."' LIMIT 1 ");
}

Any help would be very much appreciated. Thank you for reading.

--------------- Added 1352581404 at 1352581404 ---------------

I guess more specifically my question would be does anyone know how to use an if conditional in the plugin to test the value of the User Title field?

--------------- Added 1352589399 at 1352589399 ---------------

Update:

Tried this code here as a plugin, at hook register_addmember_process and at register_activate_process but doesn't work (no error message either).

if(!$vbulletin->options['verifyemail'])
{
$usertitle_check = $db->query_first("SELECT usertitle from ".TABLE_PREFIX."user where userid = ".$vbulletin->userinfo['userid']);
if ($usertitle_check == "Deactivated")
{
$userdata->set('usergroupid', 4);
}
}

What I'm trying to accomplish is that after a user registers, I have them placed in a certain usergroup. Then if their e-mail address is incorrect and their email bounces, I have them placed in a different usergroup. Once they change their email address and confirm their account, I would like them to go back to the original usergroup they were in before, instead of just going to Registered Users by default.

kh99
11-11-2012, 02:57 PM
There's this mod that does something similar: www.vbulletin.org/forum/showthread.php?t=242068 It looks like what it does is changes the usergroupid in the user table directly.

Do you have code somewhere that sets the User Title to 'Deactivated' or are you doing that manually? I'm asking because if it's begin done in code, what you might be able to do is change the usergroupid field in the useractivation table, in any row where the userid matches.

To answer your question about checking the user title in a plugin, I think at register_activate_process you'd check $user['usertitle'].

RedTurtle
11-11-2012, 03:06 PM
Thank you so much for responding! :D

I have this mod (https://vborg.vbsupport.ru/showthread.php?t=199077) which currently moves any user who it detects as already having an account on the forum to the Usergroup of Users Awaiting Moderation. I have a Custom Title set for that usergroup of 'Deactivated'.

I then have another mod that moves any account that has a false email address to the Users Awaiting Email Verification group.

So what happens currently is that if someone creates a duplicate account with a false e-mail address, they are first placed in the Awaiting Moderation group but then when their email bounces they are automatically moved to the Awaiting Email Verification group. Now if they change their email address and confirm it, they'll be moved to Registered Users, instead of being moved back to the Awaiting Moderation group.

Having them instead move back to Awaiting Moderation is what I'm trying to accomplish by having any user with usertitle 'Deactivated' moved back to Awaiting Moderation upon email confirmation.

So am not sure exactly how I should do that. Would I check usertitle with $user['usertitle'] and then use this code?

if(!$vbulletin->options['verifyemail'] AND $user['usertitle'] == 'Deactivated')
{
$newusergroupid = 4;
$vbulletin->db->query_write("UPDATE " . TABLE_PREFIX . "useractivation
SET usergroupid = '".$newusergroupid."'
WHERE activationid = '".$activateid."' LIMIT 1 ");
}

Thank you!

kh99
11-11-2012, 03:09 PM
Yeah, that's what I was thinking, but I haven't tested it or anything.

Edit: Well, actually what I was thinking is that you'd do that database update wherever it is that you're now setting the user title to 'Deactivated', then you might not have to do anything when the actual activation happens.

RedTurtle
11-11-2012, 03:17 PM
Ah thanks Kevin, but that seems to just fail silently and doesn't change the usergroup. Still having the user end up in Registered Members after email verification.

Not sure if my check for usertitle is not working, or my database write isn't working.

Any steps you could recommend on figuring out which part is failing? Or maybe I'm hooking at the wrong part?

Thank you again for your time in helping me solve all the weird issues I come up with. ;)

kh99
11-11-2012, 03:23 PM
Yeah, I answered too quickly (then I edited my post above but you may have missed it). I don't know if that code would work at register_activate_process, what I was suggesting is that you use that database update line at the same place where you're now setting the user title to 'Deactivated'.

RedTurtle
11-11-2012, 03:55 PM
Ok so looking at the way that the mod that moves users to the awaiting moderation usergroup works, it hooks at register_addmember_complete. It calls an external file via this code:

if ($vbulletin->options['madp_prevention'] >= 0 AND ($vbulletin->options['madp_prevention'] < 4 OR $vbulletin->options['madp_silent_mode']))
{
require_once(DIR . '/madp/prevention.php');
}
else
{
require_once(DIR . '/madp/set.php');
}

Then in prevention.php, the part that seems to be doing the usergroup changing is this:

if (!$vbulletin->options['madp_silent_mode'])
{
// we caught multiple(s)
$user = $vbulletin->userinfo;

$prevuserdm =& datamanager_init('User', $vbulletin, ERRTYPE_SILENT);
$prevuserdm->set_existing($user);

if ($prevent_set)
{
$prevuserdm->set('usergroupid', $prevent_group);
$prevuserdm->set('displaygroupid', 0);

if ($vbulletin->usergroupcache["{$prevent_group}"]['usertitle'] != '')
{
$prevuserdm->set('usertitle', $vbulletin->usergroupcache["{$prevent_group}"]['usertitle']);
$prevuserdm->set('customtitle', 0);
}

$peventmsg = $vbphrase['madp_message_prevented'];
}
else
{
$peventmsg = $vbphrase['madp_message_error'];
$verbose_msgs += ERROR_USERGROUP_CACHE;
}

$prevuserdm->save();
unset($prevuserdm);
}

If I try my code at register_addmember_complete, it doesn't seem to work.

My code:

if(!$vbulletin->options['verifyemail'] AND $user['usertitle'] == 'Deactivated')
{
$newusergroupid = 4;
$vbulletin->db->query_write("UPDATE " . TABLE_PREFIX . "useractivation
SET usergroupid = '".$newusergroupid."'
WHERE activationid = '".$activateid."' LIMIT 1 ");
}

Could it have anything to do with my trying to do a SQL write whereas the other plugin doesn't seem to be doing that? Sorry if that is a stupid question.

kh99
11-11-2012, 04:21 PM
I don't know what mod that is. But I think I probably confused things by mentioning a couple different possibilities. I was suggesting two possible approaches (of whihc you'd want to choose only one): 1) change the useractivation table in the same place you're currently setting the user title, in which case you don't need a plugin to do anything when the actual activation happens because it wil use the usergroup from the useractivation table. Or 2) use a plugin at hook register_active_process and check the user title field and set the usergroupid in the user table, like the mod I linked to above. The code for that would be something like:

if(!$vbulletin->options['verifyemail'] AND $user['usertitle'] == 'Deactivated')
{
$newusergroupid = 4;
$vbulletin->db->query_write("UPDATE " . TABLE_PREFIX . "user
SET usergroupid = '".$newusergroupid."'
WHERE userid = '".$user['userid']);
}

RedTurtle
11-11-2012, 04:36 PM
Thanks Kevin.

Going with Option #2, and trying that code you just included above at register_activation_process doesn't seem to work. I would prefer to get this to work since the code seems so much "neater" and easier to implement. Any way I can break this out to test the individual components to see what's failing?

Do you think Option #1 is the only way to get this to work? I'm a little hesitant to go with Option #1 just because I don't fully understand it I suppose. When you say "change the useractivation table in the same place you're currently setting the user title" does that mean edit this modification's (https://vborg.vbsupport.ru/showthread.php?t=199077) code to add something near the code where it sets the usergroup to the awaiting moderation group?

Thank you again for all your patience and time.

kh99
11-11-2012, 04:39 PM
Yeah, looking at the above code again, I don't know about the "!$vbulletin->options['verifyemail']" part, I assumed you knew why that was there. Maybe try taking out that check?

I was asking above if you had code that set the user title to 'Deactivated' - what code is doing that? If you can find where that's done, then you should be able to add the update of the useractivation table at that same point.

RedTurtle
11-11-2012, 04:43 PM
Haha so the part about the "!$vbulletin->options['verifyemail']" I actually copied from your post a year ago here: https://vborg.vbsupport.ru/showpost.php?p=2241176&postcount=2 but am not sure what it means. I figured it meant that if the user no longer needs to verify their e-mail address (meaning they already have done it).

For the code that sets the user title to 'Deactivated', I believe it works like this--I have a custom usertitle for users in the usergroup Awaiting Moderation.

And I believe this code below changes the user to the Awaiting Moderation group (and then sets the usertitle):
if ($vbulletin->usergroupcache["{$prevent_group}"]['usertitle'] != '')
{
$prevuserdm->set('usertitle', $vbulletin->usergroupcache["{$prevent_group}"]['usertitle']);
$prevuserdm->set('customtitle', 0);
}

kh99
11-11-2012, 04:48 PM
Haha so the part about the "!$vbulletin->options['verifyemail']" I actually copied from your post a year ago here: https://vborg.vbsupport.ru/showpost.php?p=2241176&postcount=2 but am not sure what it means. I figured it meant that if the user no longer needs to verify their e-mail address (meaning they already have done it).

Oh, I see. What it means is that "verify email" is turned on in the options. That post has code for a plugin using hook regitser_addmember_complete, so it's checking so that the code is only executed if you are not verifying the email.

So what if you try this:

if($user['usertitle'] == 'Deactivated')
{
$newusergroupid = 4;
$vbulletin->db->query_write("UPDATE " . TABLE_PREFIX . "user
SET usergroupid = '".$newusergroupid."'
WHERE userid = '".$user['userid']);
}

kh99
11-11-2012, 04:50 PM
I changed the above code after posting it, so make sure you try the current code.

RedTurtle
11-11-2012, 05:05 PM
Thanks Kevin,

Using the newer code you put in, but it just seems to keep failing silently. Changes user to Registered Users rather than Users Awaiting Moderation.

And to confirm it is at register_activate_process.

Could it be that when the user confirms their email, they are being changed to the Registered Group (and thus losing the usertitle 'Deactivated') before our code goes into play and therefore it doesn't find any usertitle 'Deactivated' and doesn't do anything?

Would changing execution order make a difference here?

Thank you again.

kh99
11-11-2012, 05:15 PM
Yeah, I think I'm the one who is confused, at least partly. That mod I linked to above uses hook register_addmember_complete, which is why using that method isn't working. I'll have to look at it more but I can't do it right now. If you wanted (and you haven't already), you could try looking at the code in register.php. You can see what's going on around the hook locations and hopefully figure out what needs to be done.

RedTurtle
11-12-2012, 12:02 AM
Thank you again Kevin. I've been trying to make sense of this issue for the past couple of hours but not sure I'm getting anywhere.

I took your suggestion to look at register.php and from what I could tell, it sets the usergroup that the user will become a part of right at the registration section. Then once the email address is confirmed, it simply moves them to that usergroup that was set during registration.

Here is the part where it seems to determine usergroups near the beginning of register.php:

// assign user to usergroup 3 if email needs verification
if ($vbulletin->options['verifyemail'])
{
$newusergroupid = 3;
}
else if ($vbulletin->options['moderatenewmembers'] OR $vbulletin->GPC['coppauser'])
{
$newusergroupid = 4;
}
else
{
$newusergroupid = 2;
}
// set usergroupid
$userdata->set('usergroupid', $newusergroupid);

// set languageid
$userdata->set('languageid', $vbulletin->userinfo['languageid']);

// set user title
$userdata->set_usertitle('', false, $vbulletin->usergroupcache["$newusergroupid"], false, false);


and then in this code below, it deals with the activation part of the process and it has a part where it says that we no longer need to check if the "moderate new members" setting is turned on or not since this is being handled during registration:

if ($vbulletin->GPC['a'] == 'act')
{
$vbulletin->input->clean_array_gpc('r', array(
'u' => TYPE_UINT,
'i' => TYPE_STR,
));

$userinfo = verify_id('user', $vbulletin->GPC['u'], 1, 1);

($hook = vBulletinHook::fetch_hook('register_activate_start ')) ? eval($hook) : false;

if ($userinfo['usergroupid'] == 3)
{
// check valid activation id
$user = $db->query_first("
SELECT activationid, usergroupid, emailchange
FROM " . TABLE_PREFIX . "useractivation
WHERE activationid = '" . $db->escape_string($vbulletin->GPC['i']) . "'
AND userid = $userinfo[userid]
AND type = 0
");
if (!$user OR $vbulletin->GPC['i'] != $user['activationid'])
{
// send email again
eval(standard_error(fetch_error('invalidactivateid ', $vbulletin->session->vars['sessionurl'], $vbulletin->options['contactuslink'])));
}

// delete activationid
$db->query_write("DELETE FROM " . TABLE_PREFIX . "useractivation WHERE userid=$userinfo[userid] AND type=0");

/*
This shouldn't be needed any more since we handle this during registration
if ($userinfo['coppauser'] OR ($vbulletin->options['moderatenewmembers'] AND !$userinfo['posts']))
{
// put user in moderated group
$user['usergroupid'] = 4;
}*/

if (empty($user['usergroupid']))
{
$user['usergroupid'] = 2; // sanity check
}

// ### DO THE UG/TITLE UPDATE ###

$getusergroupid = iif($userinfo['displaygroupid'] != $userinfo['usergroupid'], $userinfo['displaygroupid'], $user['usergroupid']);

$user_usergroup =& $vbulletin->usergroupcache["$user[usergroupid]"];
$display_usergroup =& $vbulletin->usergroupcache["$getusergroupid"];

// init user data manager
$userdata =& datamanager_init('User', $vbulletin, ERRTYPE_STANDARD);
$userdata->set_existing($userinfo);
$userdata->set('usergroupid', $user['usergroupid']);
$userdata->set_usertitle(
$user['customtitle'] ? $user['usertitle'] : '',
false,
$display_usergroup,
($user_usergroup['genericpermissions'] & $vbulletin->bf_ugp_genericpermissions['canusecustomtitle']) ? true : false,
($user_usergroup['genericpermissions'] & $vbulletin->bf_ugp_genericpermissions['cancontrolpanel']) ? true : false
);

require_once(DIR . '/includes/functions_ranks.php');
if ($user['userid'] == $vbulletin->userinfo['userid'])
{
$vbulletin->userinfo['usergroupid'] = $user['usergroupid'];
$vbulletin->userinfo['displaygroupid'] = $user['usergroupid'];
}

// see 3.6.x bug #176
//$userinfo['usergroupid'] = $user['usergroupid'];

($hook = vBulletinHook::fetch_hook('register_activate_proce ss')) ? eval($hook) : false;

if ($userinfo['coppauser'] OR ($vbulletin->options['moderatenewmembers'] AND !$userinfo['posts']))
{
// put user in moderated group
$userdata->save();
eval(standard_error(fetch_error('moderateuser', $userinfo['username'], fetch_seo_url('forumhome', array())), '', false));
}
else
{
// activate account
$userdata->save();

// rebuild stats so new user displays on forum home
require_once(DIR . '/includes/functions_databuild.php');
build_user_statistics();

$username = unhtmlspecialchars($userinfo['username']);
if (!$user['emailchange'])
{
if ($vbulletin->options['welcomemail'])
{
eval(fetch_email_phrases('welcomemail'));
vbmail($userinfo['email'], $subject, $message);
}

$userdata->send_welcomepm();
}

if ($user['emailchange'])
{
eval(standard_error(fetch_error('emailchanged', htmlspecialchars_uni($userinfo['email'])), '', false));
}
else
{

eval(standard_error(fetch_error('registration_comp lete', $userinfo['username'],
$vbulletin->session->vars['sessionurl'], fetch_seo_url('forumhome', array())), '', false));
}
}
}
else
{
if ($userinfo['usergroupid'] == 4)
{
// In Moderation Queue
eval(standard_error(fetch_error('activate_moderati on'), '', false));
}
else
{
// Already activated
eval(standard_error(fetch_error('activate_wronguse rgroup')));
}
}

}


So I guess my thing is that since the usergroup is already being defined during registration and there's no checks for which usergroup to put the user into at activation, is there still a way I can hook into the activation process and move a user to the desired group, or is there any other process right after activation that I can use to do this?

Thank you so much for continuously helping! :D