Plugin stops working when logged out I have a plugin that sets the user group based on whether a hidden checkbox has been ticked on the sign up form.

The plugin works exactly as I want it to while I'm logged in, but as soon as I log out the form reverts to default behaviour.

I can't find anything in Google about plugins misbehaving when someone logs out. Does anyone know what's going on and how I can overcome it?

Plugin code below;

public function init() {

    craft()-&gt;on('users.saveUser', function(Event $event) {

      $user = $event-&gt;params['user']

      if ($event-&gt;params['isNewUser']) {

        $groupPreference = $user-&gt;getContent()-&gt;groupPreference;

        if (in_array("trade", $groupPreference)) {

           craft()-&gt;userGroups-&gt;assignUserToGroups($user-&gt;id, 3);

        }

      }

    });

}
When I'm logged into the backend using my admin account and I use the form to sign up a new fake user, it works as desired and assigns the new user to the 'trade' group. If I'm logged out it just assigns the user to the default 'customers' group rather than 'trade'.


When you're logged in as an admin and test this for a new fake user, you have all of the necessary permission to perform the operation (including assigning new user/group permissions).

When you're not logged in as any user, you're missing the necessary "Assign user to groups" permissions a logged in user would need.  New users are going into the "customers" group, because presumably in Settings->Users->Settings in the Control Panel, you've got front-end registration enabled and you've assigned the default group to be "customers".

Having said that, here's a Craft 2 plugin that probably does what you're looking for. https://github.com/lindseydiloreto/craft-autoassignusergroup