We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 19328
    • 433 Posts
    Hi there,

    I have two buttons on my dashboard to create two types of MODX users (let's say "new school" and "new student"). Every school need to go in the school user group, and students in the student user group. I use Bob Ray's DefaultUserGroup plugin for this. But how can I tell the DefaultUserGroup plugin which group to assign to the new user? I need to set the plugin properties for this (dug_groups and dug_roles).

    Can I set these properties through the manager URL something like this?
    http://www.domain.com/manager/?a=security/user/create&dug_group=school&dug_roles=school


    Or am I thinking wrong and are there smarter ways to do this?

    This question has been answered by michelle84. See the first response.

      • 3749
      • 24,544 Posts
      How are the users being created (IOW, what do your buttons actually do)?

      The plugin won't help you if there's no way for it to tell which kind of user it's setting the group for. If there's something in the $_POST that would indicate which kind of user it is, you could rewrite the plugin to act appropriately.
        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
      • discuss.answer
        • 19328
        • 433 Posts
        Thanks for helping me in the right direction. Users are added using dashboard buttons with "manager/?a=security/user/create" which leads to the regular new user form. I have two buttons; one to add a school, and one to add a company.

        I've managed to create a function with which I'm able to set the desired user group automatically based on the variable "type" which can be set in the URL. (&type=school)

        * User field 'state' is going to be used for this function, so this field cannot be used for anything else *

        Create a dashboard button with this URL:
        manager/?a=security/user/create&type=school


        Create a plugin newUser and attach onUserFormPrerender onUserFormRender, and add this code

        $modx->regClientStartupScript('/manager/defaults-for-user.js');


        Create the file defaults-for-user.js with this code: (borrowed from http://forums.modx.com/index.php?action=thread&thread=65358&i=1)

        function changeForm()
        {
            var hide = 'none';
        
            // Get field 'state'
            var myType = document.getElementById('modx-user-state');
            
            // check if there is 'type' set in the URL. If so, put this value in field 'state'
            var matches = /type=([^&#=]*)/.exec(window.location.search);
            if (matches != null) {
                var type = matches[1];
                myType.value = type;
            }
            
            // hide field 'state'
            myType.parentNode.parentNode.style.display = hide;
        }
         
        // delays call so that the form loads
        setTimeout("changeForm()", 1500);
        


        create plugin newUserActions and attach to onUserFormSave and put in this code:

        <?php
        if($mode != modSystemEvent::MODE_NEW) { return; } 
        
        /* variables */
        $user = $modx->getObject('modUser',$id);
        $userProfile = $user->getOne('Profile');
        $type = $userProfile->get('state');
        
        switch ($type) {
        
        	case 'school':
        		// set usergroup to school
        		$userGroupId = 3; 
        		$user->joinGroup($userGroupId, 1); //role Member (id = 1)
        		break;
        
        	case 'company':
        		// set usergroup to company
        		$userGroupId = 4; 
        		$user->joinGroup($userGroupId, 1); //role Member (id = 1)
        		break;
        }
        
        return;
        


        There might be some code incorrect of unnecessary, because there are many more actions occurring in my plugins and javascripts. I've just extracted the parts which set the usergroup.
          • 3749
          • 24,544 Posts
          I'm glad you got it sorted. smiley
            Did I help you? Buy me a beer
            Get my Book: MODX:The Official Guide
            MODX info for everyone: http://bobsguides.com/modx.html
            My MODX Extras
            Bob's Guides is now hosted at A2 MODX Hosting