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?
5 Answers
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.
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
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";
}
}
}
?>
-
-
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. Commented Jan 18, 2013 at 16:24
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.– rfayCommented Apr 16, 2011 at 3:20
-
This is a better answer, I think, as the fact that the passwords were ever stored as MD5s makes them less secure and in need of updating anyway.– mbomb007Commented Oct 11, 2022 at 20:15
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');
Commented Mar 12, 2013 at 14:36