MyBB Community Forums

Full Version: Attach new account to parent account
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello, I'm quite new to MyBB and I'm trying to implement some modifications to the registration of new accounts in my site. I'm using the Enhanced Account Switcher Plugin found here ( https://community.mybb.com/mods.php?action=view&pid=408 ) and I want to check new users for their e-mail, if it has been used by a master account then I want to display a window for the user to input its username and password, verify that login information, and if it is correct attach the newly created account to that parent account.

Here is what I've got so far:


// Set the data for the new user.
	$user = array(
		"username" => $mybb->get_input('username'),
		"password" => $mybb->get_input('password'),
		"password2" => $mybb->get_input('password2'),
		"email" => $mybb->get_input('email'),
		"email2" => $mybb->get_input('email2'),
		"usergroup" => $usergroup,
		"referrer" => $mybb->get_input('referrername'),
		"timezone" => $mybb->get_input('timezoneoffset'),
		"language" => $mybb->get_input('language'),
		"profile_fields" => $mybb->get_input('profile_fields', MyBB::INPUT_ARRAY),
		"regip" => $session->packedip,
		"coppa_user" => $coppauser,
		"regcheck1" => $mybb->get_input('regcheck1'),
		"regcheck2" => $mybb->get_input('regcheck2'),
		"registration" => true
		);
	
	$query_registered_mails = $db->simple_select("users", "email, as_uid", "email='".$user['email']."' AND as_uid='0'", array("order_by" => 'name',"order_dir" => 'DESC'));
	
	// Check if mail exists in Database and belongs to a Master Account
	if($db->num_rows($query_registered_mails) > 0)
	{
		// Require username and password from master account
		
		// Validate master account login information
		
			// If correct set user as_uid to the master account uid
			
			// Otherwise display registration error
	}
 
How could I display the window, collect and validate this information, and continue with the registry of the account?
I wouldn't do it in the datahandler. 
I would do the check after the registration process is done and if there is/are master account(s) with the same email registered redirect the user directly to the form to enter the password and attach his account.

You have not only to make sure as_uid is 0, as_share has to be 0 as well.
As an example I would go to the end of inc/plugins/accountswitcher/as_functions.php and add this 2 functions:

$plugins->add_hook('member_do_register_end', 'accountswitcher_register_attach');

function accountswitcher_register_attach()
{
    global $mybb, $db, $eas, $user_info;

    // Get all users with the same email who are not attached or shared accounts
    $masters = array();
    $query = $db->simple_select(
        "users",
        "uid",
        "email='".$db->escape_string($user_info['email'])."' AND as_uid='0' AND as_share='0'"
    );
    while ($master = $db->fetch_array($query)) {
        if ($master['uid'] == 0) {
            continue;
        }
        // Check if the accounts are master or free accounts
        $count = $eas->get_attached($master['uid']);
        if ($count > 0 ) {
            $masters[] = $master['uid'];
        }
    }

    // If there are master accounts with the same email - redirect to the form
    if (count($masters) > 0) {
            redirect("usercp.php?action=as_registerattach", "You are redirected to attach your account", "", true);
    }
}

$plugins->add_hook('usercp_start', 'accountswitcher_register_attachforum');

function accountswitcher_register_attachforum()
{
    global $mybb, $db, $eas, $header, $usercpnav, $usercpmenu;
    global $lang, $templates, $theme, $headerinclude, $as_usercp_input;

    if ($mybb->input['action'] == 'as_registerattach'
        && ($mybb->usergroup['as_canswitch'] == 1 || $mybb->user['as_uid'] != 0 || $mybb->user['as_share'] != 0)
    ) {
        // Get all users with the same email who are not attached or shared accounts
        $masters = array();
        $as_usercp_users = $as_usercp_privacy = $selectbox = '';
        $query = $db->simple_select(
            "users",
            "uid",
            "email='".$db->escape_string($mybb->user['email'])."' AND as_uid='0' AND as_share='0'"
        );
        while ($master = $db->fetch_array($query)) {
            if ($master['uid'] == 0) {
                continue;
            }
            // Check if the accounts are master or free accounts
            $count = $eas->get_attached($master['uid']);
            if ($count > 0 ) {
                $masters[] = $master['uid'];
            }
        }

        if (count($masters) == 0) {
            redirect("usercp.php?action?as_edit", "No master account with the same email found!");
        } elseif (count($masters > 0)) {
            $selectbox .= '<tr><td><select name="username" style="min-width: 150px;">';
            if (count($masters) == 1) {
                $master_acc = get_user((int)$masters);
                $selectbox .= '
                    <option value="'.htmlspecialchars_uni($master_acc['username']).'">'.htmlspecialchars_uni($master_acc['username']).'</option>';
            } elseif (count($masters) > 1) {
                foreach ($masters as $master) {
                    $master_acc = get_user((int)$master);
                $selectbox .= '
                   <option value="'.htmlspecialchars_uni($master_acc['username']).'">'.htmlspecialchars_uni($master_acc['username']).'</option>';
                }
            }
            $selectbox .= '</select>
                ';
            $as_usercp_input = '<input type="hidden" name="action" value="as_attach" />
                                    <table width="100%" border="0">
                                        <tr><td><input type="hidden" name="select" value="attachme" /></td></tr>
                                        <tr><td><span id="as_username" class="smalltext">Attach this account to:</span></td></tr>
                                        '.$selectbox.'
                                        <tr><td><span id="as_password" class="smalltext">Password</span></td></tr>
                                        <tr><td><input type="password" name="password" size="30" /></td></tr>
                                        <tr><td>
                                            <input type="submit" value="Attach" name="Attach" class="button" />
                                        </td></tr>';
        }
        $as_usercp_options = eval($templates->render('accountswitcher_usercp_options'));
        $as_usercp = eval($templates->render('accountswitcher_usercp'));
        output_page($as_usercp);
        exit;
    }
}
Thank you very much, this worked perfectly, I'll just have to do some minor adjustments to the displayed page since my site uses a customized skin, but all in all works!
I'll probably add this function in the next update with a setting to enable it. Of course, the HTML part will be in a template so you can adjust it for every theme separately.
(2017-05-08, 06:11 PM)doylecc Wrote: [ -> ]I'll probably add this function in the next update with a setting to enable it. Of course, the HTML part will be in a template so you can adjust it for every theme separately.

did the thing ihas released as you mentioned above ?
Yes, it is.