【问题标题】:Drupal 6 user password import to Drupal 7Drupal 6 用户密码导入 Drupal 7
【发布时间】:2011-09-06 12:43:13
【问题描述】:

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

无论如何我都不是专家,而且我从未使用过 Drush,但我遇到了这个 user_update_7000 代码 sn-p found 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;
}
?>

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

谢谢,

史蒂夫

【问题讨论】:

    标签: drupal import passwords md5


    【解决方案1】:

    我没有足够的积分来添加评论,但我对 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.";
    ?>
    

    【讨论】:

    • 请将变量 $databasename 更改为 $tablename。这有点令人困惑。
    【解决方案2】:

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

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

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

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

    【讨论】:

      【解决方案3】:

      我认为您可以创建一个名为 rehash.php 的页面(在您的根目录中,与 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();
            }
          }
      ?>
      

      【讨论】:

      • 完美运行,谢谢!我需要编辑的一件事是第 19 行的 SQL...如果其他人使用此 uid 应大于 1,以避免重新散列管理员密码。
      • 我认为您的意思是 user_update_7000,而不是 user_update_7200
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-04-10
      • 1970-01-01
      • 2012-08-22
      • 1970-01-01
      • 2023-03-23
      • 2011-10-03
      • 1970-01-01
      相关资源
      最近更新 更多