FOSUserBundle: Custom password / Migration from old DB structure

后端 未结 2 672
我寻月下人不归
我寻月下人不归 2020-12-28 11:20

I want to move to Symfony2, because I am totally impressed by its modernity and good programming.

Now I am taking a users table from my old system, with 10,000 users

相关标签:
2条回答
  • 2020-12-28 11:27

    You need to create a custom password encoder:

    <?php
    
    use Symfony\Component\Security\Core\Encoder\BasePasswordEncoder;
    
    class MyPasswordEncoder extends BasePasswordEncoder
    {
        public function encodePassword($raw, $salt)
        {
            return md5($salt.$raw.$salt);
        }
    
        public function isPasswordValid($encoded, $raw, $salt)
        {
            return $this->comparePasswords($encoded, $this->encodePassword($raw, $salt));
        }
    }
    

    And configure it in security.yml:

    services:
        my_password_encoder:
            class: MyPasswordEncoder
    
    security:
        encoders:
            FOS\UserBundle\Model\UserInterface: { id: my_password_encoder }
    

    As long as User::getSalt() returns secret and User::getPassword() returns passhash you should be good to go.

    0 讨论(0)
  • 2020-12-28 11:44

    It is very easy to do with FOSUserBundle. This is the code for it:

    $userManager = $this->get('fos_user.user_manager');
    
    foreach ($items as $item) {
        $newItem = $userManager->createUser();
    
        //$newItem->setId($item->getObjId());
        // FOSUserBundle required fields
        $newItem->setUsername($item->getUsername());
        $newItem->setEmail($item->getEmail());
        $newItem->setPlainPassword($item->getPassword()); // get original password
        $newItem->setEnabled(true);
    
        $userManager->updateUser($newItem, true);
    }
    
    0 讨论(0)
提交回复
热议问题