Updating User Group Membership Question

Zanthor

Member
Let me preface this with I'm not a coder by trade but i can muck about and generally get done what needs done. I'm working on an addon that I can use to run a cron job using this guide.

The purpose of this cron job is to check if a users e-mail exists in an external database, and add the user to a group if they do. I've got it running, and returning 'success' for the users it should be, however the user doesn't seem to get added to the group.

Code:
        if ($users)
        {
            foreach ($users as $user)
            {
                // leaving out my validation code.
               // addUserGroupChange($userId, $key, $addGroups)
                $userModel->addUserGroupChange($userId, 'xiLicenseValidationValidated', XenForo_Application::get('options')->imcCurrentMemberGroup);

            }
        }


Thanks!
 
Last edited:
Did some more digging and found another example somewhere using a datawriter, this seems to have done the trick, but my question now is did I do it 'right', or is the method above ideal?

Code:
if($mdbResult->num_rows > 0)
                    {
                        // Setup Writer
                        $writer = XenForo_DataWriter::create('XenForo_DataWriter_User'); // calls the XenForo User DataWriter
                       
                        // Prep Groups
                        $groups = explode(',', $user['secondary_group_ids']);

                        $writer = XenForo_DataWriter::create('XenForo_DataWriter_User'); // calls the XenForo User DataWriter
                        $writer->setExistingData($user['user_id']); // tells the DW this is existing data to UPDATE an existing user
                        if (!$user['user_id']) {
                            return false; //or throw an exception because it's not a valid user
                        }
                        $writer->setOption(XenForo_DataWriter_User::OPTION_ADMIN_EDIT, true);           

                        // Remove the active group in case they renewed.
                        if(($key = array_search(XenForo_Application::get('options')->imcCurrentMemberGroup, $groups)) !== false)
                        {
                           unset($groups[$key]);
                        }
                        // Is the user already flagged as an expired member?
                        if (strpos($user['secondary_group_ids'],XenForo_Application::get('options')->imcExpiredMemberGroup) == false)
                        {
                            // Add to Member Group
                            $groups[] = XenForo_Application::get('options')->imcExpiredMemberGroup;
                        }

                        // Save our Work
                        $writer->setSecondaryGroups($groups);
                        $writer->save();

                    }
 
Back
Top Bottom