<p>Yeah you’re correct that <a href="https://github.com/craftcms/cms/commit/db089251994329d0fbe082c0d3ff234a01d82c98" rel="nofollow noreferrer"><code>db089251</code></a> 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.</p>

<p>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.</p>

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

<pre class="lang-php prettyprint-override"><code>Event::on(
    Users::class,
    Users::EVENT_BEFORE_ACTIVATE_USER,
    function (UserEvent $event) {
        if (Craft::$app-&gt;request-&gt;getIsSiteRequest()) {
            $event-&gt;isValid = false;
        }
    }
);
</code></pre>

CC BY-SA 4.0
2018-10-27T00:28:59.067Z
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.
Andrea DeMers
CC BY-SA 4.0
2018-10-27T11:44:53.197Z
Just so I understand: did my original code only work because of the bug?
Andrea DeMers
CC BY-SA 4.0
2018-10-27T14:04:29.367Z
@AndreaDeMers Yep
Brandon Kelly
CC BY-SA 4.0
2018-10-28T23:41:08.520Z
For those interested, I have the full Craft 3 Module using the code above on GitHub at https://github.com/ademers/pendingusermodule
Andrea DeMers
CC BY-SA 4.0
2018-10-26T23:18:31.247Z
2018-10-26T23:18:31.247Z
Brandon Kelly
<p>I have the following code in my site module:</p>

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

<p>It was working as expected until I updated from Craft 3.0.25 to 3.0.27.1</p>

<p>I think this issue is related to a change in the <code>src/controllers/UsersController.php</code> file as per GitHub <a href="https://github.com/craftcms/cms/commit/db089251994329d0fbe082c0d3ff234a01d82c98" rel="nofollow noreferrer">https://github.com/craftcms/cms/commit/db089251994329d0fbe082c0d3ff234a01d82c98</a></p>

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

<p>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?</p>

<p>I tried this:</p>

<pre><code>Event::on(
    Users::class,
    Users::EVENT_BEFORE_ACTIVATE_USER,
    function (UserEvent $event) {
        $event-&gt;isValid = false;
    }
);
</code></pre>

<p>but got <code>There was a problem activating the user.</code> when trying to activate the user account.</p>

<p>P.S: <code>Verify email addresses?</code> is disabled in <code>User Settings</code>.</p>

CC BY-SA 4.0
2018-10-26T19:46:57.370Z
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
Robin Schambach
<craft3><users><modules>
How can I set new user accounts to pending status in a Craft 3 module?
# How can I set new user accounts to pending status in a Craft 3 module?

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

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.

**Tags:** craft3, users, modules

> **Comments:**
> **username_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  

---

## 1 answers

### Answer by username_2 on 2018-10-26. Score: 3 | Accepted answer

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;
        }
    }
);

> **Comments:**
> **username_0**: 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.  
> **username_0**: Just so I understand: did my original code only work because of the bug?  
> **username_2**: @AndreaDeMers Yep  
> **username_0**: For those interested, I have the full Craft 3 Module using the code above on GitHub at https://github.com/ademers/pendingusermodule  

---

