4

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?

7
  • 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.
    – Brad Bell
    Feb 19 '15 at 23:27
  • Doing var_dump($user->getGroups()); and var_dump($user->getErrors()); both return empty arrays. Feb 20 '15 at 1:10
  • if you die('here'); inside the if $user->isInGroup() check for a user you know belongs to that group, does it stop?
    – Brad Bell
    Feb 20 '15 at 1:13
  • Nope, just completes the user registration. Feb 20 '15 at 2:35
  • When registering from the CP, are you unchecking "Send an activation email now?"
    – Brad Bell
    Feb 20 '15 at 5:57
1

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 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 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, but you'd run into the same limitation.

What you can do, however, is directly check against $_POST (craft()->request->getPost()) to see if the user group you're interested in is there in the onActivateUser event.

11
  • 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. Feb 21 '15 at 15:41
  • Are you on the latest version? That behavior changed semi-recently.
    – Brad Bell
    Feb 21 '15 at 17:06
  • I was on Craft 2.3.2632, but just updated to Craft 2.3.2635, and I'm still seeing the same behavior. Feb 23 '15 at 15:01
  • Weird... I'm not able to reproduce that locally. Maybe you've got another plugin interfering?
    – Brad Bell
    Feb 23 '15 at 19:35
  • Here's a screencast with all plugins disabled: 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. Feb 23 '15 at 20:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.