XF 2.1 Removing a user from a group

Mr. Jinx

Well-known member
I am working on an add-on that adds / removes users from secondary groups.
Adding a user works fine, but I have some problems with removing them from a group.

So for adding a user, I use this existing service:
Code:
$userGroup=5;
/** @var UserGroupChange $userGroupChange */
$userGroupChange = $this->service('XF:User\UserGroupChange');
$userGroupChange->addUserGroupChange($user->user_id, $user->user_id . '_' . $userGroup, $userGroup);

If I want to remove this user from the group, I could use this:
Code:
$userGroup=5;
/** @var UserGroupChange $userGroupChange */
$userGroupChange = $this->service('XF:User\UserGroupChange');
$userGroupChange->removeUserGroupChange($user->user_id, $user->user_id . '_' . $userGroup);

...this works, but only if the user was added with the unique Id using this service.
If the user was added to a group in the ACP, then the remove service won't work as this action is not in the change log.

Therefore I'm using something like this to remove the user from the secondary group:
Code:
        /** @var User $user */
        $user = $this->em()->find('XF:User', $userId);

        $groupIds = $user->secondary_group_ids;
        $key = array_search($params->user_group_id, $groupIds);
        unset($groupIds[$key]);

        $user->fastUpdate('secondary_group_ids', $groupIds);

Now this seems to work, but not all value's in the user table are updated correctly.
For example, 'display_style_group_id' and 'permission_combination_id' in the user table are untouched after removing with this code.
If I would remove the user from the group in ACP, those values are updates properly.
So any other way to do this the right way?
 
Back
Top Bottom