Skip to content

Instantly share code, notes, and snippets.

@lasekmiroslaw
Last active July 24, 2018 19:58
Show Gist options
  • Save lasekmiroslaw/d0298fec67813b0559f3b1f00ee58482 to your computer and use it in GitHub Desktop.
Save lasekmiroslaw/d0298fec67813b0559f3b1f00ee58482 to your computer and use it in GitHub Desktop.
password hash eventlistener symfony doctrine
<?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