We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 32347
    • 143 Posts
    I have a website that I am trying to convert over to use the revolution. The website in question is a PHP website that is old. I am only interested in getting the user account information and adding it to the revolution website. Most of the fields are similar. Does anyone for see any issues to map the need fields to the new revolution fields mode users table? One concern is how to handle the passwords? This is the first time I have done anything on the db side. Can I add fields to the revolution database? any help and discussion would help.

    Thanks
      • 3749
      • 24,544 Posts
      This is tricky. Since the old site is not a MODX site, the user info is probably all in one table. In MODX, the ID, username, and password are in the modx_users table, but most of the other user information is in the modx_user_attributes (User Profile) table.

      The username will come across fine, but the password is very tricky unless it's in plain text (which it actually is in some old PHP sites). If not, what's usually stored is not the password, but a one-way hash of the password (and maybe a salt). If there's no salt, and you can determine the hashing method, and it's one of the methods MODX uses (e.g., MD5), you may be able import the modx_users part of the data if you set the hashing method. It may or may not work.

      If the passwords are in plain text, you can create the new users in MODX by reading an exported CSV file, and creating a new user with the MODX security/user/create processor (using $modx->runProcessor). You'd have to get the user object, and the user profile object, then set all the fields from the data in the CSV file using:

      $user->set('fieldname', $value);
      $profile->set('fieldname', $value);
      // etc.
      $user->save();
      $profile->save();
      


      It's complicated code because it has to check each field and know where it goes.
      FYI, the email address is required for the user profile.

      Obviously, this is a lot of work and can easily go wrong. If there aren't a lot of users, it might be easier to do it by hand.

      I should also mention the importX extra, which might just work for you.

      And I should mention that you can create a plugin using the login code from the old site that lets users log in with their original passwords, then converts them to MODX-style (more secure) passwords, so the code only needs to run once for each user. There's a series of articles on it here: http://bobsguides.com/blog.html/2016/04/06/bypassing-the-modx-manager-login-i/.
        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
        • 32347
        • 143 Posts
        Hello Bob,

        thanks for the response. I was hoping to write a query that could import the different information. I have 1030 users. Do all users, backend and front end reside in the same tables?

        The website is going to have different tiered users that get access to different things, any advice on how to best use Modx login on this.

        As far as the password could I set a password then have them change it when they log into the account? Sending them an email stating the username and password, but require change at login? Something to that effect.

        Also, I am enjoying the information in your book, so if there is something in it that can help me with this project, please by all means reference it?
          • 32347
          • 143 Posts
          There is salt and passwords are not in plain text.

          I have never converted a full php site to modx before. The whole website is built around users accounts, so any wisdom would be very helpful.

          Thanks
            • 3749
            • 24,544 Posts
            Do all users, backend and front end reside in the same tables?

            Yes.

            The website is going to have different tiered users that get access to different things, any advice on how to best use Modx login on this.

            The login process just logs you in. The access control is done with the Security Permissions system. I'm afraid it's not very intuitive, but if all you need is to limit users in particular User Groups to particular groups of resources (front-end pages), it's not quite as bad. There's some information on a bunch of different pages here: http://bobsguides.com/revolution-permissions.html.

            There is also this 50-minute video of me explaining how it works.

            If you want to limit access to files, you use Media Sources.

            As far as the password could I set a password then have them change it when they log into the account? Sending them an email stating the username and password, but require change at login? Something to that effect.

            Yes, as long as all the usernames are unique that could work. Actually forcing them to create a new password would be a little tricky and would take some custom code, but it's definitely doable if you have the PHP skills.

            There is salt and passwords are not in plain text.

            As I mentioned, if you can find the login code at the old site, you could put it in a MODX plugin at the new site and use it to both log the user in, and update their password and salt to MODX-style without the user even being aware of it. That way they could continue to use their old username and password.

            I've done this for a client, so it definitely works. There's description of what the plugin looks like in the Blog article I linked to above.

            Thanks for buying my book. There's some information in there about how user information is stored, but not much that speaks to your specific problem.

            There's a field list for the user object here: http://bobsguides.com/modx-object-quick-reference.html#modUser.

            And one for the User Profile here: http://bobsguides.com/modx-object-quick-reference.html#modUserProfile.

            *************

            The basic process I used for importing the users is to create translation arrays, one for the user object and one for the profile object. The key is the name of the field at the old site and the value is the name of the field at the new site.

            The user import snippet code looks something like this (untested and off the top of my head):


            $output = '';
            $userFields = array(
               'oldField1' => 'newField1',
               'oldField2' => 'newField2',
               // etc.
            }
            
            $profileFields = array(
               'oldField1' => 'newField1',
               'oldField2' => 'newField2',
               // etc.
            }
            
            while (line from CSV is read) {
                $userData = array of oldFieldname/value pairs holding data for one user created from CSV line
                    $output = '<br />Processing user: ' . $userData['username'];
                foreach ($userData as $fieldName => $value) {
            
                    $user = $modx->newObject('modUser');
                    $userProfile = $this->modx->newObject('modUserProfile');
                    $user->addOne($userProfile, 'Profile');
            
                    if (in_array($fieldName, $userFields)) {
            
                        /* Password and salt fields need special handling, otherwise, MODX will 
                           hash the hash and create a new salt */
            
                        if ($field == 'password') {
                            $user->_setRaw($userFields[$fieldName], $value);
                        } elseif ($field == 'salt') {
                            $user->_setRaw($userFields[$fieldName], $value);                
                        } else {
                            $user->set($userFields[$fieldName], $value);
                            $output .= '</br> --- Set new field ' . $userFields[$fieldName] . 'to . $value;
                        }
            
                    } elseif (in_array($fieldName, $profileFields))  {
                        $profile->set($profileFields[$fieldName], $value);
                        $output .= '</br> --- Set new field ' . $profileFields[$fieldName] . 'to . $value;
                    } else {
                       $output .= '</br> --- Unused field (old) ' . $fieldName . ' -- value = ' . $value;
                    }
            
                }
                /* Uncomment these after the output looks correct */
                // $user->save();
                // $userProfile->save();
            
            }
            
            return $output;
              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