I migrate an old system of Users with symfony 2.4, i want to plenty use the FOSUserBundle and the encodePassword function to use some properties of a user (for example, its id) but i don't find a way to insert $myUserObject into this MyPasswordEncoder class.
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));
}
}
found some nice way:
create a custom password encoder into my userbundle extending the 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));
}
}
register the encoder as a service of myuserbundle, inject it the doctrine service.
services:
sdz_user.custom_encoder:
class: "Sdz\UserBundle\Security\Encoder\CustomEncoder"
arguments: [ @Doctrine]
configure the global security.yml
security:
encoders:
# encodeur de password selon l'ancienne façon
FOS\UserBundle\Model\UserInterface:
id: sdz_user.custom_encoder