将旧用户迁移到 symfony2 [英] Migrating legacy users to symfony2

查看:26
本文介绍了将旧用户迁移到 symfony2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从 expressionengine 转移到 symfony2,我正在寻找迁移用户密码的最佳方式.目标是让旧用户使用其现有凭据登录,而新用户的密码则以默认方式创建.

I'm moving from expressionengine to symfony2 and I'm looking for the best way to migrate the user passwords. The goal is to let legacy users log in with their existing credentials, while passwords for new users are created the default way.

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

I have looked at custom authentication providers and custom user providers and thought about wether or not to create a separate entity for the legacy users, but I don't know what'd be the best way/design to achieve the above.

仅供参考:

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

有人可以就解决方案给我建议吗?

Can anyone advice me on a solution?

推荐答案

想通了!

创建自定义编码器并使用 FOSAdvancedEncoder 包来选择合适的编码器.

Create a custom encoder and use FOSAdvancedEncoder bundle to select the appropriate encoder.

1.创建编码器

    <?php

    namespace AcmeMyBundleSecurityEncoder;

    use SymfonyComponentSecurityCoreEncoderPasswordEncoderInterface;

    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: AcmeMyBundleSecurityEncoderLegacyEncoder

3.安装 FOSAdvancedEncoderBundle

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

4.配置您的编码器

app/config.yml中:

fos_advanced_encoder:
    encoders:
        FOSUserBundleModelUserInterface: sha512
        legacy_encoder:
            id: acme.legacy_encoder

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

use FOSAdvancedEncoderBundleSecurityEncoderEncoderAwareInterface;
use FOSUserBundleEntityUser as BaseUser;

class User extends BaseUser implements EncoderAwareInterface {

  ...

  public function getEncoderName() {

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

      return NULL;
  }

}

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

Remember to add a boolean field to administer if a user is a legacy user or not.

就是这样.

这篇关于将旧用户迁移到 symfony2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆