Drupal 6用户密码导入Drupal 7

时间:2011-06-01 17:57:36

标签: drupal import passwords md5

除了用户之外,我真的不需要将任何数据导入到我的D7版本中。我(通过SQL)导入了我的用户数据,但是D7密码加密方法现在不同了。

我不是任何想象力的专家,我从未使用过Drush,但我遇到过这个user_update_7000代码片段,发现user.install(http://api.drupal.org/api/drupal/modules--user--user.install/function/user_update_7000/7

<?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;
}
?>

我可以在哪里运行此脚本以更新数据库中的密码字段?

谢谢,

史蒂夫

3 个答案:

答案 0 :(得分:8)

我认为你可以创建一个名为rehash.php的页面(在你的root中,与update.php相同)。然后,先以管理员身份登录,然后再浏览此页面。请参阅下面的代码(大多数摘自最新的drupal 7安装中的user_update_7200)...

更糟糕的情况是,您可以创建一个简单的自定义模块并将此代码放在那里。

请注意,您应该先备份

<?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();
      }
    }
?>

答案 1 :(得分:0)

这个答案很完美。我用它来从Drupal 5站点更新。我做了一些改变以适应我的目的:

  1. 我没有限制更新的密码数量。我希望所有这些都更新,我正在更新的系统有超过1,000个用户。

  2. 我添加了一项检查,以确保我没有两次更新密码。这样,如果它超时(就像它对我来说)修改所有密码,我可以重新运行rehash.php来完成转换。但请注意,一旦用户登录,重新散列密码时将删除前导“U”。

    if (substr($account->pass, 0, 1) == 'U')
    {
        continue;
    }
    

答案 2 :(得分:0)

我没有足够的积分来添加评论,但我已经对hross'回答做了一些改进(并提交了草稿更新)。

这是一个带有文档的改进脚本,能够为那些进行手动Drupal 6到7合并的用户指定非默认用户表。它还包含了jpb的检查。

<?php
  /**
   * Use this script to update Drupal 6 users password hashes to Drupal 7 specs.
   * 
   * Ensure you BACKUP YOUR USERS TABLE before using this script! If not your whole site! 
   * Name this file update_users.php and place in your Drupal root, same place as update.php
   *
   * - If you've manually inserted a new table into your database, change the $databasename below.
   * - If this does not run, ensure you are logged into your site as admin.
   * - If this does not run, check your drupal watchdog and/or PHP logs
   * - If you see this error "PDOException: SQLSTATE[22001]: String data, right truncated: 1406 Data too long for column 'pass' at row 1:"
   *   you need to update your table's structure so that pass is a varchar(128).
   *
   * BACKUP, THIS MAY BREAK YOUR SITE AND EAT YOUR DATA!
   */

  echo "Starting. \r\n";

  // Change this if you've made a custom table
  $databasename = "users";

  // Update this many users
  $count = 1000;

  // 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;

  $result = db_query_range("SELECT uid, pass FROM {" . $databasename . "} WHERE uid > 10 ORDER BY uid", 0, $count);
  foreach ($result as $account) {
    $has_rows = TRUE;
    if (substr($account->pass, 0, 1) != 'U') {
      echo "updating account: " . $account->uid . " \r\n";
      $new_hash = user_hash_password($account->pass, $hash_count_log2);
      if ($new_hash) {
        // Indicate an updated password.
        $new_hash  = 'U' . $new_hash;
        db_update($databasename)
          ->fields(array('pass' => $new_hash))
          ->condition('uid', $account->uid)
          ->execute();
      }
    }
  }
  echo "Done.";
?>