1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

setSecondaryGroups issue

Discussion in 'XenForo Development Discussions' started by GrdPa, Aug 8, 2013.

  1. GrdPa

    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: Aug 9, 2013
  2. GrdPa

    GrdPa Member

    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.
     
  3. Jeremy

    Jeremy XenForo Moderator Staff Member

    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.
     
  4. xf_phantom

    xf_phantom Well-Known Member

    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_EDITtrue);
    $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
     
  5. GrdPa

    GrdPa Member

    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: Aug 9, 2013
  6. Jeremy

    Jeremy XenForo Moderator Staff Member

    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.
     
  7. GrdPa

    GrdPa Member

    I've read your code you use :
    I've the same issue with this function. Probably because i'm using a lot of secondary usergroups.
     
  8. Jeremy

    Jeremy XenForo Moderator Staff Member

    How often are you changing groups?
     
  9. GrdPa

    GrdPa Member

    addon.jpg

    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
     
  10. tyteen4a03

    tyteen4a03 Well-Known Member

    I suggest changing the workflow of your addon to use checboxes instead.
     
  11. GrdPa

    GrdPa Member

    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.jpg
     
  12. tyteen4a03

    tyteen4a03 Well-Known Member

    I said checkboxes so you only need to update permission cache once.
     
  13. GrdPa

    GrdPa Member

    Yes, I understood.
    But logically I should not need to rebuild this cache.
     

Share This Page