Hi all,

I don't really need to import any data into my D7 build other than users. I have (by SQL) imported my user data however, the D7 password encryption method is now different.

I'm not an expert by any stretch of the imagination and I've never used Drush, but I have come across this user_update_7000 code snippet found user.install (http://api.drupal.org/api/drupal/modules--user--user.install/function/us...)

<?php
require_once DRUPAL_ROOT . '/' . variable_get('password_inc', 'includes/password.inc');
$old_hash = md5('password');
$hash_count_log2 = 11;

$new_hash = user_hash_password($old_hash, $hash_count_log2);

if ($new_hash) {
  // Indicate an updated password.
  $new_hash  = 'U' . $new_hash;
}
?>

Where could I run this script in order to update the password field in my DB?

Thanks,

Steve

Comments

Anonymous’s picture

Create a page named something like rehash.php (in your root, same place as update.php) and copy in code below. Log in to Drupal site as administrator, browse to rehash.php.

<?php
    // bootstrap stuff
    define('DRUPAL_ROOT', getcwd());

    include_once DRUPAL_ROOT . '/includes/bootstrap.inc';
    drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

    require_once DRUPAL_ROOT . '/' . variable_get('password_inc', 'includes/password.inc');

    // Lower than DRUPAL_HASH_COUNT to make the update run at a reasonable speed.
    $hash_count_log2 = 11;

    //  Hash again all current hashed passwords.
    $has_rows = FALSE;

    // Update this many users
    $count = 1000;

    $result = db_query_range("SELECT uid, pass FROM {users} WHERE uid > 1 ORDER BY uid", 0, $count);
    foreach ($result as $account) {
      $has_rows = TRUE;
      $new_hash = user_hash_password($account->pass, $hash_count_log2);
      if ($new_hash) {
        // Indicate an updated password.
        $new_hash  = 'U' . $new_hash;
        db_update('users')
          ->fields(array('pass' => $new_hash))
          ->condition('uid', $account->uid)
          ->execute();
      }
    }
?>

Thanks to hross over @ stackoverflow (http://stackoverflow.com/questions/6205605/drupal-6-user-password-import...)

tahiticlic’s picture

Well is this script still working with last Drupal version?

I can"t make it work... stored hash is wrong.

[Edit] : printing the new_hash variable at the end of user_hash_password function shows a right hash, corresponding to the password. BUT, the hash stored is not the same... Is there a trigger in the database that can encrypt it again when saving???

Drupal rocks!
www.tahiticlic.com

tahiticlic’s picture

Well it seems to work on another install, I can't get why this didn"t work the first time.

Drupal rocks!
www.tahiticlic.com

michael_lessard_micles.biz’s picture

Just to say your (swilsondesign) "module" or rehash.php script worked great !

To be safe, I first copied the D6 table users over to the D7 table, renaming the table: old_users
( Done using the phpMyAdmin, with the Operations tab at the top of the D7 table.)

With your script, I can easily both (choice) convert the D6 table to a D7 compatible format* OR insert the old passwords (MD5) in the D7 users table with the new encryption (SHA1).

* Important note for Drupal webmasters out there :
If you use the method of converting the D6 passwords first (i.e. in the D6 database or in an imported old_users table), you must modify the pass field first: from the original varchar 32 to varchar 128. Otherwise the rehash.php will tell you that the destination field is too short to accept the new passwords.

Easier method : mind you, it is much simpler to just first import the pass field directly in the D7 users table and then run the rehash.php script.

Michael Lessard
webmaster of Quebec City "democracy in action" media

chhavik’s picture

Hi,

I am migrating users from d6 to d7 using migrate module. I used the above specified logic or even tried adding $this->destination = new MigrateDestinationUser(array('md5_passwords' => TRUE)); which automatically saves the password with a 'U' appended.
http://drupal.org/node/1349758

But when i try to login using my old password, it says. Sorry, unrecognized username or password. Have you forgotten your password?

Anonymous’s picture

I didn't use any modules to migrate users. All i did was match db rows using a SQL import. When all the users are imported, run this script.

S

thatjustin’s picture

Why would having a U in front of the hash make this work? Does Drupal see that and interpret the hash differently?

Anonymous’s picture