We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 577
    • 132 Posts
    I need to be able to create a user from within the web context in a "user admin" area of the site. Overall I have the permissions set well enough to only allowed logged users of type "userAdmin" into the area in question but not able to create user. I could very simply be doing this all wrong and or not have the proper level of permission for my "user admin" to be able to do this. That said the script simply fails with no error logs.. any help or direction is appreciated. I have also added my system "Administrator" account to this same UG as a super user. This then allows my Administrator access to the area but still not user created so my bet is my code is wrong to start.

    This just dies and never echos.
    $user = $modx->getNewObject('modUser');
    $user->set('username','adamtester');
    $user->set('password','adam');
                if($user->save()){
                    echo 'success';die;
                }
                else{
                    echo 'failed';die;
                }
    
      "One of these days I will get around to my own website... Its only been about 12 years... maybe tomorrow smiley"
    • Did you mean
      $user = $modx->newObject('modUser');
      ?
        • 577
        • 132 Posts
        I need a day off ... thank you
          "One of these days I will get around to my own website... Its only been about 12 years... maybe tomorrow smiley"
          • 577
          • 132 Posts
          Yep that is all it was... I would now like to propose the getNewObject is an alias for newObject tongue J/K thanks again

          As simple a solution this is it made my night... after the bad news I got elsewhere this just made my day smiley
            "One of these days I will get around to my own website... Its only been about 12 years... maybe tomorrow smiley"
            • 577
            • 132 Posts
            This works but is there a better way???

            $user = $modx->newObject('modUser');
            $user->set('username','hello');
            $user->set('password','world');
            
            if($user->save()){
            
            $id = $user->get('id');
            $profile  = $modx->newObject('modUserProfile');
                            
            $profile->fromArray(array(
            'internalKey' => $id,
            'fullname' => 'Adam Smith',
            'address' => '12345',
            'zip' => '91355',
            'city' => 'Valencia',
            'state' => 'CA',
            'country' => 'USA'
            ));
                            
            $profile->save();
            
              "One of these days I will get around to my own website... Its only been about 12 years... maybe tomorrow smiley"
              • 3749
              • 24,544 Posts
              I think this would be a little faster (untested):

              <?php
              $user = $modx->newObject('modUser', array('username'=>'hello', 'password'=>'world'));
              
              $profile  = $modx->newObject('modUserProfile',               
              array(
              'internalKey' => $id,
              'fullname' => 'Adam Smith',
              'address' => '12345',
              'zip' => '91355',
              'city' => 'Valencia',
              'state' => 'CA',
              'country' => 'USA'
              ));
              
              $user->addOne($profile);
              
              if (! $user->save() ) {
                return "oops -- problem with save";
              }
              


              The profile will be saved automatically.
                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
              • Could also just put all the data in an array and abuse $modx->runProcessor. DOes all the permission checking too - so may run into some issues with too strict permissions for that tongue
                http://rtfm.modx.com/display/revolution20/Using+runProcessor

                  Mark Hamstra • Developer spending his days working on Premium Extras and a MODX Site Dashboard with the ability to remotely upgrade MODX and extras to make the MODX world a little better.

                  Tweet me @mark_hamstra, check my infrequent blog at markhamstra.com, my slightly more frequent ramblings at MODX.today or see code at Github.