Last active
July 24, 2018 19:58
-
-
Save lasekmiroslaw/d0298fec67813b0559f3b1f00ee58482 to your computer and use it in GitHub Desktop.
password hash eventlistener symfony doctrine
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\EventListener; | |
use Doctrine\ORM\Event\LifecycleEventArgs; | |
use App\Entity\User; | |
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface; | |
class PasswordHashListener | |
{ | |
private $passwordEncoder; | |
public function __construct(UserPasswordEncoderInterface $passwordEncoder) | |
{ | |
$this->passwordEncoder = $passwordEncoder; | |
} | |
public function prePersist(LifecycleEventArgs $args) | |
{ | |
$entity = $args->getEntity(); | |
// only act on some "User" entity | |
if (!$entity instanceof User) { | |
return; | |
} | |
$password = $this->passwordEncoder->encodePassword($entity, $entity->getPlainPassword()); | |
$entity->setPassword($password); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment