<p>The problem is that both <a href="https://craftcms.com/docs/plugins/events-reference#users-onSaveUser" rel="nofollow noreferrer">onSaveUser</a> and <a href="https://craftcms.com/docs/plugins/events-reference#users-onBeforeSaveUser" rel="nofollow noreferrer">onBeforeSaveUser</a> get fired from <a href="https://craftcms.com/classreference/services/UsersService#saveUser-detail" rel="nofollow noreferrer">UsersService->saveUser()</a> and user group assignment happens in the <a href="https://craftcms.com/classreference/controllers/UsersController#actionSaveUser-detail" rel="nofollow noreferrer">UsersController->actionSaveUser()</a> <em>after</em> <code>UsersService-&gt;saveUser()</code> has already been called.</p>

<p>You should probably be using <a href="https://craftcms.com/docs/plugins/events-reference#userGroups-onBeforeAssignUserToGroups" rel="nofollow noreferrer">onBeforeAssignUserToGroups</a> and <a href="https://craftcms.com/docs/plugins/events-reference#userGroups-onAssignUserToGroups" rel="nofollow noreferrer">onAssignUserToGroups</a> instead.</p>

CC BY-SA 3.0
2016-12-19T19:03:06.387Z
2016-12-19T19:03:06.387Z
Brad Bell
<p>I'm trying to make a very basic plugin that will send an email alert to a User if an Admin changes their User Group.</p>

<p>I thought I could use the onBeforeSaveUser to get the User's current User Group, and then use the onSaveUser event to compare that with the Event's User Group - and then if they differ, do the mail sending.</p>

<p>It doesn't work, and I don't know why. Both events always get the same value for the User Group. What am I not understanding correctly?</p>

<pre><code>private $existingGroups = NULL;

public function init()
{
    craft()-&gt;on('users.onBeforeSaveUser', function(Event $event) {
        $eventUser = $event-&gt;params['user']; // get the user we are editing

        $user = craft()-&gt;users-&gt;getUserById($eventUser-&gt;id); // get that user's existing information, before we edit it

        $this-&gt;existingGroups = $user-&gt;getGroups(); // store the groups the User belongs to
        GroupNotificationPlugin::log("existingGroup: " . $this-&gt;existingGroups[0]-&gt;name, LogLevel::Info);
    });

    craft()-&gt;on('users.onSaveUser', function(Event $event) {
        $user = $event-&gt;params['user']; // get the user we are editing

        GroupNotificationPlugin::log("Was: " . $this-&gt;existingGroups[0]-&gt;name, LogLevel::Info);
        GroupNotificationPlugin::log("Now: " . $user-&gt;getGroups()[0]-&gt;name, LogLevel::Info);

        if($this-&gt;existingGroups[0]-&gt;name != $user-&gt;getGroups()[0]-&gt;name){
            GroupNotificationPlugin::log("Changing group", LogLevel::Info);
        }
    });
}
</code></pre>

<p>Was: XXX and Now: XXX are always reporting the same, even when I know that I have changed the User Group (unchecking the old one, checking a new one, saving).</p>

<plugin-development><users>
onBeforeSaveUser vs onSaveUser
# onBeforeSaveUser vs onSaveUser

(Asked by: username_0 on 2016-12-19)

I'm trying to make a very basic plugin that will send an email alert to a User if an Admin changes their User Group.
I thought I could use the onBeforeSaveUser to get the User's current User Group, and then use the onSaveUser event to compare that with the Event's User Group - and then if they differ, do the mail sending.
It doesn't work, and I don't know why. Both events always get the same value for the User Group. What am I not understanding correctly?
private $existingGroups = NULL;

public function init()
{
    craft()->on('users.onBeforeSaveUser', function(Event $event) {
        $eventUser = $event->params['user']; // get the user we are editing

        $user = craft()->users->getUserById($eventUser->id); // get that user's existing information, before we edit it

        $this->existingGroups = $user->getGroups(); // store the groups the User belongs to
        GroupNotificationPlugin::log("existingGroup: " . $this->existingGroups[0]->name, LogLevel::Info);
    });

    craft()->on('users.onSaveUser', function(Event $event) {
        $user = $event->params['user']; // get the user we are editing

        GroupNotificationPlugin::log("Was: " . $this->existingGroups[0]->name, LogLevel::Info);
        GroupNotificationPlugin::log("Now: " . $user->getGroups()[0]->name, LogLevel::Info);

        if($this->existingGroups[0]->name != $user->getGroups()[0]->name){
            GroupNotificationPlugin::log("Changing group", LogLevel::Info);
        }
    });
}

Was: XXX and Now: XXX are always reporting the same, even when I know that I have changed the User Group (unchecking the old one, checking a new one, saving).

**Tags:** plugin-development, users


---

## 1 answers

### Answer by username_1 on 2016-12-19. Score: 2 | Accepted answer

The problem is that both [onSaveUser](https://craftcms.com/docs/plugins/events-reference#users-onSaveUser) and [onBeforeSaveUser](https://craftcms.com/docs/plugins/events-reference#users-onBeforeSaveUser) get fired from [UsersService->saveUser()](https://craftcms.com/classreference/services/UsersService#saveUser-detail) and user group assignment happens in the [UsersController->actionSaveUser()](https://craftcms.com/classreference/controllers/UsersController#actionSaveUser-detail) *after* `UsersService->saveUser()` has already been called.

You should probably be using [onBeforeAssignUserToGroups](https://craftcms.com/docs/plugins/events-reference#userGroups-onBeforeAssignUserToGroups) and [onAssignUserToGroups](https://craftcms.com/docs/plugins/events-reference#userGroups-onAssignUserToGroups) instead.


---

