rainlab/user-plugin

Event on user registration from back-office not available?

Closed this issue ยท 5 comments

Hi,

It seem that rainlab.user.register event doesn't work it this case (like auto activation). I'm trapped, I would expect a lot on this point...
In fact, it's the administrator to register all user accounts (app for a course session), not to users themselves.
To fully exploit the module, do I have to create an entire users manage interface in front-end for admin, or is there another way for this?

Thank you for your advice :)

Hi @fujitsuDev

The Vanilla theme contains a useful set of pages that allows user registration. Take a look here:
https://github.com/rainlab/vanilla-theme

You can also set the activation mode via Settings > User settings once the RainLab.User plugin is installed.

The plugin does fire this event upon registration via the front-end, but not the backend.

// This logic only executes from the CMS component (frontend)
//
Event::fire('rainlab.user.register', [$user, $data]);

If you wanted a more robust version of this event, you could tap into the model event. This will ensure the logic occurs everywhere, frontend and backend together.

// This logic will always execute
//
\RainLab\User\Models\User::extend(function($model) {
    $model->bindEvent('model.afterCreate', function() use ($model) {
        // ... do things ...
    });
});

I hope this makes sense. Thanks for using October CMS!

The Vanilla theme contains a useful set of pages that allows user registration. Take a look here:
https://github.com/rainlab/vanilla-theme

Thank you, I take look this way.

You can also set the activation mode via Settings > User settings once the RainLab.User plugin is installed.

This settings not supported when creating a user account from the back office, only from front-end too.

Thanks :)

We've made a note to allow this workflow in a future version. Thank you for your insight

If you wanted a more robust version of this event, you could tap into the model event. This will ensure the logic occurs everywhere, frontend and backend together.

// This logic will always execute
//
\RainLab\User\Models\User::extend(function($model) {
    $model->bindEvent('model.afterCreate', function() use ($model) {
        // ... do things ...
    });
});

Ref: https://octobercms.com/docs/plugin/extending#extending-user-model

Finally, I took this way and that works perfectly, from individual user add and with User import/export too!

From my plugin boot():

\RainLab\User\Models\User::extend(function($model) {
    $model->bindEvent('model.afterCreate', function() use ($model) {
        UserModel::where('id', $model->id)->update(['is_activated' => true]); // Force auto user activation
        // Others things to do...
        Mail::sendTo($model->email, 'rainlab.user::mail.welcome', ['name' => $model->name]);
    });
});

Very simple. For a moment I almost questioned the October power and its plugins! ๐Ÿฅ‡ :)
Thanks again.

Bravo! Well done