We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 26503
    • 620 Posts
    Sean Kimball Reply #1, 12 years ago
    I'm trying to change a new user's class_key value when they are created VIA the manager.

    In a plugin that fires on the onUserSave event:

    <?php
    
    $uid = $_POST['id'];
    
    $sql = "update modx_users set `class_key` = 'extUser' where id = $uid;";
    
    $modx->log(modX::LOG_LEVEL_ERROR, 'query = ' . $sql);
    
    $query = $modx->query($sql);
    
    return;


    Which works when you EDIT an EXISTING user, but does not work if you try to create a new user.

    Any thoughts on how to do this? none of the events [*obviously*] look like they fire when a new user is created.
      *** Not just websites, we also create signage, banners, print, trade show displays and more! ***

      Sean Kimball CLP, CLS.
      Technical Director / Sr. Developer | BigBlock Studios
      ._______________________________________________.
      Bigblock Studios http://www.bigblockstudios.ca Web site design & development.
      27-1300 King Street East. Box 167 Oshawa, Ontario L1H8J4 Canada.
      phone/fax: 905-426-5525
      • 3749
      • 24,544 Posts
      I'm not sure that event fires reliably for new users. Looking at the code, it appears that it only gets called when the user is saved with $user->save(), not when the user/create or user/update processors are called.

      I think OnUserFormSave is called for both create and update, but I believe it only fires when you use the Create/Edit User form in the Manager.

      Be sure to save the user after setting the class_key.

      Note that changing the user's class_key could possibly cause trouble with extras that extend modUser, though it's not a problem with ClassExtender (which leaves the class_key as modUser).

      IMO, changing the users' class_key is only necessary if they are separate from "regular" users and you need to search among just the users with the modified class_key. If *all* users get the new class_key, it's not necessary to change it.
        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
        • 26503
        • 620 Posts
        Sean Kimball Reply #3, 12 years ago
        The problem is that the user has not been saved yet, no user object or database object to edit. I tried changing the $_POST data, but no luck. [very frustrating]

        Quote from: BobRay at Sep 23, 2014, 11:36 PM
        I'm not sure that event fires reliably for new users. Looking at the code, it appears that it only gets called when the user is saved with $user->save(), not when the user/create or user/update processors are called.

        I think OnUserFormSave is called for both create and update, but I believe it only fires when you use the Create/Edit User form in the Manager.

        Be sure to save the user after setting the class_key.

        Note that changing the user's class_key could possibly cause trouble with extras that extend modUser, though it's not a problem with ClassExtender (which leaves the class_key as modUser).

        IMO, changing the users' class_key is only necessary if they are separate from "regular" users and you need to search among just the users with the modified class_key. If *all* users get the new class_key, it's not necessary to change it.
          *** Not just websites, we also create signage, banners, print, trade show displays and more! ***

          Sean Kimball CLP, CLS.
          Technical Director / Sr. Developer | BigBlock Studios
          ._______________________________________________.
          Bigblock Studios http://www.bigblockstudios.ca Web site design & development.
          27-1300 King Street East. Box 167 Oshawa, Ontario L1H8J4 Canada.
          phone/fax: 905-426-5525
          • 3749
          • 24,544 Posts
          Erm ... that's impossible. OnUserFormSave only fires after the user has been successfully saved.

          This would be the entire code for the plugin (unless there are conditions you need to check):

          $user->set('class_key', 'Whatever');
          $user->save();
            Did I help you? Buy me a beer
            Get my Book: MODX:The Official Guide
            MODX info for everyone: http://bobsguides.com/modx.html
            My MODX Extras
            Bob's Guides is now hosted at A2 MODX Hosting
            • 26503
            • 620 Posts
            Sean Kimball Reply #5, 12 years ago
            ok - hmm, maybe I'm not seeing that because if we try to get any user attributes in the plugin [$user->get('id');] modx will assume the current user [manager] ~ how do we get the ID of the user that was just saved in the plugin for OnUserFormSave ??


            Quote from: BobRay at Sep 24, 2014, 04:56 PM
            Erm ... that's impossible. OnUserFormSave only fires after the user has been successfully saved.

            This would be the entire code for the plugin (unless there are conditions you need to check):

            $user->set('class_key', 'Whatever');
            $user->save();
              *** Not just websites, we also create signage, banners, print, trade show displays and more! ***

              Sean Kimball CLP, CLS.
              Technical Director / Sr. Developer | BigBlock Studios
              ._______________________________________________.
              Bigblock Studios http://www.bigblockstudios.ca Web site design & development.
              27-1300 King Street East. Box 167 Oshawa, Ontario L1H8J4 Canada.
              phone/fax: 905-426-5525
              • 3749
              • 24,544 Posts
              In a plugin attached to OnUserFormSave, the $user object will always be the user being created or edited.
                Did I help you? Buy me a beer
                Get my Book: MODX:The Official Guide
                MODX info for everyone: http://bobsguides.com/modx.html
                My MODX Extras
                Bob's Guides is now hosted at A2 MODX Hosting
                • 26503
                • 620 Posts
                Sean Kimball Reply #7, 12 years ago
                ouch - it's just me, I must be losing my mind!

                Quote from: BobRay at Sep 25, 2014, 06:12 PM
                In a plugin attached to OnUserFormSave, the $user object will always be the user being created or edited.
                  *** Not just websites, we also create signage, banners, print, trade show displays and more! ***

                  Sean Kimball CLP, CLS.
                  Technical Director / Sr. Developer | BigBlock Studios
                  ._______________________________________________.
                  Bigblock Studios http://www.bigblockstudios.ca Web site design & development.
                  27-1300 King Street East. Box 167 Oshawa, Ontario L1H8J4 Canada.
                  phone/fax: 905-426-5525
                  • 3749
                  • 24,544 Posts
                  You may have been thinking of $modx->user, which (if set) will be the current logged-in user.
                    Did I help you? Buy me a beer
                    Get my Book: MODX:The Official Guide
                    MODX info for everyone: http://bobsguides.com/modx.html
                    My MODX Extras
                    Bob's Guides is now hosted at A2 MODX Hosting