symfony2自定义passwordEncoder与用户数据

时间:2014-04-02 12:48:51

标签: php security symfony passwords encoder

我使用symfony 2.4迁移旧的用户系统, 我想充分利用FOSUserBundle和encodePassword函数来使用用户的某些属性(例如,它的id),但我找不到将$ myUserObject插入此MyPasswordEncoder类的方法。

namespace Tykayn\MeluzineBundle\Services;
class MyPasswordEncoder extends  \Symfony\Component\Security\Core\Encoder\BasePasswordEncoder 

{

public function encodePassword($raw, $salt, **$myUserObject** )
    {
        return sha1('blahblah' . $raw . 'blehbleh'. $salt. $myUserObject->id ) ;
    } 
public function isPasswordValid($encoded, $raw, $salt)
    {
        return $this->comparePasswords($encoded, $this->encodePassword($raw, $salt));
    }
}

1 个答案:

答案 0 :(得分:0)

找到了一些不错的方法:

在我的userbundle中创建一个自定义密码编码器,扩展了FOSuserbundle。

namespace Sdz\UserBundle\Security\Encoder;
use Symfony\Component\Security\Core\Encoder\BasePasswordEncoder;

class CustomEncoder extends BasePasswordEncoder {
public function encodePassword($raw, $salt) {
// do doctrine stuff to get the user info
return $hashed;
    }
public function isPasswordValid($encoded, $raw, $salt) {
  return $this->comparePasswords($encoded, $this->encodePassword($raw, $salt));
    }
}

将编码器注册为myuserbundle的服务,将其注入教条服务。

services:
sdz_user.custom_encoder:
    class: "Sdz\UserBundle\Security\Encoder\CustomEncoder"
    arguments: [ @Doctrine]

配置全局security.yml

security:


encoders:
    # encodeur de password selon l'ancienne façon
    FOS\UserBundle\Model\UserInterface:
        id: sdz_user.custom_encoder