3

I have a site that allows public registration, and I want to be able to control where users are redirected to after activating their account. However the default user group has access to the Control Panel, so as explained in “activateAccountSuccessPath doesn't work as expected”, users are taken to the Control Panel login page after activation, rather than the path defined by the activateAccountSuccessPath config setting.

Is there any way to change that behavior, so even users with access to the Control Panel will get redirected to the activateAccountSuccessPath after account activation?

2

You can write a plugin that listens to the users.onActivateUser event, and manually redirects the browser to your desired page.

Just add this code to your plugin’s primary class:

public function init()
{
    craft()->on('users.activateUser', array($this, 'onActivateUser'));
}

public function onActivateUser(Event $event)
{
    /* @var UserModel $user */
    $user = $event->params['user'];

    // Are they in the default group?
    // (Indicates this was probably public registration)
    if ($user->isInGroup('myDefaultGroup'))
    {
        // Redirect them to the activateAccountSuccessPath
        $activateAccountSuccessPath = craft()->config->getLocalized('activateAccountSuccessPath');
        $url = UrlHelper::getSiteUrl($activateAccountSuccessPath);
        craft()->request->redirect($url);
    }
}
  • Great stuff Brandon, this is working well. Just a couple of additional questions: 1. is there anyway to get this working in conjunction with autoLoginAfterAccountActivation (I'm talking of a dashboard page which uses {% requireLogin %}). 2. Is there any technical or security reason why this isn't built in as a default config setting? – Rob Jan 28 '16 at 20:12
  • @Rob This should work with autoLoginAfterAccountActivation = true. This isn’t something we would make a config setting because it’s super edge case. The expected behavior is that a user with access to the Control Panel would be directed to the Control Panel when their account has been activated. – Brandon Kelly Jan 28 '16 at 20:21
  • Fair enough. Unfortunately, it doesn't work with that setting and just redirects to loginPath — it's far better than our previous solution however, and (I've updated my post with a full config setup for posterity) – Rob Jan 28 '16 at 20:39

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.