如何将用户密码从Drupal 6迁移到Drupal 7?


20

我正在尝试将用户从Drupal 6迁移到Drupal 7站点。我的问题是如何将其密码从MD5更改为哈希值1(由D7使用)。
你有什么主意吗?

Answers:


11

要将md5密码更新为哈希密码,我需要使用user_hash_password()压缩一个“ U”。这是我用来使其工作的脚本。

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

然后我跑了

drush scr <name_of_the_script_file>

而且有效。


11作为迭代计数是标准的D6吗?
Sam152

7

有一个非常简单的答案:

<?php
  $this->destination = new MigrateDestinationUser(array('md5_passwords' => TRUE));
  ...
  $this->addFieldMapping('pass', 'source_password');
?>

参考:保留用户密码


5

如果有人需要一个独立的PHP脚本来将用户从Drupal 6迁移到Drupal 7,那么这里是:

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

    }
    ?>

您能否提及它的放置位置?
Rootical V. 2012

1
@Rootical V,只要您在脚本中放置了正确的路径和数据库凭据,就可以通过命令行从任何位置运行该脚本。
Ajinkya Kulkarni

2

好吧,如果您进行升级,那么输入密码就可以了。我想您可能可以查看升级代码以了解其操作方式。

但是,如果您只是在迁移用户,则最可能的方法可能只是向所有人发送一次性登录链接,然后让他们重置密码。


我只是注意到drupal.org/project/login_one_time与views_bulk_operations集成在一起,因此,如果您要使密码无效,这将是重新建立密码的好方法。
rfay 2011年

0

如果我从D7站点上的devel / php运行此程序,我发现我只需要:

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

}

两个站点都在同一台Web服务器上。


您选择手动更改密码。迁移模块也可以使用$this->destination = new MigrateDestinationUser(array('md5_passwords' => TRUE)); ... $this->addFieldMapping('pass', 'source_password');
Neograph734 2013年
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.