We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 7690
    • 166 Posts
    I have a client that has over 300 employees.
    Now i am creating a protected employee subsite. But is there a way to insert via an excel sheet web users?

    Because entering 300 names, emails and usernames will take a long time.
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      Yes, I’m sure I’m not the only one to have written a csv-to-sql script to insert into the three tables used by MODx for user management. This would be for a csv file with the structure username - password - fullname - email.
      <?php
      $table = $modx->getFullTableName('web_users');
      $table2 = $modx->getFullTableName('web_user_attributes');
      $table3 = $modx->getFullTableName('web_groups');
      // open and read csv file
      $csvfile = 'path-to-csv-file';
      if (($handle = fopen($csvfile, "r")) !== FALSE) {
          while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) {
              // insert into web_users table and get ID
              $fields = array('username'=>$modx->db->escape($data[0])
                  'password'=>md5($modx->db->escape($data[1])));
              $modx->db->insert($fields,$table);
              $insertId = $modx->db->getInsertId();
              // insert fullname, dummy email and photo into web_users_attributes table
              $fields = array('internalKey'=>$insertId,
                  'fullname'=>$modx->db->escape($data[2]),
                  'email'=>$modx->db->escape($data[3]));
              $modx->db->insert($fields, $table2);
              // insert into web_groups table
              $fields = array('webgroup'=>3,'webuser'=>$insertId);
              $modx->db->insert($fields, $table3);
      fclose($handle);
      } else { echo "<h2>Unable to open $csvfile</h2>"; }
      ?>

      This does not consider duplicated usernames or emails, and you’ll need to know the ID of the web user group you want to put them into, but it works well if there are no other web users, and should be a good starting point for a more robust bulk import of web users if you need one.

        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
        • 7690
        • 166 Posts
        thnx, this will come in handy i hope.
        I will recieve the excel sheet tomorrow and i hope i can convert it to a csv.

        You just put this on a temp page and past the code in a snippet. Run it and delete the page again?


        1 problem. I don’t have a password for the employees. And it has to be generated and mailed to them.
        Is this possible?
          • 20349 ☆ A M B ☆
          • 453 Posts
          Hmm... I have a similar issue...but wuth 1200 names

          Can this be done but with this fileds (in MODx):
          Username, Password, E-mail address, Mobile phone number


          I will create the sheet myself if that helps wink

            BASE - Web Design Studio
            MODX Ambassador

            Website
            • 4385
            • 372 Posts
            I have used this module in the past, and it has worked for me.

            WebusersImport
            http://www.modxcms.com/forums/index.php?topic=6863
            http://modxcms.com/extras/package/498
              DropboxUploader -- Upload files to a Dropbox account.
              DIG -- Dynamic Image Generator
              gus -- Google URL Shortener
              makeQR -- Uses google chart api to make QR codes.
              MODxTweeter -- Update your twitter status on publish.
              • 7690
              • 166 Posts
              wow, it works really well.

              However it won’t automatically generate passwords. I need this because the client doesn’t deliver a sheet with passwords, due to security reasons.
                • 28042 ☆ A M B ☆
                • 24,524 Posts
                Here’s the code from the weblogin.common.inc.php file for generating a password, and for sending the login details to somebody:
                    function webLoginGeneratePassword($length = 10) {
                        $allowable_characters = "abcdefghjkmnpqrstuvxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789";
                        $ps_len = strlen($allowable_characters);
                        mt_srand((double)microtime()*1000000);
                        $pass = "";
                        for($i = 0; $i < $length; $i++) {
                            $pass .= $allowable_characters[mt_rand(0,$ps_len-1)];
                        }
                        return $pass;
                    }    
                
                    // Send new password to the user
                    function webLoginSendNewPassword($email,$uid,$pwd,$ufn){
                        global $modx, $site_url;
                        $mailto = $modx->config['mailto'];
                        $websignupemail_message = $modx->config['websignupemail_message'];    
                        $emailsubject = $modx->config['emailsubject'];
                        $emailsender = $modx->config['emailsender']; 
                        $site_name = $modx->config['site_name'];
                        $site_start = $modx->config['site_start'];
                        $message = sprintf($websignupemail_message, $uid, $pwd); // use old method
                        // replace placeholders
                        $message = str_replace("[+uid+]",$uid,$message);
                        $message = str_replace("[+pwd+]",$pwd,$message);
                        $message = str_replace("[+ufn+]",$ufn,$message);
                        $message = str_replace("[+sname+]",$site_name,$message);
                        $message = str_replace("[+semail+]",$emailsender,$message);
                        $message = str_replace("[+surl+]",$site_url,$message);
                        if (!ini_get('safe_mode')) $sent = mail($email, $emailsubject, $message, "From: ".$emailsender."\r\n"."X-Mailer: Content Manager - PHP/".phpversion(), "-f {$emailsender}");
                        else $sent = mail($email, $emailsubject, $message, "From: ".$emailsender."\r\n"."X-Mailer: Content Manager - PHP/".phpversion());
                        if (!$sent) webLoginAlert("Error while sending mail to $mailto",1);
                        return true;
                    }
                

                It should not be difficult to adapt this to a stand-alone script; or even to a module. Or, you could simply use the email address or name as the password, using the md5() function to make the hash to insert into the database. Sending the email is rather obvious, just collect the variables, build the message, and send the mail, using the phpmailer class to send via SMTP if your host doesn’t allow the PHP mail() function.
                  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
                  • 7690
                  • 166 Posts
                  yeah, i have combined and modified the module script.
                  Works perfect smiley

                  Maybe a nice function to build in the new release of WebLoginPE?
                    • 20349 ☆ A M B ☆
                    • 453 Posts
                    Quote from: DNA at Dec 08, 2009, 11:49 AM

                    yeah, i have combined and modified the module script.
                    Works perfect smiley

                    Can you maybe share it with rest of us smiley
                      BASE - Web Design Studio
                      MODX Ambassador

                      Website
                      • 20349 ☆ A M B ☆
                      • 453 Posts
                      Quote from: Mproject at Dec 17, 2009, 06:25 PM

                      Quote from: DNA at Dec 08, 2009, 11:49 AM

                      yeah, i have combined and modified the module script.
                      Works perfect smiley

                      Can you maybe share it with rest of us smiley
                      guess not?
                        BASE - Web Design Studio
                        MODX Ambassador

                        Website