Migrating legacy users to symfony2

▼魔方 西西 提交于 2019-11-28 17:58:57

Figured it out!

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

1. Create the encoder

    <?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. Register your encoder as service

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

3. Install FOSAdvancedEncoderBundle

Look here: https://github.com/friendsofsymfony/FOSAdvancedEncoderBundle/blob/master/Resources/doc/index.md

4. Configure your encoders

In app/config.yml:

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

5. Implement the encoder aware interface in your User Class

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

}

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

That's it.

Daniel Ribeiro

Maybe the thread about exporting members from ExpressionEngine to Wordpress will help you.

I don't see any difficulties other than exporting the results from a custom query to the FOSUserBundle structure.

Important things to remember:

  • As Derek Hogue greatly points out on that thread, users will most likely have to reset their passwords
  • You will have to understand the both structures really well in order to correctly import data
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!