1

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()->on('users.saveUser', function(Event $event) {

      $user = $event->params['user']

      if ($event->params['isNewUser']) {

        $groupPreference = $user->getContent()->groupPreference;

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

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

        }

      }

    });

}
2
  • 1
    So... you're logged out, but presumably another logged in user has saved a user and that user isn't being assigned to the trade group? But when you're logged in and you save a user, it happens?
    – Brad Bell
    Commented Aug 16, 2018 at 22:22
  • There's a sign up form on the front end that has a prechecked hidden checkbox with the name 'groupPreference' (actually the name is 'fields[groupPreference[]' because that's how you've got to write it apparently) and a value of trade. 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'.
    – Jordan D
    Commented Aug 21, 2018 at 16:14

1 Answer 1

1

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

3
  • Thanks for the explanation. Can you think of any way around it? I've already had a go with that plugin and it doesn't do what we want. We've got other sign-up forms on the site and we don't want users that sign up through that to be automatically assigned into the trade group. We want some that sign up through this form to be assigned to the trade group, and some that sign up through another form to be simple customers. This plugin doesn't let you do that as far as I can tell.
    – Jordan D
    Commented Aug 24, 2018 at 15:34
  • You can fork the plugin and do whatever custom logic you want in it.
    – Brad Bell
    Commented Aug 24, 2018 at 22:53
  • Thanks, I realise I could do that but I'd rather understand which bit of their plugin it is that gives them the correct permissions/negates the need for the correct permissions and apply that logic to my own plugin. I can see they use a diffferent method, modifying the $_POST['groups'] array, but I have tried this and that $_POST['groups'] variable didn't exist and I couldn't create it. Which part of their plugin would I need to apply to mine in order that I could set users to be included in groups other than customers?
    – Jordan D
    Commented Aug 30, 2018 at 10:57

Your Answer

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

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