Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-02-21T15:41:54.130Z
Answers.Text: 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.
Answers.UserDisplayName: Trevor Davis
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-02-21T17:06:08.743Z
Answers.Text: Are you on the latest version? That behavior changed semi-recently.
Answers.UserDisplayName: Brad Bell
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-02-23T15:01:18.547Z
Answers.Text: I was on Craft 2.3.2632, but just updated to Craft 2.3.2635, and I'm still seeing the same behavior.
Answers.UserDisplayName: Trevor Davis
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-02-23T19:35:09.057Z
Answers.Text: Weird... I'm not able to reproduce that locally. Maybe you've got another plugin interfering?
Answers.UserDisplayName: Brad Bell
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-02-23T20:24:28.750Z
Answers.Text: 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.
Answers.UserDisplayName: Trevor Davis
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-02-23T20:39:10.603Z
Answers.Text: Are you doing the test from an admin account or an account that has "register user" permissions?
Answers.UserDisplayName: Brad Bell
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-02-23T20:58:57.473Z
Answers.Text: An admin account
Answers.UserDisplayName: Trevor Davis
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-02-23T21:01:57.410Z
Answers.Text: Oh! You've probably unchecked "Verify Email Addresses" in Settings->Users->Settings. Let me do some testing in that scenario.
Answers.UserDisplayName: Brad Bell
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-02-23T21:05:57.303Z
Answers.Text: Correct, it is unchecked.
Answers.UserDisplayName: Trevor Davis
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-02-24T00:28:27.223Z
Answers.Text: Updated my answer.
Answers.UserDisplayName: Brad Bell
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-02-24T15:06:45.493Z
Answers.Text: 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.
Answers.UserDisplayName: Trevor Davis
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-02-21T00:02:36.920Z
Answers.LastActivityDate: 2015-02-24T00:28:15.870Z
Answers.LastEditDate: 2015-02-24T00:28:15.870Z
Answers.UserDisplayName: Brad Bell
Body: <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>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-02-19T23:27:02.443Z
Comments.Text: 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.
Comments.UserDisplayName: Brad Bell
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-02-20T01:10:23.553Z
Comments.Text: Doing `var_dump($user->getGroups());` and `var_dump($user->getErrors());` both return empty arrays.
Comments.UserDisplayName: Trevor Davis
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-02-20T01:13:05.373Z
Comments.Text: if you `die('here');` inside the `if $user->isInGroup()` check for a user you know belongs to that group, does it stop?
Comments.UserDisplayName: Brad Bell
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-02-20T02:35:20.483Z
Comments.Text: Nope, just completes the user registration.
Comments.UserDisplayName: Trevor Davis
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-02-20T05:57:43.070Z
Comments.Text: When registering from the CP, are you unchecking "Send an activation email now?"
Comments.UserDisplayName: Brad Bell
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-02-20T14:44:16.103Z
Comments.Text: Correct, that checkbox is not checked.
Comments.UserDisplayName: Trevor Davis
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-02-20T17:46:02.923Z
Comments.Text: 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.
Comments.UserDisplayName: Brad Bell
Tags: <users><registration>
Title: Create default password for new user registration in CP
ThreadText: # 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.  
  
  ---
  
  