src/EventSubscriber/EasyAdminSubscriber.php line 45

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\Article;
  4. use App\Entity\ArticleContenu;
  5. use App\Entity\Page;
  6. use App\Entity\PageContenu;
  7. use App\Entity\Projet;
  8. use App\Entity\User;
  9. use Doctrine\ORM\EntityManagerInterface;
  10. use Doctrine\Persistence\ManagerRegistry;
  11. use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityPersistedEvent;
  12. use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityUpdatedEvent;
  13. use Psr\Log\LoggerInterface;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
  16. class EasyAdminSubscriber implements EventSubscriberInterface
  17. {
  18.     private $entityManager;
  19.     private $passwordEncoder;
  20.     private ManagerRegistry $manager;
  21.     public function __construct(
  22.         ManagerRegistry $manager,
  23.         EntityManagerInterface $entityManager,
  24.         UserPasswordEncoderInterface $passwordEncoder,
  25.         LoggerInterface $logger
  26.     ) {
  27.         $this->manager $manager;
  28.         $this->entityManager $entityManager;
  29.         $this->passwordEncoder $passwordEncoder;
  30.         $this->logger $logger;
  31.     }
  32.     public static function getSubscribedEvents()
  33.     {
  34.         return [
  35.             BeforeEntityPersistedEvent::class => ['beforeAdd'],
  36.             BeforeEntityUpdatedEvent::class => ['beforeUpdate'], //surtout utile lors d'un reset de mot passe plutôt qu'un réel update, car l'update va de nouveau encrypter le mot de passe DEJA encrypté ...
  37.         ];
  38.     }
  39.     public function beforeAdd(BeforeEntityPersistedEvent $event)
  40.     {
  41.         $entity $event->getEntityInstance();
  42.         if ($entity instanceof User) {
  43.             $this->setPassword($entity);
  44.         } else if ($entity instanceof Page) {
  45.             $entity->setDescription(str_replace([
  46.                 '<div>''</div>',
  47.                 '<h1>''</h1>',
  48.                 '<h2>''</h2>',
  49.                 '<h3>''</h3>',
  50.                 '<h4>''</h4>',
  51.                 '<h5>''</h5>',
  52.                 '<h6>''</h6>',
  53.                 '<p>''</p>',
  54.                 '<pre>''</pre>',
  55.                 '<blockquote>''</blockquote>'
  56.             ], ""$entity->getDescription()));
  57.             $entity->setDescriptionCourte(str_replace([
  58.                 '<div>''</div>',
  59.                 '<h1>''</h1>',
  60.                 '<h2>''</h2>',
  61.                 '<h3>''</h3>',
  62.                 '<h4>''</h4>',
  63.                 '<h5>''</h5>',
  64.                 '<h6>''</h6>',
  65.                 '<p>''</p>',
  66.                 '<pre>''</pre>',
  67.                 '<blockquote>''</blockquote>'
  68.             ], ""$entity->getDescriptionCourte()));
  69.             $entity->setDescriptionFin(str_replace([
  70.                 '<div>''</div>',
  71.                 '<h1>''</h1>',
  72.                 '<h2>''</h2>',
  73.                 '<h3>''</h3>',
  74.                 '<h4>''</h4>',
  75.                 '<h5>''</h5>',
  76.                 '<h6>''</h6>',
  77.                 '<p>''</p>',
  78.                 '<pre>''</pre>',
  79.                 '<blockquote>''</blockquote>'
  80.             ], ""$entity->getDescriptionFin()));
  81.         } else if ($entity instanceof PageContenu) {
  82.             $entity->setParagraph(str_replace([
  83.                 '<div>''</div>',
  84.                 '<h1>''</h1>',
  85.                 '<h2>''</h2>',
  86.                 '<h3>''</h3>',
  87.                 '<h4>''</h4>',
  88.                 '<h5>''</h5>',
  89.                 '<h6>''</h6>',
  90.                 '<p>''</p>',
  91.                 '<pre>''</pre>',
  92.                 '<blockquote>''</blockquote>'
  93.             ], ""$entity->getParagraph()));
  94.         } else if ($entity instanceof Article) {
  95.             $entity->setDescription(str_replace([
  96.                 '<div>''</div>',
  97.                 '<h1>''</h1>',
  98.                 '<h2>''</h2>',
  99.                 '<h3>''</h3>',
  100.                 '<h4>''</h4>',
  101.                 '<h5>''</h5>',
  102.                 '<h6>''</h6>',
  103.                 '<p>''</p>',
  104.                 '<pre>''</pre>',
  105.                 '<blockquote>''</blockquote>'
  106.             ], ""$entity->getDescription()));
  107.         } else if ($entity instanceof ArticleContenu) {
  108.             $entity->setParagraphe(str_replace([
  109.                 '<div>''</div>',
  110.                 '<h1>''</h1>',
  111.                 '<h2>''</h2>',
  112.                 '<h3>''</h3>',
  113.                 '<h4>''</h4>',
  114.                 '<h5>''</h5>',
  115.                 '<h6>''</h6>',
  116.                 '<p>''</p>',
  117.                 '<pre>''</pre>',
  118.                 '<blockquote>''</blockquote>'
  119.             ], ""$entity->getParagraphe()));
  120.         } else if ($entity instanceof Projet) {
  121.             $entity->setAppSecret(bin2hex(random_bytes(16)));
  122.         } else {
  123.             return;
  124.         }
  125.     }
  126.     public function beforeUpdate(BeforeEntityUpdatedEvent $event)
  127.     {
  128.         $entity $event->getEntityInstance();
  129.         /* if ($entity instanceof User) {
  130.             $this->setPassword($entity);
  131.         } else */
  132.         if ($entity instanceof Page) {
  133.             $entity->setDescription(str_replace([
  134.                 '<div>''</div>',
  135.                 '<h1>''</h1>',
  136.                 '<h2>''</h2>',
  137.                 '<h3>''</h3>',
  138.                 '<h4>''</h4>',
  139.                 '<h5>''</h5>',
  140.                 '<h6>''</h6>',
  141.                 '<p>''</p>',
  142.                 '<pre>''</pre>',
  143.                 '<blockquote>''</blockquote>'
  144.             ], ""$entity->getDescription()));
  145.             $entity->setDescriptionCourte(str_replace([
  146.                 '<div>''</div>',
  147.                 '<h1>''</h1>',
  148.                 '<h2>''</h2>',
  149.                 '<h3>''</h3>',
  150.                 '<h4>''</h4>',
  151.                 '<h5>''</h5>',
  152.                 '<h6>''</h6>',
  153.                 '<p>''</p>',
  154.                 '<pre>''</pre>',
  155.                 '<blockquote>''</blockquote>'
  156.             ], ""$entity->getDescriptionCourte()));
  157.             $entity->setDescriptionFin(str_replace([
  158.                 '<div>''</div>',
  159.                 '<h1>''</h1>',
  160.                 '<h2>''</h2>',
  161.                 '<h3>''</h3>',
  162.                 '<h4>''</h4>',
  163.                 '<h5>''</h5>',
  164.                 '<h6>''</h6>',
  165.                 '<p>''</p>',
  166.                 '<pre>''</pre>',
  167.                 '<blockquote>''</blockquote>'
  168.             ], ""$entity->getDescriptionFin()));
  169.         } else if ($entity instanceof PageContenu) {
  170.             $entity->setParagraph(str_replace([
  171.                 '<div>''</div>',
  172.                 '<h1>''</h1>',
  173.                 '<h2>''</h2>',
  174.                 '<h3>''</h3>',
  175.                 '<h4>''</h4>',
  176.                 '<h5>''</h5>',
  177.                 '<h6>''</h6>',
  178.                 '<p>''</p>',
  179.                 '<pre>''</pre>',
  180.                 '<blockquote>''</blockquote>'
  181.             ], ""$entity->getParagraph()));
  182.         } else if ($entity instanceof Article) {
  183.             $entity->setDescription(str_replace([
  184.                 '<div>''</div>',
  185.                 '<h1>''</h1>',
  186.                 '<h2>''</h2>',
  187.                 '<h3>''</h3>',
  188.                 '<h4>''</h4>',
  189.                 '<h5>''</h5>',
  190.                 '<h6>''</h6>',
  191.                 '<p>''</p>',
  192.                 '<pre>''</pre>',
  193.                 '<blockquote>''</blockquote>'
  194.             ], ""$entity->getDescription()));
  195.         } else if ($entity instanceof ArticleContenu) {
  196.             $entity->setParagraphe(str_replace([
  197.                 '<div>''</div>',
  198.                 '<h1>''</h1>',
  199.                 '<h2>''</h2>',
  200.                 '<h3>''</h3>',
  201.                 '<h4>''</h4>',
  202.                 '<h5>''</h5>',
  203.                 '<h6>''</h6>',
  204.                 '<p>''</p>',
  205.                 '<pre>''</pre>',
  206.                 '<blockquote>''</blockquote>'
  207.             ], ""$entity->getParagraphe()));
  208.         } else if ($entity instanceof Projet) {
  209.             if ($entity->getAppSecret() == null || $entity->getAppSecret() == '') {
  210.                 $entity->setAppSecret(bin2hex(random_bytes(16)));
  211.             }
  212.         } else {
  213.             return;
  214.         }
  215.     }
  216.     /**
  217.      * @param User $entity
  218.      */
  219.     public function setPassword(User $entity): void
  220.     {
  221.         $pass $entity->getPassword();
  222.         $entity->setPassword(
  223.             $this->passwordEncoder->encodePassword(
  224.                 $entity,
  225.                 $pass
  226.             )
  227.         );
  228.         $this->entityManager->persist($entity);
  229.         $this->entityManager->flush();
  230.     }
  231. }