src/EventSubscriber/EasyAdminSubscriber.php line 24

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\Categorie;
  4. use App\Entity\Devis;
  5. use App\Entity\ProjetAcces;
  6. use App\Entity\User;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityPersistedEvent;
  9. use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityUpdatedEvent;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  12. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  13. class EasyAdminSubscriber implements EventSubscriberInterface
  14. {
  15.     private $entityManager;
  16.     private $passwordEncoder;
  17.     private $skey "SpPowerPass444?!";
  18.     private $iv "02f30dffbb0d084755f438f7d8be4a7d";
  19.     public function __construct(EntityManagerInterface $entityManagerUserPasswordHasherInterface $passwordEncoder,
  20.         TokenStorageInterface $token)
  21.     {
  22.         $this->entityManager $entityManager;
  23.         $this->passwordEncoder $passwordEncoder;
  24.         $this->token $token;
  25.     }
  26.     public static function getSubscribedEvents()
  27.     {
  28.         return [
  29.             BeforeEntityPersistedEvent::class => ['add'],
  30.             BeforeEntityUpdatedEvent::class => ['update'], 
  31.         ];
  32.     }
  33.     public function add(BeforeEntityPersistedEvent $event){
  34.         $entity $event->getEntityInstance();
  35.         if ($entity instanceof ProjetAcces) {
  36.             $this->addAcces($entity);
  37.         }else if ($entity instanceof User) {
  38.             $this->addUser($entity);
  39.         }else if($entity instanceof Devis){
  40.             $this->addDevis($entity);
  41.         }else if($entity instanceof Categorie){
  42.             $this->setClient($entity);
  43.         }
  44.     }
  45.     public function setClient($entity){
  46.         $entity->setClient($this->token->getToken()->getUser()->getClient());
  47.     }
  48.     public function addDevis($entity){
  49.         $entity->setDateCreation(new \DateTime);
  50.         $this->entityManager->persist($entity);
  51.         $this->entityManager->flush();
  52.         $count count($this->entityManager->getRepository('App:Devis')->findAll());
  53.         $numgen str_pad($count4"0"STR_PAD_LEFT);
  54.         $numero strtoupper(date("Y") . '-' $numgen "_DRAFT");
  55.         $entity->setNumero($numero);
  56.         $this->em->persist($entity);
  57.         $this->em->flush();
  58.     }
  59.     public function addAcces($entity)
  60.     {
  61.         $this->encodePassword($entity);
  62.     }
  63.     public function addUser($entity)
  64.     {          
  65.           $this->setPassword($entity);
  66.     }
  67.     public function update(BeforeEntityUpdatedEvent $event){
  68.         $entity $event->getEntityInstance();
  69.         if ($entity instanceof ProjetAcces) {
  70.             $this->updateAcces($entity);
  71.         }
  72.         if ($entity instanceof User) {
  73.             $this->addUser($entity);
  74.         }
  75.     }
  76.     public function updateAcces($entity)
  77.     {
  78.         $this->encodePassword($entity);
  79.     }
  80.     public function updateUser($entity)
  81.     {
  82.           $this->setPassword($entity);
  83.     }
  84.     
  85.     /**
  86.        * @param User $entity
  87.        */
  88.     public function encodePassword(ProjetAcces $entity): void
  89.     {
  90.         $pass $entity->getPlainPassword();
  91.         if($pass != null){
  92.             $entity->setPass(
  93.                 $this->encode($pass)
  94.             );
  95.         }
  96.         $this->entityManager->persist($entity);
  97.         $this->entityManager->flush();
  98.     }
  99.     private function safe_b64encode($string)
  100.     {
  101.         $data base64_encode($string);
  102.         $data str_replace(array('+''/''='), array('-''_'''), $data);
  103.         return $data;
  104.     }
  105.     private function encode($value)
  106.     {
  107.         if (!$value) {
  108.             return false;
  109.         }
  110.         $text $value;
  111.         $crypttext mcrypt_encrypt(MCRYPT_RIJNDAEL_256$this->skey$textMCRYPT_MODE_CBC$this->iv);
  112.         return trim($this->safe_b64encode($crypttext));
  113.     }
  114.     /**
  115.        * @param User $entity
  116.        */
  117.     public function setPassword(User $entity): void
  118.     {
  119.         $pass $entity->getPlainPassword();
  120.         if($pass != null){
  121.             $entity->setPassword(
  122.                 $this->passwordEncoder->hashPassword(
  123.                     $entity,
  124.                     $pass
  125.                 )
  126.             );
  127.         }
  128.         $this->entityManager->persist($entity);
  129.         $this->entityManager->flush();
  130.     }
  131. }