We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 26503
    • 620 Posts
    I'm trying to create a plugin that will notify users that they have been added to a group by an administrator & am trying to use the OnUserAddToGroup event to do it.

    It was working then it mysteriously stopped working!

    it only appeared to work from the Security->access Controls & only if you right clicked and added a user to a group, it would not work if you dragged a user from one group to another and it would not work if you went through Security->Manage Users->access Permissions & tried to add them there.

    It was sending email and getting user information and everything, now all the plugin has in it is:
    <?php
    
    $modx->log(modX::LOG_LEVEL_ERROR, 'Group add plugin running');
    
    
    


    and won't even log that the plugin was run!!

    I've dumped the cache several times, logged in and out, verified that the plugin is enabled... all appears correct!

    what could be going on here?

    -sean
      *** 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
      • 26503
      • 620 Posts
      Well this is frustrating.

      The plugin will run if I place it on the OnUserRemoveFromGroup event!!!

      Argh!

      Just tried the same plugin on another modx install - same problem.

      here is the complete code [that works on the remove event]
      <?php
      
      $profile = $user->getOne('Profile');
      
      $adminemail = $modx->config['registration_admin_email'];
      
      if (empty($profile)) {
          $modx->log(modX::LOG_LEVEL_ERROR,'Could not retrieve profile for user: ' . $user->get('username'));
          return true;
      }else{
      
      	$placeholders = array(
      		'username' => $user->get('username'),
      		'fullname' => $profile->get('fullname'),
      		'email' => $profile->get('email'),
      		'password' => $user->get('password'),
      		'city' => $profile->get('city'),
      		'state' => $profile->get('state'),
      		'date' => date('Y-m-d H:i')
      		);
      	$message = $modx->getChunk('EmailApplicationApprovedTpl',$placeholders);
      	
      	$modx->getService('mail', 'mail.modPHPMailer');
      	$modx->mail->set(modMail::MAIL_BODY,$message);
      	$modx->mail->set(modMail::MAIL_FROM,'[email protected]');
      	$modx->mail->set(modMail::MAIL_FROM_NAME,'CWM Admin');
      	$modx->mail->set(modMail::MAIL_SENDER,'CWM Admin');
      	$modx->mail->set(modMail::MAIL_SUBJECT,'Your Application at CWM has been approved!');
      	$modx->mail->address('to',$adminemail);
      	$modx->mail->address('reply-to','[email protected]');
      	$modx->mail->setHTML(true);
      	
      	
      	if (!$modx->mail->send()) {
      	    $modx->log(modX::LOG_LEVEL_ERROR,'An error occurred while trying to send the email: '.$modx->mail->mailer->ErrorInfo);
      	}else{
      		$modx->log(modX::LOG_LEVEL_ERROR,'Application success email sent to'. $user->get('username'));
      	}
      	$modx->mail->reset();
      
      }
      
      [ed. note: sean69 last edited this post 11 years, 9 months ago.]
        *** 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 don't see anything wrong, though I would add an if($user)() at the top so you're sure the $user object is getting through (if you're getting the profile, though, it obviously is).

        Are any of the elements used (plugin or chunk) static elements? I've had trouble with static elements in plugins.

        Have you checked to make sure mail is still working? (e.g., QuickEmail). Your host may have changed the mail setup.

        Double-check that the Plugin is not disabled and is still connected to the event.

        I assume that by dumping the cache, you mean you manually deleted all files in the core/cache directory. If not, try that -- and clear your browser cache and cookies for good measure.

        Going into PhpMyAdmin and repairing all the tables in the DB can't hurt.



        ------------------------------------------------------------------------------------------
        PLEASE, PLEASE specify the version of MODX you are using.
        MODX info for everyone: http://bobsguides.com/modx.html [ed. note: BobRay last edited this post 11 years, 9 months ago.]
          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
        • Sounds like a bug to me, probably worth filing.

          It may be more reliable to query the database tables to verify from the source who's in what group... but that's a long work-around.
            • 26503
            • 620 Posts
            Quote from: BobRay at Jul 22, 2012, 05:34 AM


            Are any of the elements used (plugin or chunk) static elements? I've had trouble with static elements in plugins.

            Nope - I've had similar problems with static elements myself. [and code mirror to, it occasionally 'adds' code!!]

            Have you checked to make sure mail is still working? (e.g., QuickEmail). Your host may have changed the mail setup.

            Mail works, I was testing the event by trying to get it to log something... [I'm the host BTW wink ]


            Double-check that the Plugin is not disabled and is still connected to the event.


            Triple checked in fact, completely removed and recreated a number of times.



            I assume that by dumping the cache, you mean you manually deleted all files in the core/cache directory. If not, try that -- and clear your browser cache and cookies for good measure.


            Absolutely, still no love.


            Going into PhpMyAdmin and repairing all the tables in the DB can't hurt.



            Tried it ... nothing.




            I'm going to try to get 'anything' to run on that event and see if it even does anything, it's not documented so it's possible it does not even execute when I think it does [though I have tried changing user groups via user management without success either]

            It is starting to look like a bug.
              *** 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
              • 26503
              • 620 Posts
              Pretty convinced that the OnUserAddToGroup event does not run:

              I put this in a plugin and set it to trigger on every UserGroups event:

              $eventname = $modx->event->name;
              $modx->log(modX::LOG_LEVEL_ERROR,'System event running = ' . $eventname);
              


              I then tried adding a user to a group by every method I know of, editing the user in user manager, adding the user to a group under access controls & adding the user on the users tab while editing the group.

              this is all I got:

               [2012-07-22 14:56:52] (ERROR @ /index.php) `[[+resource]]` is not a valid integer and may not be passed to makeUrl()
              [2012-07-22 14:56:56] (ERROR @ /connectors/security/group.php) System event running = OnUserBeforeRemoveFromGroup
              [2012-07-22 14:56:56] (INFO @ /connectors/security/group.php) Removed modUserGroupMember instance with primary key 213
              [2012-07-22 14:56:56] (ERROR @ /connectors/security/group.php) System event running = OnUserRemoveFromGroup
              [2012-07-22 14:58:44] (ERROR @ /connectors/security/group.php) System event running = OnUserGroupBeforeSave
              [2012-07-22 14:58:44] (ERROR @ /connectors/security/group.php) System event running = OnUserGroupSave
              [2012-07-22 14:58:44] (ERROR @ /connectors/security/group.php) System event running = OnUserGroupBeforeSave
              [2012-07-22 14:58:44] (ERROR @ /connectors/security/group.php) System event running = OnUserGroupSave
              [2012-07-22 14:58:44] (ERROR @ /connectors/security/group.php) System event running = OnUserGroupBeforeSave
              [2012-07-22 14:58:44] (ERROR @ /connectors/security/group.php) System event running = OnUserGroupSave
              [2012-07-22 14:58:44] (INFO @ /connectors/security/group.php) Removed modUserGroupMember instance with primary key 70
              





              No OnUserAddToGroup .... filing a bug report.
                *** 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
                Sometimes System Events and/or processors are added to the core before the code to use them gets created. The addUserToGroup event didn't exist when I wrote the book, so it must be fairly new. The adduser.php processor exists and it does invoke OnUserAddToGroup, but it may be for future use. It's in core/model/modx/processors/security/group/.


                Looking at the joinGroup() method of the user object, which may (or may not) be what's used at present, no events are invoked.

                I think you may be SOL, because I suspect that when a user is added to a group, the modUserGroupMember object is created without saving the user.

                Your only hope would be first see if OnUserSave fires when you add the user to a group by various methods (by using your code tied to OnUserSave). If it does, you may be able to detect a change in user group by comparing what's in the DB to what's in the $scriptProperties array (possibly $scriptProperties['data']). I'm doing that to detect changed TVs in a plugin I'm working on that's connected to OnDocFormSave.

                The OnUserSave event might be fired when you add a user to a group on the Access Permissions tab in Manage Users. I think that would be the best bet.


                ------------------------------------------------------------------------------------------
                PLEASE, PLEASE specify the version of MODX you are using.
                MODX info for everyone: http://bobsguides.com/modx.html

                  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
                  Quote from: BobRay at Jul 23, 2012, 06:19 AM
                  Sometimes System Events and/or processors are added to the core before the code to use them gets created. The addUserToGroup event didn't exist when I wrote the book, so it must be fairly new. The adduser.php processor exists and it does invoke OnUserAddToGroup, but it may be for future use. It's in core/model/modx/processors/security/group/.

                  I see that it [addUserToGroup] is supposed to be running but it does not, is there somewhere else to look to see if thing is actually enabled or in use?

                  I've tried adding

                  
                  if ($this->xpdo instanceof modX) {
                              $this->xpdo->invokeEvent('OnUserBeforeAddToGroup',array(
                                   'groupId' => $groupId,
                  
                                  ));
                        }
                  


                  to the joingroup function in core/model/modx/moduser.class.php ... but it does not 'just work'
                    *** 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 guess that means joinGroup() is one of the MODX "convenience functions" that are for developers but not used in the core code. I just did a search and can't find it being called anywhere.

                    The fact that OnAddUserToGroup never fires when users are added to a group in the Manager pretty much guarantees that it's not in use anywhere. I couldn't find any code that references it, but calls to processors are hard to find because they're often dynamic, so it's hard to be sure.

                    My guess is that all the places that add a user to a group simply create the modUserGroupMember on the spot, which means that you're not going to get anywhere with the current code unless, as I suggested above, the user object is saved at that point.


                    ------------------------------------------------------------------------------------------
                    PLEASE, PLEASE specify the version of MODX you are using.
                    MODX info for everyone: http://bobsguides.com/modx.html

                      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
                      yea - it's looking pretty bleak.... time for a 'rethink'
                        *** 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