We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 34193
    • 330 Posts
    OK using security/user/create to add a user, got most of it working trying to add the user to a group is confusing me I have tried 2 ways that work but with strange side effects.

    way 1
    $this->modx->runProcessor('security/user/create', array(
                            ..........
                            'groups' => json_encode(array('user_group'=>'Parents','role'=>'1')),
                            'newpassword' => TRUE,
                            'passwordnotifymethod' => 'e',
                            'passwordgenmethod' => 'g',
                            'class_key' => 'ParentExtUser',
                        ));
    


    This way adds the user to the Administrator group not the Parents.

    way 2
    $this->modx->runProcessor('security/user/create', array(
                            ..........
                            'groups' => json_encode(array('user_group'=>'2','role'=>'1')),
                            'newpassword' => TRUE,
                            'passwordnotifymethod' => 'e',
                            'passwordgenmethod' => 'g',
                            'class_key' => 'ParentExtUser',
                        ));
    


    This adds the user to the right group but also to the Administrator group.

    So is this a bug or am I doing something wrong and if it’s me how do I do it?
      • 22303 MODX Staff
      • 10,725 Posts
      In your second approach, make sure 2 is an integer, not a string (i.e. ’2’).
        • 34193
        • 330 Posts
        hmmm ok so I tried this
        $this->modx->runProcessor('security/user/create', array(
                                ..........
                                'groups' => json_encode(array('user_group'=>2,'role'=>'1')),
                                'newpassword' => TRUE,
                                'passwordnotifymethod' => 'e',
                                'passwordgenmethod' => 'g',
                                'class_key' => 'ParentExtUser',
                            ));
        


        But now only adds it to the Administrator group again.

        I’m guessing the only way to do it by name is to carry out a search by name and use the resulting id.

        hmmm ok was just checking the tables in phpMyAdmin and it won’t let me view the rows it says I have 4 rows in membergroup_name but won’t let me open it wonder if this might be the issue.

        ok If I delete all my created user groups i can access the table but as soon as I add my groups I can’t.
          • 34193
          • 330 Posts
          OK sorted the DB issue but that still hasn’t solved the group issue.

          when using

          ’groups’ => json_encode(array(’user_group’=>2,’role’=>’1’)),

          this inserts the following records into the DB
          [table]
          [tr][td]id[/td][td]user_group[/td][td]member[/td][td]role[/td][/tr]
          [tr][td]11[/td][td]0[/td][td]31[/td][td]1[/td][/tr]
          [tr][td]12[/td][td]1[/td][td]31[/td][td]1[/td][/tr]
          [/table]

          Where as using ’groups’ => json_encode(array(’user_group’=>’2’,’role’=>’1’)),
          [table]
          [tr][td]id[/td][td]user_group[/td][td]member[/td][td]role[/td][/tr]
          [tr][td]13[/td][td]2[/td][td]32[/td][td]2[/td][/tr]
          [tr][td]14[/td][td]1[/td][td]32[/td][td]1[/td][/tr]
          [/table]

          so an int only adds it to the admin group and a group 0 which does not exist. where as a string ’2’ adds it the right group and the admin group but also adds the wrong role.

          What I want to achieve is to just add it to my group without the admin, and with the right role.
            • 34193
            • 330 Posts
            Bump any help greatfully received smiley
              • 22303 MODX Staff
              • 10,725 Posts
              Why are you json_encoding the data? I don’t believe you want to do that...
                • 34193
                • 330 Posts
                OK tried not json_encodeing the data
                'groups' =>array('user_group'=>2,'role'=>'1')
                and that won’t add to any group. EDIT just realised i’ve got user_group but it expects usergroup, but still not working right.

                I got the idea to try the json_encode from this code in create, I have commented on how I think this is working

                /* create user group links */
                if (isset($scriptProperties['groups'])) {
                    $ugms = array();
                    $groups = $modx->fromJSON($scriptProperties['groups']); /*fromJSON I am assuming is expecting data in json format to then do somthing with it, ie get something from json*/
                    foreach ($groups as $group) {
                        $ugm = $modx->newObject('modUserGroupMember');
                        $ugm->set('user_group',$group['usergroup']);
                        $ugm->set('role',$group['role']);
                        $ugms[] = $ugm;
                    }
                    $user->addMany($ugms,'UserGroupMembers');
                }
                


                  • 34193
                  • 330 Posts
                  OK solved this but it seems a little strange at first but I think I understand why. You need to create and array of arrays of pairs.

                  'groups' => json_encode(array(array('usergroup'=> '2' ,'role'=>'1'))),
                  


                  I guess this is so that you can then add a user to multiple groups using somthing like

                  'groups' => json_encode(array(array('usergroup'=> '2' ,'role'=>'1')),array('usergroup'=>'3', 'role'=>'1') ),
                  


                  This way adding the user to group 2 and 3.

                  Anyway as I said this seems to solve the problem thanks for your help just mentioning the json_encode made me look deeper as to why I had, and that solved the issue.