<p>That's because groups are not attached to the user directly. At the moment Craft stores the user it hasn't handled the permissions not the user groups.</p>

<p>The workflow is</p>

<ol>
<li>Handling all kinds of variables and data</li>
<li>Fetch/Create a user</li>
<li>Populate the new field values for the user</li>
<li>Store the user</li>
<li>Handle verifications such as sending them an email</li>
<li>Assign the user to groups and grant them permissions</li>
</ol>

<p>At the moment your event triggers the user wasn't assigned to the groups.</p>

<p>If you want to change users depending on their groups you should use the  <code>Users::EVENT_AFTER_ASSIGN_USER_TO_GROUPS</code> event </p>

CC BY-SA 4.0
2018-10-25T17:06:30.247Z
2018-10-25T17:06:30.247Z
Robin Schambach
<pre><code>Event::on(\craft\services\Elements::class, \craft\services\Elements::EVENT_AFTER_SAVE_ELEMENT, function(Event $event) {
    if ($event-&gt;element instanceof \craft\elements\User) {
        $user = Craft::$app-&gt;getUsers()-&gt;getUserById($event-&gt;element-&gt;id);
        if ($user-&gt;isInGroup('abc')) {
            // Do something
        } else {
            // Do something different
        }
    }
});
</code></pre>

<p>I created a C3 module on pluginfactory.io. The code above works just fine in detecting when a user is saved/updated.</p>

<p>For sake of simplicity I have two user groups 1) <code>abc</code> 2) <code>def</code>.</p>

<p>When I uncheck the user from <code>def</code> and check him into <code>abc</code> (or the other way around), and save the user, my code won't register that the user group has changed, but thinks the user is still in the previously selected group. If I re-save the user (without changing groups), suddenly the code sees the correct group.</p>

<p>I assume that despite using <code>EVENT_AFTER_SAVE_ELEMENT</code>, the system still saves the user group change to the db after the <code>Event</code> triggers. Could that be it?</p>

<p>It's weird though, because any other field I pull from the user, e.g. if I change their email, my code is able to pick up the new email on the first save, so why not the change in user group?</p>

<p>Any assistance is greatly appreciated,
cheers!</p>

<p>Edit: Made problem clearer to understand.</p>

<craft3><modules>
Module: EVENT_AFTER_SAVE_ELEMENT won't detect changed user group
# Module: EVENT_AFTER_SAVE_ELEMENT won't detect changed user group

(Asked by: username_0 on 2018-10-25)

Event::on(\craft\services\Elements::class, \craft\services\Elements::EVENT_AFTER_SAVE_ELEMENT, function(Event $event) {
    if ($event->element instanceof \craft\elements\User) {
        $user = Craft::$app->getUsers()->getUserById($event->element->id);
        if ($user->isInGroup('abc')) {
            // Do something
        } else {
            // Do something different
        }
    }
});

I created a C3 module on pluginfactory.io. The code above works just fine in detecting when a user is saved/updated.
For sake of simplicity I have two user groups 1) abc 2) def.
When I uncheck the user from def and check him into abc (or the other way around), and save the user, my code won't register that the user group has changed, but thinks the user is still in the previously selected group. If I re-save the user (without changing groups), suddenly the code sees the correct group.
I assume that despite using EVENT_AFTER_SAVE_ELEMENT, the system still saves the user group change to the db after the Event triggers. Could that be it?
It's weird though, because any other field I pull from the user, e.g. if I change their email, my code is able to pick up the new email on the first save, so why not the change in user group?
Any assistance is greatly appreciated,
cheers!
Edit: Made problem clearer to understand.

**Tags:** craft3, modules


---

## 1 answers

### Answer by username_1 on 2018-10-25. Score: 3 | Accepted answer

That's because groups are not attached to the user directly. At the moment Craft stores the user it hasn't handled the permissions not the user groups.

The workflow is

1. Handling all kinds of variables and data
2. Fetch/Create a user
3. Populate the new field values for the user
4. Store the user
5. Handle verifications such as sending them an email
6. Assign the user to groups and grant them permissions

At the moment your event triggers the user wasn't assigned to the groups.

If you want to change users depending on their groups you should use the `Users::EVENT_AFTER_ASSIGN_USER_TO_GROUPS` event


---

