<?php
namespace App\EventSubscriber;
use App\Entity\Categorie;
use App\Entity\Devis;
use App\Entity\ProjetAcces;
use App\Entity\User;
use Doctrine\ORM\EntityManagerInterface;
use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityPersistedEvent;
use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityUpdatedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
class EasyAdminSubscriber implements EventSubscriberInterface
{
private $entityManager;
private $passwordEncoder;
private $skey = "SpPowerPass444?!";
private $iv = "02f30dffbb0d084755f438f7d8be4a7d";
public function __construct(EntityManagerInterface $entityManager, UserPasswordHasherInterface $passwordEncoder,
TokenStorageInterface $token)
{
$this->entityManager = $entityManager;
$this->passwordEncoder = $passwordEncoder;
$this->token = $token;
}
public static function getSubscribedEvents()
{
return [
BeforeEntityPersistedEvent::class => ['add'],
BeforeEntityUpdatedEvent::class => ['update'],
];
}
public function add(BeforeEntityPersistedEvent $event){
$entity = $event->getEntityInstance();
if ($entity instanceof ProjetAcces) {
$this->addAcces($entity);
}else if ($entity instanceof User) {
$this->addUser($entity);
}else if($entity instanceof Devis){
$this->addDevis($entity);
}else if($entity instanceof Categorie){
$this->setClient($entity);
}
}
public function setClient($entity){
$entity->setClient($this->token->getToken()->getUser()->getClient());
}
public function addDevis($entity){
$entity->setDateCreation(new \DateTime);
$this->entityManager->persist($entity);
$this->entityManager->flush();
$count = count($this->entityManager->getRepository('App:Devis')->findAll());
$numgen = str_pad($count, 4, "0", STR_PAD_LEFT);
$numero = strtoupper(date("Y") . '-' . $numgen . "_DRAFT");
$entity->setNumero($numero);
$this->em->persist($entity);
$this->em->flush();
}
public function addAcces($entity)
{
$this->encodePassword($entity);
}
public function addUser($entity)
{
$this->setPassword($entity);
}
public function update(BeforeEntityUpdatedEvent $event){
$entity = $event->getEntityInstance();
if ($entity instanceof ProjetAcces) {
$this->updateAcces($entity);
}
if ($entity instanceof User) {
$this->addUser($entity);
}
}
public function updateAcces($entity)
{
$this->encodePassword($entity);
}
public function updateUser($entity)
{
$this->setPassword($entity);
}
/**
* @param User $entity
*/
public function encodePassword(ProjetAcces $entity): void
{
$pass = $entity->getPlainPassword();
if($pass != null){
$entity->setPass(
$this->encode($pass)
);
}
$this->entityManager->persist($entity);
$this->entityManager->flush();
}
private function safe_b64encode($string)
{
$data = base64_encode($string);
$data = str_replace(array('+', '/', '='), array('-', '_', ''), $data);
return $data;
}
private function encode($value)
{
if (!$value) {
return false;
}
$text = $value;
$crypttext = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $this->skey, $text, MCRYPT_MODE_CBC, $this->iv);
return trim($this->safe_b64encode($crypttext));
}
/**
* @param User $entity
*/
public function setPassword(User $entity): void
{
$pass = $entity->getPlainPassword();
if($pass != null){
$entity->setPassword(
$this->passwordEncoder->hashPassword(
$entity,
$pass
)
);
}
$this->entityManager->persist($entity);
$this->entityManager->flush();
}
}