<p>If you have "Verify Email Addresses" checked in Settings->Users->Settings <em>and</em> you uncheck "Send activation email now?" users will start in a "pending" state, so the <a href="http://buildwithcraft.com/docs/plugins/events-reference#users-onActivateUser" rel="nofollow">onActivateUser</a> event won't get fired.</p>

<p>If you uncheck "Verify Email Addresses" in Settings->Users->Settings, the "Send activation email now?" checkbox will disappear when registering a new user, users will start in an "active" state and the <a href="http://buildwithcraft.com/docs/plugins/events-reference#users-onActivateUser" rel="nofollow">onActivateUser</a> event will fire.</p>

<p>As you've found, user group information isn't available to the user on the same request they get created in because user group permissions don't get processed until <em>after</em> the onSaveUser event has fired.  You could use <a href="http://buildwithcraft.com/docs/plugins/events-reference#users-onSaveUser" rel="nofollow">onSaveUser</a>, but you'd run into the same limitation.</p>

<p>What you can do, however, is directly check against $_POST (<a href="http://buildwithcraft.com/classreference/services/HttpRequestService#getPost-detail" rel="nofollow">craft()->request->getPost()</a>) to see if the user group you're interested in is there in the onActivateUser event.</p>

CC BY-SA 3.0
2015-02-21T15:41:54.130Z
Hmmm but the onActivateUser event is definitely firing (with the checkbox unchecked). Also, the users are not in the pending state, but they are active.
Trevor Davis
CC BY-SA 3.0
2015-02-21T17:06:08.743Z
Are you on the latest version? That behavior changed semi-recently.
Brad Bell
CC BY-SA 3.0
2015-02-23T15:01:18.547Z
I was on Craft 2.3.2632, but just updated to Craft 2.3.2635, and I'm still seeing the same behavior.
Trevor Davis
CC BY-SA 3.0
2015-02-23T19:35:09.057Z
Weird... I'm not able to reproduce that locally. Maybe you've got another plugin interfering?
Brad Bell
CC BY-SA 3.0
2015-02-23T20:24:28.750Z
Here's a screencast with all plugins disabled: http://cl.ly/2G0O0y3k1h0s

It shows that users are automatically activated, and the users.onActivateUser event is getting fired, and the $user->getErrors() array is empty.
Trevor Davis
CC BY-SA 3.0
2015-02-23T20:39:10.603Z
Are you doing the test from an admin account or an account that has "register user" permissions?
Brad Bell
CC BY-SA 3.0
2015-02-23T20:58:57.473Z
An admin account
Trevor Davis
CC BY-SA 3.0
2015-02-23T21:01:57.410Z
Oh! You've probably unchecked "Verify Email Addresses" in Settings->Users->Settings. Let me do some testing in that scenario.
Brad Bell
CC BY-SA 3.0
2015-02-23T21:05:57.303Z
Correct, it is unchecked.
Trevor Davis
CC BY-SA 3.0
2015-02-24T00:28:27.223Z
Updated my answer.
Brad Bell
CC BY-SA 3.0
2015-02-24T15:06:45.493Z
Got it, makes sense now. I'll go the $_POST route, but I'd also like to add a feature request for a user registration event that fires after all the user information has been saved.
Trevor Davis
CC BY-SA 3.0
2015-02-21T00:02:36.920Z
2015-02-24T00:28:15.870Z
2015-02-24T00:28:15.870Z
Brad Bell
<p>I have a situation where I want to assign a default password when a user is registered in the CP, but only if they are assigned to a specific user group. Plugin code looks like this:</p>

<pre><code>craft()-&gt;on('users.onActivateUser', function(Event $event)
{
    $user = $event-&gt;params['user'];

    // Test to see if a user is in group
    if ($user-&gt;isInGroup('groupName'))
    {
        $user-&gt;newPassword = 'Password123';
        craft()-&gt;users-&gt;saveUser($user);
    }
});
</code></pre>

<p>I've tried latching on to both the users.onSaveUser and users.onActivateUser events, and in both situations, there are no groups are assigned to the user. Is there an event that is at the absolute end of user registration that I can watch to access their user groups?</p>

CC BY-SA 3.0
2015-02-19T23:27:02.443Z
You sure there are no groups assigned to the user, or $user isn't just failing validation when you're saving?  After saving, try calling $user->getErrors() and see if there is anything there.
Brad Bell
CC BY-SA 3.0
2015-02-20T01:10:23.553Z
Doing `var_dump($user->getGroups());` and `var_dump($user->getErrors());` both return empty arrays.
Trevor Davis
CC BY-SA 3.0
2015-02-20T01:13:05.373Z
if you `die('here');` inside the `if $user->isInGroup()` check for a user you know belongs to that group, does it stop?
Brad Bell
CC BY-SA 3.0
2015-02-20T02:35:20.483Z
Nope, just completes the user registration.
Trevor Davis
CC BY-SA 3.0
2015-02-20T05:57:43.070Z
When registering from the CP, are you unchecking "Send an activation email now?"
Brad Bell
CC BY-SA 3.0
2015-02-20T14:44:16.103Z
Correct, that checkbox is not checked.
Trevor Davis
CC BY-SA 3.0
2015-02-20T17:46:02.923Z
I'm stumped... I think what you have should work fine. If you want to send the plugin to support@buildwithcraft.com I can take a closer look at it and post any results here.
Brad Bell
<users><registration>
Create default password for new user registration in CP
# Create default password for new user registration in CP

(Asked by: username_0 on 2015-02-19)

I have a situation where I want to assign a default password when a user is registered in the CP, but only if they are assigned to a specific user group. Plugin code looks like this:
craft()->on('users.onActivateUser', function(Event $event)
{
    $user = $event->params['user'];

    // Test to see if a user is in group
    if ($user->isInGroup('groupName'))
    {
        $user->newPassword = 'Password123';
        craft()->users->saveUser($user);
    }
});

I've tried latching on to both the users.onSaveUser and users.onActivateUser events, and in both situations, there are no groups are assigned to the user. Is there an event that is at the absolute end of user registration that I can watch to access their user groups?

**Tags:** users, registration
> **Comments:**
> **username_1**: You sure there are no groups assigned to the user, or $user isn't just failing validation when you're saving?  After saving, try calling $user->getErrors() and see if there is anything there.  
> **username_0**: Doing `var_dump($user->getGroups());` and `var_dump($user->getErrors());` both return empty arrays.  
> **username_1**: if you `die('here');` inside the `if $user->isInGroup()` check for a user you know belongs to that group, does it stop?  
> **username_0**: Nope, just completes the user registration.  
> **username_1**: When registering from the CP, are you unchecking "Send an activation email now?"  
> **username_0**: Correct, that checkbox is not checked.  
> **username_1**: I'm stumped... I think what you have should work fine. If you want to send the plugin to support@buildwithcraft.com I can take a closer look at it and post any results here.  

---

## 1 answers

### Answer by username_1 on 2015-02-21. Score: 1 | Accepted answer

If you have "Verify Email Addresses" checked in Settings->Users->Settings *and* you uncheck "Send activation email now?" users will start in a "pending" state, so the [onActivateUser](http://buildwithcraft.com/docs/plugins/events-reference#users-onActivateUser) event won't get fired.

If you uncheck "Verify Email Addresses" in Settings->Users->Settings, the "Send activation email now?" checkbox will disappear when registering a new user, users will start in an "active" state and the [onActivateUser](http://buildwithcraft.com/docs/plugins/events-reference#users-onActivateUser) event will fire.

As you've found, user group information isn't available to the user on the same request they get created in because user group permissions don't get processed until *after* the onSaveUser event has fired. You could use [onSaveUser](http://buildwithcraft.com/docs/plugins/events-reference#users-onSaveUser), but you'd run into the same limitation.

What you can do, however, is directly check against $\_POST ([craft()->request->getPost()](http://buildwithcraft.com/classreference/services/HttpRequestService#getPost-detail)) to see if the user group you're interested in is there in the onActivateUser event.

> **Comments:**
> **username_0**: Hmmm but the onActivateUser event is definitely firing (with the checkbox unchecked). Also, the users are not in the pending state, but they are active.  
> **username_1**: Are you on the latest version? That behavior changed semi-recently.  
> **username_0**: I was on Craft 2.3.2632, but just updated to Craft 2.3.2635, and I'm still seeing the same behavior.  
> **username_1**: Weird... I'm not able to reproduce that locally. Maybe you've got another plugin interfering?  
> **username_0**: Here's a screencast with all plugins disabled: http://cl.ly/2G0O0y3k1h0s  It shows that users are automatically activated, and the users.onActivateUser event is getting fired, and the $user->getErrors() array is empty.  
> **username_1**: Are you doing the test from an admin account or an account that has "register user" permissions?  
> **username_0**: An admin account  
> **username_1**: Oh! You've probably unchecked "Verify Email Addresses" in Settings->Users->Settings. Let me do some testing in that scenario.  
> **username_0**: Correct, it is unchecked.  
> **username_1**: Updated my answer.  
> **username_0**: Got it, makes sense now. I'll go the $_POST route, but I'd also like to add a feature request for a user registration event that fires after all the user information has been saved.  

---

