We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
  • Is there a service for creating new users, so one uses getService() instead of newObject()?

    There is also runProcessor(). Somewhat confused, which one is best to use when creating a new user?

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

    [ed. note: sottwell last edited this post 9 years, 2 months ago.]
      Studying MODX in the desert - http://sottwell.com
      Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
      Join the Slack Community - http://modx.org
    • I can use getService('user', MODX_CORE_PATH . 'model/modx/moduser.modUser'); but I suspect that it only refers to the current user. Which of course MODX already does when processsing a request.
        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
      • discuss.answer
        • 4172
        • 5,888 Posts
        I think the best is to use runProcessor.
        For example a little script, which does create parallel users to resources:

        <?php
        
        //$parent = '75';
        $parent = '39';
        $c = $modx->newQuery('modResource');
        $c->where(array('parent' => $parent));
        
        if ($collection = $modx->getCollection('modResource', $c)) {
            foreach ($collection as $resource) {
                $data = array();
                $data['username'] = $resource->getTVValue('user_name');
                $data['email'] = $resource->getTVValue('arzt-email');
                $data['fullname'] = $resource->get('pagetitle');
                $data['passwordnotifymethod'] = 'e';
                $data['active'] = '1';
                $data['userpage_id'] = $resource->get('id');
                $data['mailTpl'] = 'newUserMail';
                $group = array();
                $group['usergroup'] = '3';
                $group['role'] = '5';
                $data['groups'] = array();
                $data['groups'][] = $group;
        
                //echo $data['fullname'];
        
                if ($user = $modx->getObject('modUser', array('username' => $data['username']))) {
                    //user exists allready
                } else {
                    $response = $modx->runProcessor('security/user/create_custom', $data);
        
                    if ($response->isError()) {
                        //$updateerror = true;
                        //echo $errormsg = $response->getMessage();
                        //echo 'error';
                    }
                    if ($object = $response->getObject()) {
                        $userid = $modx->getOption('id', $object, '');
                        $resource->setTVValue('user_id', $userid);
                    }
                }
            }
        }
        


        Or anothor one, which handles special extended fields from a CMP:
        https://github.com/Bruno17/migxclientmanager/blob/master/core/components/migxclientmanager/processors/mgr/default/update.php

          -------------------------------

          you can buy me a beer, if you like MIGX

          http://webcmsolutions.de/migx.html

          Thanks!
        • Thank you. I had gotten runProcessor and getService confused.
            Studying MODX in the desert - http://sottwell.com
            Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
            Join the Slack Community - http://modx.org
            • 4172
            • 5,888 Posts
            create_custom was a special create-processor, which handles the
            usermail - tpl
            $data['mailTpl'] = 'newUserMail';


            you would use
            $modx->runProcessor('security/user/create', $data);
              -------------------------------

              you can buy me a beer, if you like MIGX

              http://webcmsolutions.de/migx.html

              Thanks!