2

I have the following code in my site module:

Event::on(
    User::class,
    User::EVENT_BEFORE_SAVE,
    function (ModelEvent $event) {
        /** @var User $user */
        $user = $event->sender;
        if ($event->isNew === true) {
            $user->pending = true;
        }
    }
);

It was working as expected until I updated from Craft 3.0.25 to 3.0.27.1

I think this issue is related to a change in the src/controllers/UsersController.php file as per GitHub https://github.com/craftcms/cms/commit/db089251994329d0fbe082c0d3ff234a01d82c98

Also, I replaced my 3.0.27.1 UsersController.php with one from a 3.0.25 install, and my code works again.

What is the best/correct/official way to set new user accounts to pending status in a module that won't break with future Craft updates?

I tried this:

Event::on(
    Users::class,
    Users::EVENT_BEFORE_ACTIVATE_USER,
    function (UserEvent $event) {
        $event->isValid = false;
    }
);

but got There was a problem activating the user. when trying to activate the user account.

P.S: Verify email addresses? is disabled in User Settings.

1
  • You could check for certain body params and only invalidate the event in case the body param is set. For example if there is no valid ID set -> it's probably a new user and not an existing one. Another approach is to listen to the previous event and initialize the beforeActivate if it's a new user Oct 26, 2018 at 19:46

1 Answer 1

3

Yeah you’re correct that db089251 is the culprit here, which fixed a bug where user activation events weren’t getting fired when users were first created, if the “Verify email addresses?” setting was disabled.

Your new code is close, but that is going to get called each time Craft tries to activate a user—both when they are first created, and when you try to manually activate a user later on. So you just need to add a condition to your event handler.

As a starting point, maybe just check if it’s a site (front-end) request.

Event::on(
    Users::class,
    Users::EVENT_BEFORE_ACTIVATE_USER,
    function (UserEvent $event) {
        if (Craft::$app->request->getIsSiteRequest()) {
            $event->isValid = false;
        }
    }
);
4
  • I was close! I had actually planned to to check for front-end requests since it is ultimately my goal to review accounts created via my registration form before activating them. Also, it makes sense, since the information contained in accounts created by Admins in the CP is already verified. Oct 27, 2018 at 0:28
  • Just so I understand: did my original code only work because of the bug? Oct 27, 2018 at 11:44
  • @AndreaDeMers Yep Oct 27, 2018 at 14:04
  • 2
    For those interested, I have the full Craft 3 Module using the code above on GitHub at github.com/ademers/pendingusermodule Oct 28, 2018 at 23:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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