【问题标题】:Migrating legacy users to symfony2将旧用户迁移到 symfony2
【发布时间】:2012-08-13 20:32:08
【问题描述】:

我正在从表达式引擎迁移到 symfony2,并且我正在寻找迁移用户密码的最佳方法。目标是让旧用户使用其现有凭据登录,而新用户的密码则以默认方式创建。

我查看了自定义身份验证提供程序和自定义用户提供程序,并考虑是否为旧用户创建单独的实体,但我不知道实现上述目标的最佳方式/设计是什么。

仅供参考:

  • 据我所知,表达式引擎只是加密密码 使用 sha1 就是这样。
  • 我目前正在使用 FOSUserBundle。

谁能给我一个解决方案的建议?

【问题讨论】:

  • 我认为最好的解决方案是将所有用户重新导入fos_user 表。由于 FOSUserBundle 使用 SHA1,密码一定不是问题。
  • 我认为 FOSUserBundle 使用 sha512
  • 您可以将编码器工厂配置为使用您喜欢的哈希算法。
  • 是的,我明白了。但是,我需要同时支持新老用户。我需要知道使用什么设计来实现这一目标
  • 你不必,只需将sha1配置为密码算法即可。

标签: php authentication symfony migration expressionengine


【解决方案1】:

想通了!

创建自定义编码器并使用 FOSAdvancedEncoder 捆绑包选择合适的编码器。

1.创建编码器

    <?php

    namespace Acme\MyBundle\Security\Encoder;

    use Symfony\Component\Security\Core\Encoder\PasswordEncoderInterface;

    class LegacyEncoder implements PasswordEncoderInterface {

        public function encodePassword($raw, $salt)
        {
            // Your Custom encoder logic
            return $something 
        }

        public function isPasswordValid($encoded, $raw, $salt)
        {
            return $encoded === $this->encodePassword($raw, $salt);
        }

    }

2。将您的编码器注册为服务

services:
    acme.legacy_encoder:
        class: Acme\MyBundle\Security\Encoder\LegacyEncoder

3.安装 FOSAdvancedEncoderBundle

看这里:https://github.com/friendsofsymfony/FOSAdvancedEncoderBundle/blob/master/Resources/doc/index.md

4.配置您的编码器

app/config.yml:

fos_advanced_encoder:
    encoders:
        FOS\UserBundle\Model\UserInterface: sha512
        legacy_encoder:
            id: acme.legacy_encoder

5.在您的用户类中实现编码器感知接口

use FOS\AdvancedEncoderBundle\Security\Encoder\EncoderAwareInterface;
use FOS\UserBundle\Entity\User as BaseUser;

class User extends BaseUser implements EncoderAwareInterface {

  ...

  public function getEncoderName() {

      if($this->islegacy()) {
          return "legacy_encoder";
      }

      return NULL;
  }

}

记得添加一个布尔字段来管理用户是否是旧用户。

就是这样。

【讨论】:

    【解决方案2】:

    也许关于exporting members from ExpressionEngine to Wordpress 的帖子会对你有所帮助。

    除了将自定义查询的结果导出到 FOSUserBundle 结构之外,我没有发现任何困难。

    要记住的重要事项:

    • 正如 Derek Hogue 在该线程中所指出的那样,用户很可能不得不重置密码
    • 您必须非常了解这两种结构才能正确导入数据

    【讨论】:

    • 抱歉,这行不通。正如我在问题中所说:The goal is to let legacy users log in with their existing credentials, while passwords for new users are created the default way.
    • 是的,它会起作用的。您可以只保留每个密码的哈希值,而不是要求用户重置它们。
    • 我已经测试过了,它不会。这是因为 symfony 将散列密码基于盐和普通密码的组合。 EE 只使用普通密码的 sha1 散列变体
    • 当然不会神奇地起作用。你是开发者,对吧?您将不得不做一些适应工作才能使其按预期工作。您可以将security: encoders: FOS\UserBundle\Model\UserInterface: sha512 更改为正确的哈希值并感到高兴,或者编写一些自定义代码来处理新用户和旧用户的情况。
    猜你喜欢
    • 2023-03-26
    • 2020-05-08
    • 2013-02-17
    • 2017-11-07
    • 2014-12-09
    • 1970-01-01
    • 2015-02-04
    • 2018-07-01
    • 1970-01-01
    相关资源
    最近更新 更多