User Groups and PHP


Hey all,

I've found a few threads on this and they seem to recommend the use of:

$visitor->removeUserGroupChange($visitor["user_id"], $randKey);
$visitor->addUserGroupChange($visitor["user_id"], $randKey, getIDFromRank($data["rank"]));

But why is a random key needed and what does it do? I've implemented my code and the user groups are simply not updated with this code.

I've got a $visitor object ($visitor = XenForo_Visitor::getInstance()->toArray();) and a DataWriter that updates custom field values.

                if (strtolower($data["link_id"] == strtolower($linkingkey))) {
                    $customFields = unserialize($visitor["custom_fields"]);
                    $customFields["minecraft"] = $data["name"];
                    $customFields["mcuuid"] = $data["uuid"];
                    $dw = XenForo_DataWriter::create('XenForo_DataWriter_User');
                    $dw->setOption(XenForo_DataWriter_User::OPTION_ADMIN_EDIT, true);
                $randKey = "cYNh7Z2MABLLMQMhjTHfBaJK";
            $visitor->removeUserGroupChange($visitor["user_id"], $randKey);
                    $visitor->addUserGroupChange($visitor["user_id"], $randKey, 2);
                    if ($dw->save()) {
                        $data["forum_linked"] = true; // Sets to true in memory
                        $couchBucket->upsert("playerdata:".$data["uuid"], $data); // Overwrite stored data to say forum_linked=true (upsert is replace)
                        $success = "Successfully linked website account <i>" . $visitor["username"] . "</i> to Minecraft account <i>" . $data["name"] . "</i>.";
                } else {
                    $error = "The supplied linking key is invalid.";

Custom fields are successfully updated whilst removeUserGroupChange throws:
Fatal error: Call to a member function removeUserGroupChange() on a non-object in link.php.
I tried to apply it on a User_Model instead of a $visitor object ($user_Model = XenForo_Model::create('XenForo_Model_User'); but whilst there's no immediate error, nothing changes.

What is it that I'm missing?
Last edited:
Top Bottom