20

I am trying to migrate users from a Drupal 6 to a Drupal 7 site. My problem is how to change their password from MD5 to the hash one (used by D7).
Do you have any idea?

11

To update the md5 password to the hashed one I needed to use user_hash_password() and concact an 'U'. Here is the script that I used to make it work.

<?php
        require_once DRUPAL_ROOT . '/' . variable_get('password_inc', 'includes/password.inc');
        $res = db_query('select * from drupal.users');

        if($res) {
                foreach ($res as $result) {
                        $hashed_pass = user_hash_password($result->pass, 11);
                        if ($hashed_pass) {
                          $hashed_pass  = 'U' . $hashed_pass;
                          db_update('users')->fields(array('pass' => $hashed_pass))->condition('uid', $result->uid)->execute();
                        }
                }
        }

Then I ran

drush scr <name_of_the_script_file>

And it worked.

  • Is 11 as the iteration count a standard D6 thing? – Sam152 Feb 15 '13 at 1:33
7

There is a VERY simple answer to this:

<?php
  $this->destination = new MigrateDestinationUser(array('md5_passwords' => TRUE));
  ...
  $this->addFieldMapping('pass', 'source_password');
?>

Reference: Preserving user passwords

5

If somebody needs a standalone PHP script to migrate users from Drupal 6 to Drupal 7, then here it is:

  <?php
    /*
    Standalone PHP script to migrate users from Drupal 6 to Drupal 7 programatically.
    Date: 9-4-2012
    */

    // set HTTP_HOST or drupal will refuse to bootstrap
    $_SERVER['HTTP_HOST'] = 'example.org';
    $_SERVER['REMOTE_ADDR'] = '127.0.0.1';


    //root of Drupal 7 site
    $DRUPAL7_ROOT="/var/www/ace";
    define('DRUPAL_ROOT',$DRUPAL7_ROOT);
    chdir($DRUPAL7_ROOT);
    require_once "./includes/bootstrap.inc";
    drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
    require_once "./includes/password.inc";

    //connect to Drupal 6 database
    //syntax:mysqli(hostname,username,password,databasename);
    $db= new mysqli('localhost','ace6','ace6','ace6');
    if(mysqli_connect_errno())  {
        echo "Conection error. Could not connect to Drupal 6 site!";
        exit;
    }

    //get users from Drupal 6 database
    $query="select * from users";
    $result=$db->query($query);
    //count number of users
    $num_results=$result->num_rows;
    for($i=0;$i<$num_results;$i++){

        //fetch each row/user
        $row=$result->fetch_assoc();

        //migrate only active users
        if($row['status']==1){

            //convert password from Drupal 6 style to Drupal 7 style
            $hashed_pass='U'.user_hash_password($row['pass'],11);

            //check if user with same email address already exists in Drupal 7 database, if it does, do not migrate
            if (!user_load_by_mail($row['mail'])) {
                $account = new stdClass;
                $account->is_new = TRUE;
                $account->name = $row['name'];
                $account->pass = $hashed_pass;
                $account->mail = $row['mail'];
                $account->init = $row['mail'];
                $account->status = TRUE;
                $account->roles = array(DRUPAL_AUTHENTICATED_RID => TRUE);
                $account->timezone = variable_get('date_default_timezone', '');
                //create user in Drupal 7 site 
                user_save($account);
                //print message
                echo "User acount ".$row['name']." has been created\n";
            }
        }

    }
    ?>
  • Could you please mention where it should be placed? – Rootical V. Dec 27 '12 at 12:54
  • 1
    @Rootical V you can run this script from any location through command line given that you put correct path and database credentials in the script. – Ajinkya Kulkarni Jan 18 '13 at 16:24
2

Well, if you upgrade you come out with your passwords OK. I guess you could probably look at the upgrade code to see how they do that.

However, if you're just migrating users, probably the most likely approach would be just to send out one-time login links to everybody and get them to reset their passwords.

  • I just noticed that drupal.org/project/login_one_time integrates with views_bulk_operations, so if you are going to invalidate passwords, this would be a great way to re-establish them. – rfay Apr 16 '11 at 3:20
0

If I ran this from devel/php on the D7 site, I found I only needed:

require_once "./includes/password.inc";

//connect to Drupal 6 database
//syntax:mysqli(hostname,username,password,databasename);
$db= new mysqli('localhost','ace6','ace6','ace6');
if(mysqli_connect_errno())  {
    echo "Conection error. Could not connect to Drupal 6 site!";
    exit;
}

//get users from Drupal 6 database
$query="select * from users";
$result=$db->query($query);
//count number of users
$num_results=$result->num_rows;
for($i=0;$i<$num_results;$i++){

    //fetch each row/user
    $row=$result->fetch_assoc();

    //migrate only active users
    if($row['status']==1){

        //convert password from Drupal 6 style to Drupal 7 style
        $hashed_pass='U'.user_hash_password($row['pass'],11);

        //check if user with same email address already exists in Drupal 7 database, if it does, do not migrate
        if (!user_load_by_mail($row['mail'])) {
            $account = new stdClass;
            $account->is_new = TRUE;
            $account->name = $row['name'];
            $account->pass = $hashed_pass;
            $account->mail = $row['mail'];
            $account->init = $row['mail'];
            $account->status = TRUE;
            $account->roles = array(DRUPAL_AUTHENTICATED_RID => TRUE);
            $account->timezone = variable_get('date_default_timezone', '');
            //create user in Drupal 7 site 
            user_save($account);
            //print message
            echo "User acount ".$row['name']." has been created\n";
        }
    }

}

Both sites were on the same webserver.

  • You chose to manually alter the password. It can also be done by the migration module using $this->destination = new MigrateDestinationUser(array('md5_passwords' => TRUE)); ... $this->addFieldMapping('pass', 'source_password'); – Neograph734 Mar 12 '13 at 14:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.