XF 2.1 Remove secondary usergroup

Edman

Member
I'm trying to remove a secondary usergroup.

I used the following code to add it:
PHP:
      /** @var \XF\Service\User\UserGroupChange $userGroupChange */
      $userGroupChange = \XF::service('XF:User\UserGroupChange');
      $userGroupChange->addUserGroupChange($user->user_id, '<a key describing the change>', [<array of user group ids to add to>]);

However, removing it doesn't work this way, as the usergroup I'm trying to remove was added via the admin CP (actually, it was imported direct into DB from vBulletin export)

I could just get the field from the DB, explode the usergroupid array, and remove it, glue it back and update the field, but I'm guessing the general permissions on the forums won't be updated this way.
 
PHP:
 $userGroupChange = \XF::service('XF:User\UserGroupChange');
 $userGroupChange->removeUserGroupChange($userid, $key)
 
I need to remove a secondary usergroup from 1600 users that have expired user upgrades. I don't think its possible to search for users with expired user upgrades.

Besides, I'd like to do this daily, so I'm writing a cron job.
 
Well, I gave an example.
PHP:
$groups = $user->secondary_group_ids;
if ($removeGroupId = $this->getActionValue('remove_group_id'))
        {
            $key = array_search(intval($removeGroupId), $groups);
            if ($key !== false)
            {
                unset($groups[$key]);
                $changeSecondaryGroups = true;
            }
        }
        if ($changeSecondaryGroups)
        {
            $user->secondary_group_ids = $groups;
        }
 
I need to remove a secondary usergroup from 1600 users that have expired user upgrades. I don't think its possible to search for users with expired user upgrades.

Besides, I'd like to do this daily, so I'm writing a cron job.
XenForo, by default, will remove secondary user groups when a user upgrade expires. A custom cron is not required for this.

I'm not familiar with the vBulletin importer but as long as your vBulletin installation was correct (assuming the code logic is... logical), it should've been removing the secondary groups itself, so the imported data shouldn't have any such mismatches.

It seems likely that you can do what you're wanting to do without an add-on.
 
Last edited:
Nope, it doesn't work, because Xenforo doesn't port over user upgrades from vBulletin

If I have a vBulletin installation and somebody has purchased upgrades, he is now in Registered usergroup and Donator secondary usergroup. Xenforo then ports him over in that state. I bought a script from this forum to port over user upgrades so they keep on working, but when the upgrades expire, Xenforo reverts them to their original state, which is what they were in when they were ported over - Registered usergroup and Donator secondary usergroup.

So I wrote a query that looks for members of specific secondary usergroups (that you can only get via the upgrade) but no active subscription

Code:
            SELECT
                u.user_id,
                u.username,
                u.user_group_id,
                u.secondary_group_ids,
                a.user_upgrade_record_id
            FROM xf_user AS u
            LEFT JOIN xf_user_upgrade_active AS a ON u.user_id = a.user_id
            WHERE u.secondary_group_ids IN (9, 12)
            HAVING a.user_upgrade_record_id IS NULL
            ORDER BY u.user_id

I then loop through the results and remove the secondary usergroups from any members found, ran as a cronjob daily.
 
@Edman I have a very similar situation. I tried your SQL query above but it fails, I think because I'm using MariaDB not MySQL (errors are like this: "Warning: #1292 Truncated incorrect DOUBLE value: '14,35'"). (As an aside, I have alternately used this to find ONE secondary group ('12') listed in secondary_group_ids:
WHERE secondary_group_ids LIKE '%12%')

But regardless, I'm wondering if you know this: could a given user exist on BOTH the xf_user-upgrade_active table AND the xf_user_upgrade_expired table, and why?
 
I also use MariaDB.

Maybe you have an error in your SQL syntax? If you want to search for 2 strings, you need to put them in quotes separately ('14', '35') or if you're looking for two integers then (14,35)
This only works if you only have one secondary user group, which was my case.

It maybe possible that a user has an active and expired subscription, why not? They may have an expired subscription from some time back, and now have a current active subscription purchased recently. I haven't ran into this case, as I have been runing xenforo for only a few months.

Either way, as far as this code goes, it doesn't really matter. It just searches for active subscriptions, and if there are none, it removes the subscription usergroup.
 
Top Bottom