We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 50573
    • 4 Posts
    I have a client requirement to be able to add users programmatically from both a front-end screen and in bulk from a csv (or similar) file.
    I have identified the tables that are altered when I add a user.
    I am struggling with how the 'salt' is generated and used.
    I know the salt is used to improve security, but am unclear on what value I should set for it in the new user entry, and what I should base it on so that these users work just as one's added from the backend interface.

    Any help would be appreciated.

    Thanks in advance.

    P

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

    • discuss.answer
      • 4172
      • 5,888 Posts
      you can try to use the login-package to add users from frontend.

      here's some working example-code, which imports users from CSV-files

      <?php
      
      $default_email = '[email protected]';
      $uploadpath = $modx->getOption('base_path') . 'userimport/';
      
      /**
       * Add User Group memberships to the User
       * @return array
       */
      function setUserGroups($groups, &$object)
      {
          global $modx;
          $memberships = array();
      
          if ($groups !== null) {
              $groups = is_array($groups) ? $groups : $modx->fromJSON($groups);
              $groupsAdded = array();
              $idx = 0;
              foreach ($groups as $group) {
                  if (in_array($group['usergroup'], $groupsAdded))
                      continue;
      
                  /**
                   @var modUserGroupMember $membership */
                  $membership = $modx->newObject('modUserGroupMember');
                  $membership->set('user_group', $group['usergroup']);
                  $membership->set('role', $group['role']);
                  $membership->set('member', $object->get('id'));
                  $membership->set('rank', isset($group['rank']) ? $group['rank'] : $idx);
                  $membership->save();
                  $memberships[] = $membership;
                  $groupsAdded[] = $group['usergroup'];
                  $idx++;
              }
          }
          return $memberships;
      }
      
      /**
       * @return modUserProfile
       */
      function addProfile($properties, &$object)
      {
          global $modx;
          $profile = $modx->newObject('modUserProfile');
          $profile->fromArray($properties);
          //$profile->set('blocked', $this->getProperty('blocked', false));
          //$profile->set('photo', '');
          $object->addOne($profile, 'Profile');
          return $profile;
      }
      
      
      set_time_limit(1000);
      
      $dir = dir($uploadpath); // dir Objekt!
      $files = array();
      while ($file = $dir->Read()) {
          //dbg2( "UPLD:   Dir-Eintrag :" ,$child) ;
          $filename = strval($file);
          $p = strlen($filename);
          $ext = strtolower(substr($filename, $p - 4, 4)); //	check extension at end of name!
      
          if ($ext == '.txt') {
              $info['file'] = $filename;
              $info['type'] = $ext;
              $files[$filename] = $info;
          } //if
      } //wend
      $dir->close();
      
      ksort($files);
      
      foreach ($files as $filename => $file) {
          $idx = 1;
          if (($handle = fopen($uploadpath . $filename, "r")) !== false) {
              while (($data = fgetcsv($handle, 1000, ",")) !== false) {
                  $row = array();
                  if ($idx == 1) {
                      $fields = $data;
                  } else {
                      $num = count($data);
      
                      for ($c = 0; $c < $num; $c++) {
                          $field = $fields[$c];
                          $row[$field] = mb_convert_encoding($data[$c], "UTF-8");
                      }
                      if (!strstr($row['email'], '@')) {
                          $row['email'] = $default_email;
                      }
      
                      $data = array();
                      $data['active'] = true;
                      //$data['specifiedpassword'] = $row['password'];
                      //$data['confirmpassword'] = $row['password'];
                      //$data['passwordgenmethod'] = 'spec';
                      //$data['passwordnotifymethod'] = 's';
                      $data['username'] = $row['username'];
                      $data['fullname'] = $row['name'];
                      $data['email'] = $row['email'];
      
                      $groups = explode('*', $row['usertype']);
                      $usergroups = array();
                      foreach ($groups as $group) {
                          $groupname = trim($group);
      
                          if ($usergroup = $modx->getObject('modUserGroup', array('name' => $groupname))) {
      
                          } else {
                              $usergroup = $modx->newObject('modUserGroup');
                              $usergroup->set('name', $groupname);
                              $usergroup->set('dashboard', 1);
                              $usergroup->save();
                          }
      
                          $groupdata = array();
                          $groupdata['role'] = '1';
                          $groupdata['usergroup'] = $usergroup->get('id');
                          $usergroups[] = $groupdata;
                      }
                      
                      if ($object = $modx->getObject('modUser',array('username'=>$data['username']))){
                          
                      }else{
                          $object = $modx->newObject('modUser');
                          $object->fromArray($data);
                          addProfile($data,$object);
                          $object->set('password',$row['password']);   
                          $object->save();
                          setUserGroups($usergroups,$object);
                              
                      }               
                      
      
                      //$response = $modx->runProcessor('security/user/create', $data);
                  }
                  $idx++;
              }
              fclose($handle);
          }
      }
      
      return $modx->error->success();
      


      you could also use the runProcessor - method, instaed creating them by newObject diretly,
      which would generate the passwords and send a activation email.
        -------------------------------

        you can buy me a beer, if you like MIGX

        http://webcmsolutions.de/migx.html

        Thanks!
        • 50573
        • 4 Posts
        Slick!

        Thanks much.

        P