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

Rebuild Single User Cache?

Discussion in 'XenForo Development Discussions' started by ScottLott, Sep 19, 2013.

  1. ScottLott

    ScottLott Member

    Hey Guys,
    I've got an addon that injects user group IDs into the XenForo database.

    Unfortunately, when secondary user group IDs are injected the user's permissions are ruined preventing them from having access to any part of the forum.

    I've narrowed the problem down to the "permission_combination_id" on the XenForo user table. When this value isn't set correctly, the user looses their permissions. (They have access to nothing, they can't even view the forums home page)

    The (current) solution is to rebuild the user cache. This takes the existing user group IDs from the primary user group and secondary user groups and recalculates the permission combination ID.

    But I can't just rebuild the user cache every 10 minutes, this is something that needs to happen on the fly as each user has their group IDs updated.

    Knowing this, I narrowed the code down to the XenForo_DataWriter class. Turns out there is a function in there called "rebuildPermissionCombinationId" that is ran to do the magic.

    How would I call the "XenForo_DataWriter" class from a PHP script outside of XenForo? I'm also having trouble figuring out what variables need to be fed into XenForo_DataWriter for it to know which user to rebuild.

  2. Chris D

    Chris D XenForo Developer Staff Member

    I'm not sure how much of the following code is absolutely necessary for what you're trying to do, but generally external scripts use the following few lines initially to initiate XenForo:


    $fileDir dirname(__FILE__);

    $fileDir '/library/XenForo/Autoloader.php');
    XenForo_Autoloader::getInstance()->setupAutoloader($fileDir '/library');

    XenForo_Application::initialize($fileDir '/library'$fileDir);

    $deps = new XenForo_Dependencies_Public();
    You will of course have to alter the various paths so they are correct in relation to your script.

    From there, you can access a DataWriter like this:

    $writer XenForo_DataWriter::create('XenForo_DataWriter_User');
    You can specify existing data like this:

    Then, you can run your function like this:

    Finally, you may need to save, especially if you've made any other changes in the writer:

    Which, incidentally, brings me to another point.

    That rebuild is probably automatically triggered when other data is changed in the DataWriter.

    If you're making changes to the data in the user record, and you use the DataWriter to do it, then the rebuild may trigger automatically in the preSave() or postSave() functions.
    ScottLott likes this.
  3. ScottLott

    ScottLott Member

    Thanks a million!
  4. ScottLott

    ScottLott Member

    Alright, new problem.

    The application I'm working with uses the Zend framework (aMember), just like XenForo. Setting up the code above produces this error:
    Exception_Zend: "Registry is already initialized"

    I spent the past few days trying to extract the code from XenForo that calculates the Permission Combination ID without success.

    I did some digging about the Zend framework and found the "addModuleDirectory()" function, I tried to use that to load the XenForo modules into aMember without success.

    I'm out of ideas.

    Again, I'm trying to force a User Cache rebuild on specific users as their usergroups are modified by aMember. If there's another slick way of doing this I haven't thought of so far, I'm open to ideas. :)

    WOO! I got it working, here's how:
    I setup a updateGroups.php file that contains this:
        $user_Id = $_GET['id'];
            $startTime = microtime(true);
            $fileDir = "community";
            require($fileDir . '/library/XenForo/Autoloader.php');
            XenForo_Autoloader::getInstance()->setupAutoloader($fileDir . '/library');
            XenForo_Application::initialize($fileDir . '/library', $fileDir);
            XenForo_Application::set('page_start_time', $startTime);
            $deps = new XenForo_Dependencies_Public();
            $writer = XenForo_DataWriter::create('XenForo_DataWriter_User');
    and dropped it into the root folder of the server.

    Then I dropped the following code into the "setGroups" function inside the aMember/XenForo plugin. This function is called for database rebuilds as well as each time a user's groups are changed by aMember.

            $ch = curl_init('http://localhost/updateGroup.php?id=' . $record->user_id);
    I use cURL to pass the user's ID to a PHP file that processes the user's permissions and rebuilds them.

    The only drawback is XenForo database rebuilds inside aMember take 4-5 times longer now. But that's a problem I can handle. :)

    Thanks so much guys!
    Last edited: Sep 21, 2013

Share This Page