5

How can I assign a user group to a user when it's created through the front end?

EDIT: So a few more details. I see there are some great answers if you want to put all users in one group, but unfortunately that's not the case. I have different forms for different groups. Ideally I would like to have checkboxes on the front end you I can choose which group they are assigned to.

7

Under Settings > Users > Settings Tab there is an option to allow public registration and Default User Group with a drop down that lets you select a user group.

If you need more flexibility, you could also write a simple plugin with an init function defined in the primary plugin class that listens to the saveUser event, and assigns a user group based on some criteria that you define — perhaps test the value of a custom user field on users (that is a hidden field on the registration form).

*UPDATED to assign group based on checkbox field called 'groupPreference':

namespace Craft;
class BusinessLogicPlugin extends BasePlugin {
  public function getName() {
     return 'Business Logic';
  }
  public function getVersion() {
     return '0.1';
  }
  public function getDeveloper() {
     return craft()->getSiteName();
  }
  public function getDeveloperUrl() {
     return 'http://mysite.com';
  }
  public function init() {
     parent::init();

     craft()->on('users.saveUser', function(Event $event) {

        // only fire if new user
        if ($event->params['isNewUser']) {

           // retrieve the userModel from the event
           $user = $event->params['user'];
           // retrieve the groupPreference field content
           $groupPreference = $user->getContent()->groupPreference;
           // create an empty array to store group ids
           $groups = array();

           // test for values in groupPreference array
           if (in_array("group1", $groupPreference)) {
              $groups[] = 1;
           }
           if (in_array("group2", $groupPreference)) {
              $groups[] = 2;
           }
           if (in_array("group3", $groupPreference)) {
              $groups[] = 3;
           }
           // assign the user to the groups
           $_POST['groups'] = $groups;
        }         
     });
  }
}

Haven't tested, but should be pretty close. Please comment if you see an error.

Keep in mind this event will fire, whenever this field is defined, even when saving a user through the CP. So you would have to delete the value before reassigning the user to a different group, or they will just get reassigned.

| improve this answer | |
  • I think i might be able to figure something out from this. Have just never dug into plugin development before. Was kinda hoping for a front-end solution like with any other field in an entry form. – paralyzah Oct 5 '14 at 21:19
  • That's pretty much the whole plugin. Would just need to put it in a folder called businesslogic and drop it in the craft/plugins folder. And then create a field called myHiddenField (or whatever you want) in your user custom fields. May need to tweek the if ($testCriteria) {...} statement to get it to work exactly like you want; and add the php wrapper. FYI, the name businesslogicplugin is actually a real plugin created by doublesecretagency as a template for creating custom plugins for projects. – Douglas McDonald Oct 5 '14 at 21:52
  • 1
    I imagine that they don't allow the arbitrary assigning of groups from a front-end form because it could present a security risk. – Douglas McDonald Oct 5 '14 at 22:00
  • I just read your edit: if you make the 'myHiddenField' a dropdown both in the CMS and in the form, you could then test for the value of that field in the init function above and assign the group based on the value. I would just be careful not to make the field values 1-to-1 with the groupids, so that someone couldn't make themselves an admin (if they manipulated the post data). – Douglas McDonald Oct 5 '14 at 22:08
  • I updated the script to test for a custom checkbox field called groupPreference. Not tested. – Douglas McDonald Oct 5 '14 at 22:45
3

To have a new user automatically assigned to a user group, there are two easy ways to do it.


(1) If the user registers through a front-end form:

You can pre-set their user group by going to Settings > Users > Settings, and selecting the "Allow public registration?" checkbox. This will reveal a dropdown menu, from which you can select their destination user group...

enter image description here

Any users who sign up after that point will be automatically placed into that user group.


(2) If the user is registered through the control panel:

This was an issue I encountered on a few different sites, so I wrote a plugin to easily manage it...

Auto-Assign User Group

It's very easy to use, here's a screenshot:

enter image description here

Any users who are created after that point will be automatically placed into whatever user group(s) you specified.

| improve this answer | |

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.