We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 22303 MODX Staff
    • 10,725 Posts
    User settings are stored in the user session, so if you change the settings for a user, you will likely need to flush the user session in order to see the new settings.
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      And is there an API function for flushing the user session?
        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
        • 29606
        • 63 Posts
        Ok, it seems to be working now, using the following code:

        if (!isset($modx->_userConfig['SMF_ID']))
        {
          $setting =&$modx->newObject('modUserSetting');
          $setting->set('user', $id);
          $setting->set('key', 'SMF_ID');
          $setting->set('value', $value);
          // associate it with a modUser
          $user->addOne($setting);
          // save it
          $setting->save();
          $modx->reloadConfig();
        }


        and the value is available when the user is logged in at $modx->_userConfig[’SMF_ID’]
        But it seems the user have to log out then log in again for it to be available.
        Anyway, that serves my purpose, thanks everybody for the help !!
          • 21257 MODX Staff
          • 730 Posts
          Quote from: Thalion at Aug 11, 2009, 05:38 PM

          Ok, this worked, now I have a 5000 lines error log. What am I looking for ?
          Well, we’re down the rabbit-hole now, huh? grin
          Look for some occurance of "error" - xpdo will give you some information about any failed sql queries. It’s possible that there aren’t any, but I thought it worth a look.
          (You’ll want to turn that logging back off after solving this, or else it could grow pretty quickly on a production site.)
            Mike Schell
            Lead Developer, MODX Cloud
            Email: [email protected]
            GitHub: https://github.com/netProphET/
            Twitter: @mkschell
            • 29606
            • 63 Posts
            Still working on this, and I think I have it pinned down :
            One user ’A’ logs in into the Web context using the "login" snippet. As of now, he doesn’t have the ’SMF_ID’ setting - as verified with
            if (!isset($modx->_userConfig[’SMF_ID’]))
            So I create it using the code already mentionned.
            Now, user ’A’ has the SMF_ID setting.

            But, there still is a problem, that is if user B logs in from the same browser (so I’m thinking ’cookies’, here), then for him (B), $modx->_userConfig[’SMF_ID’] returns the value for user A, until I flush the sessions.

            So I join sottwell : is there an API to flush user session ?
              • 22303 MODX Staff
              • 10,725 Posts
              Did you log out as user A before attempting to log in as user B? I don’t how it could have the same session unless you are still logged in as user A. But if it does, that definitely needs to be addressed...
                • 29606
                • 63 Posts
                Well, that’s easy to check :
                from a fresh installation, load the package ’login’, then create an uncached web ressource with
                [[!login]]
                [[!TestSetting]]


                Snippet ’TestSetting’ is :
                <?php
                if ($modx->getLoginUserName($modx->context->key))
                {
                  $modx->reloadConfig();
                  $user= $modx->getUser();
                  $id=$user->get('id');
                
                if (!isset($modx->_userConfig['SMF_ID']))
                {
                  $setting =&$modx->newObject('modUserSetting');
                  $setting->set('user', $id);
                  $setting->set('key', 'SMF_ID');
                  $setting->set('value', $id);
                  // associate it with a modUser
                  $user->addOne($setting);
                  // save it
                  $setting->save();
                  $user->save();
                  }
                  $modx->reloadConfig();
                }
                
                return 'Setting for user ID : ' . $id . ' is : ' . $modx->_userConfig['SMF_ID'];
                ?>


                Then create userA and userB.
                Login with UserA, you get Setting for user ID : 1 is :
                Then logout, and login again with userA, you get Setting for user ID : 1 is : 1
                Then logout, and login with userB, you get Setting for user ID : 2 is : 1
                no matter how many times you login and logout...
                  • 22303 MODX Staff
                  • 10,725 Posts
                  I’m not sure where that code came from, but if I create two pages, one with the Login snippet followed by a SetUserSetting snippet, and another with UserSettingTest (see code below), it displays the proper data every time...

                  SetUserSetting:
                  <?php
                  if ($modx->user->isAuthenticated('web')) {
                    $setting =$modx->newObject('modUserSetting');
                    $setting->set('user', $modx->user->get('id'));
                    $setting->set('key', 'SMF_ID');
                    $setting->set('value', $modx->user->get('id'));
                    $setting->save();
                  }
                  ?>


                  UserSettingTest
                  <?php
                  return $modx->getOption('SMF_ID', null, "No SMF_ID is set");
                  ?>


                  $modx->reloadConfig() is an internal method and not really intended for use in a front-end snippet.
                    • 29606
                    • 63 Posts
                    Quote from: OpenGeek at Aug 13, 2009, 03:51 PM

                    I’m not sure where that code came from, but if I create two pages, one with the Login snippet followed by a SetUserSetting snippet, and another with UserSettingTest (see code below), it displays the proper data every time...

                    I tried your code, and it still doesn’t work for the second user, it still displays the data from the first one if I don’t flush all user sessions in between huh
                      • 22303 MODX Staff
                      • 10,725 Posts
                      Are you calling the snippet cached? That’s the only thing I can think of. It’s working as expected for me here...