setSecondaryGroups issue

GrdPa

Member
Hi,
I'm making an addon for my website and i've an issue with secondary user groups. This function add one secondary user group to a member. But when I use this function many time to add many secondary groups, i've permission issue.
I'm going to edit the member in the admin section and i save only. My permission issue disappears.
Have you an idea ?

Thanks a lot.

Code:
private function addSecondaryGroup($groupSet, $userId)
    {
      
$writer = XenForo_DataWriter::create('XenForo_DataWriter_User'); // calls the XenForo User DataWriter
        if ($userId)
        {
            $writer->setExistingData($userId); // tells the DW this is existing data to UPDATE an existing user
        }
      
        $userModel = XenForo_Model::create('XenForo_Model_User');
        $myUser = $userModel->getUserById($userId);
      
        $secondaryGroupIds = explode(',',$myUser['secondary_group_ids']);
        array_push($secondaryGroupIds,$groupSet);
        $writer->setOption(XenForo_DataWriter_User::OPTION_ADMIN_EDIT, true);
        $writer->setSecondaryGroups($secondaryGroupIds);
        $writer->rebuildUserGroupRelations();
        $writer->rebuildPermissionCombinationId();
        $writer->save();
        return true;
    }
 
Last edited:
Juste a precision :
My permission issue is that the user lose these acces (but he is in the good usergroups). Juste save the user under the admin section solve this problem. I don't understand.
 
I don't have code examples readily available right now, but I add secondary groups in my license validation add-on. It may be worthwhile to look into that to find some guidance.
 
PHP:
private function addSecondaryGroup($groupSet, $userId) {
$writer = XenForo_DataWriter::create('XenForo_DataWriter_User'); // calls the XenForo User DataWriter

$writer->setExistingData($userId); // tells the DW this is existing data to UPDATE an existing user }
if (!$userId) {
return false; //or throw an exception because it's not a valid user
}
$writer->setOption(XenForo_DataWriter_User::OPTION_ADMIN_EDIT, true);
$groups = explode(',', $userDw->get('secondary_group_ids'));
$groups[] = $groupSet;
$writer->setSecondaryGroups($groups);
$writer->save();
return true; }

no need to call all the other stuff, it will be handled automatically by the datawriter
 
Hi,
Thank you for your help but my problem persists.
To resolve I had to add the following line:

Code:
XenForo_Model::create('XenForo_Model_Permission')->rebuildPermissionCache();

However, rebuilding the cache is very long. Do you have an idea to not have to use it?

PS: My addon is a frontend addon, I don't know if it has an impact.
 
Last edited:
If you look at XI License Validation you should be able to avoid the permissions cache. I haven't noticed any delay in permissions appearing.
 
If you look at XI License Validation you should be able to avoid the permissions cache. I haven't noticed any delay in permissions appearing.

I've read your code you use :
$userModel->addUserGroupChange($userId, 'xiLicenseValidationValidated', XenForo_Application::get('options')->xilicensevalidationGroup);

I've the same issue with this function. Probably because i'm using a lot of secondary usergroups.
 
addon.webp

It's my addon. When i clic on red link the addon add the usergroup to the user and when i clic on green link, it remove the usergroup.

So, for one user, i can modifie many usergroups in under one minute.

PS: Sorry for my poor english
 
I'v change the workflow but my issue is the same. If i don't add the following code, my user lose all these rights (but he is in the Usergroups).

Code:
XenForo_Model::create('XenForo_Model_Permission')->rebuildPermissionCache();

This is very strange...

addon.webp
 
Top Bottom