2011-06-01 78 views
7

我並不真正需要的任何數據導入到我比其他用戶D7版本。我有(通過SQL)導入我的用戶數據,但是,D7密碼加密方法現在有所不同。Drupal的6個用戶密碼進口的Drupal 7

我不是由任何發揮想象力的專家,我從來沒有用過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; 
} 
?> 

我能在哪裏運行這個腳本爲了更新我的數據庫中的密碼字段?

感謝,

史蒂夫

回答

8

我想你可以創建一個名爲頁面類似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(); 
     } 
    } 
?> 
+1

完美工作,謝謝!我需要編輯的一件事就是第19行的SQL ...如果其他人使用此uid應該大於1以避免重新刷新管理員密碼。 – 2011-06-01 19:34:05

+1

好點=)。更新。 – hross 2011-06-01 19:56:26

+0

我覺得你的意思user_update_7000,不user_update_7200 – aaronbauman 2016-06-23 16:37:15

0

這個答案是完美的。我用它從Drupal 5網站進行更新。我做了一些修改以適應我的目的:

  1. 我沒有限制更新密碼的數量。我希望所有這些更新,我正在更新的系統有超過1,000個用戶。

  2. 我添加了一個檢查,以確保我沒有更新密碼兩次。這樣,如果超時(就像我這樣做)修改所有密碼,我可以重新運行rehash.php來完成轉換。但是,請注意,一旦用戶登錄,密碼重新散列時,將刪除前導「U」。

    if (substr($account->pass, 0, 1) == 'U') 
    { 
        continue; 
    } 
    
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."; 
?> 
+0

請更改變量$數據庫名稱至$表名。這有點令人困惑。 – 2013-05-18 08:58:24