Answers.Body: <blockquote>
    <p>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'.</p>
  </blockquote>
  
  <p>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).</p>
  
  <p>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".</p>
  
  <p>Having said that, here's a Craft 2 plugin that probably does what you're looking for. <a href="https://github.com/lindseydiloreto/craft-autoassignusergroup" rel="nofollow noreferrer">https://github.com/lindseydiloreto/craft-autoassignusergroup</a></p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-08-24T15:34:36.303Z
Answers.Text: 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.
Answers.UserDisplayName: Jordan D
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-08-24T22:53:39.373Z
Answers.Text: You can fork the plugin and do whatever custom logic you want in it.
Answers.UserDisplayName: Brad Bell
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-08-30T10:57:32.980Z
Answers.Text: 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?
Answers.UserDisplayName: Jordan D
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-08-24T04:12:40.257Z
Answers.LastActivityDate: 2018-08-24T04:12:40.257Z
Answers.UserDisplayName: Brad Bell
Body: <p>I have a plugin that sets the user group based on whether a hidden checkbox has been ticked on the sign up form.</p>
  
  <p>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.</p>
  
  <p>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?</p>
  
  <p>Plugin code below;</p>
  
  <pre><code>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);
  
          }
  
        }
  
      });
  
  }
  </code></pre>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-08-16T22:22:13.130Z
Comments.Text: 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?
Comments.UserDisplayName: Brad Bell
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-08-21T16:14:02.783Z
Comments.Text: 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'.
Comments.UserDisplayName: Jordan D
Tags: <plugin-development><users><user-groups>
Title: Plugin stops working when logged out
ThreadText: # Plugin stops working when logged out
  
  (Asked by: username_0 on 2018-08-16)
  
  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);
  
          }
  
        }
  
      });
  
  }
  
  ```
  
  **Tags:** plugin-development, users, user-groups
  
  > **Comments:**
  > **username_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?  
  > **username_0**: 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'.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2018-08-24. Score: 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>
  
  > **Comments:**
  > **username_0**: 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.  
  > **username_1**: You can fork the plugin and do whatever custom logic you want in it.  
  > **username_0**: 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?  
  
  ---
  
  