首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >迁移用户密码散列

迁移用户密码散列
EN

Drupal用户
提问于 2020-01-24 07:11:03
回答 2查看 829关注 0票数 0

我们正在将我们的旧站点从普通PHP迁移到Drupal 8,密码在旧站点中使用MD5进行散列,我需要在Drupal中迁移它们。

我尝试了以下代码,但它接受密码作为纯文本,并计算其SHA哈希。

代码语言:javascript
运行
复制
public function prepareRow(Row $row) {
    $password = $row->getSourceProperty('user_password');
    $hash = \Drupal::service('password')->hash($password);
    $row->setSourceProperty('user_password', $hash);
  }

这是配置文件。

代码语言:javascript
运行
复制
id: users
label: User migration
migration_group: mymodule_general_migration_groups
source:
  plugin: mymodule_migration_users
process:
  pass: user_password
  mail: user_email
  init: user_email
  status:
    plugin: default_value
    default_value: 1
  name:
    plugin: dedupe_entity
    source: username
    entity_type: user
    field: name
    postfix: _
  created:
    plugin: callback
    source: created
    callable: strtotime
  changed: user_regdate
  access: user_regdate
  login: user_lastvisit
destination:
  plugin: entity:user
  md5_password: true
migration_dependencies: {}
dependencies:
  enforced:
    module:
      - mymodule_migration
EN

回答 2

Drupal用户

回答已采纳

发布于 2020-01-28 09:11:51

此简单代码在将md5密码迁移到drupal 8时运行正常。

代码语言:javascript
运行
复制
$password_service = \Drupal::service('password');

  $password = $row->getSourceProperty('user_password'); // 'user_password' is the old db column which is in md5 format

  $hashed_password = 'U' . $password_service->hash($password); // U will indicate that the password needs rehashing.
  $row->setSourceProperty('hash_password', $hashed_password);
票数 1
EN

Drupal用户

发布于 2020-01-24 07:41:47

因为您不知道密码,只知道散列,所以不能直接迁移它。(MD5是弱的,但没有那么弱。)散列的全部目的是密码应该是任何人都不知道的,除了用户。有两种解决方案。

  • 重置所有用户密码,并在迁移后向他们发送一个一次性登录密码--正如上面的注释中所建议的那样。
  • 在登录时编写重新散列密码的自定义代码:“如果密码是用MD5散列的,则在用户(成功)登录时重新散列并存储哈希。”这是因为在登录时,您有用户的密码,因为他们刚刚输入了密码。您知道从存储的hash.Initially中使用哪种散列算法,您可以使用MD5密码哈希迁移用户。

事实证明,Drupal会自动重新散列(请参阅UserAuth::authenticate()),但对于MD5则不然,除非您用"U“前缀稍微修改现有散列。(谢谢d70rr3s和Souvik。)另见用户_更新_7000

票数 3
EN
页面原文内容由Drupal提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://drupal.stackexchange.com/questions/290453

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档