src/EventSubscriber/EasyAdminSubscriber.php line 61

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\News;
  4. use App\Entity\Transaction;
  5. use App\Entity\User;
  6. use App\Kernel;
  7. use App\Security\AppAuthenticator;
  8. use Doctrine\ORM\EntityManagerInterface;
  9. use EasyCorp\Bundle\EasyAdminBundle\Event\AfterEntityBuiltEvent;
  10. use EasyCorp\Bundle\EasyAdminBundle\Event\AfterEntityDeletedEvent;
  11. use EasyCorp\Bundle\EasyAdminBundle\Event\AfterEntityPersistedEvent;
  12. use EasyCorp\Bundle\EasyAdminBundle\Event\AfterEntityUpdatedEvent;
  13. use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityDeletedEvent;
  14. use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityPersistedEvent;
  15. use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityUpdatedEvent;
  16. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  17. use Symfony\Component\HttpFoundation\Request;
  18. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  19. use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
  20. use Symfony\Component\String\Slugger\SluggerInterface;
  21. use App\Controller\ProjectUtilsController;
  22. class EasyAdminSubscriber implements EventSubscriberInterface
  23. {
  24.     /**
  25.      * @var UserPasswordEncoderInterface
  26.      */
  27.     private $encoder;
  28.     private $slugger;
  29.     private $authenticator;
  30.     private $storage;
  31.     private $kernel;
  32.     private $em;
  33.     public function __construct(SluggerInterface $sluggerUserPasswordEncoderInterface $encoderAppAuthenticator $authenticatorEntityManagerInterface $emTokenStorageInterface $storageKernel $kernel)
  34.     {
  35.         $this->slugger $slugger;
  36.         $this->encoder $encoder;
  37.         $this->authenticator $authenticator;
  38.         $this->storage $storage;
  39.         $this->kernel $kernel;
  40.         $this->em $em;
  41.     }
  42.     public static function getSubscribedEvents()
  43.     {
  44.         return [
  45.             AfterEntityBuiltEvent::class => ['afterEntityBuilt'],
  46.             AfterEntityDeletedEvent::class => ['afterEntityDeleted'],
  47.             AfterEntityPersistedEvent::class => ['afterEntityPersisted'],
  48.             AfterEntityUpdatedEvent::class => ['afterEntityUpdated'],
  49.             BeforeEntityDeletedEvent::class => ['beforeEntityDeleted'],
  50.             BeforeEntityPersistedEvent::class => ['beforeEntityPersisted'],
  51.             BeforeEntityUpdatedEvent::class => ['beforeEntityUpdated'],
  52.         ];
  53.     }
  54.     public function afterEntityBuilt(AfterEntityBuiltEvent $event)
  55.     {
  56.     }
  57.     public function afterEntityDeleted(AfterEntityDeletedEvent $event)
  58.     {
  59.         $entity $event->getEntityInstance();
  60.         if (!($entity instanceof User)) {
  61.             return;
  62.         }
  63.     }
  64.     public function afterEntityPersisted(AfterEntityPersistedEvent $event)
  65.     {
  66.         $entity $event->getEntityInstance();
  67.         $container $this->kernel->getContainer();
  68.         $em $container->get('doctrine')->getManager();
  69.         $doctrine $container->get('doctrine');
  70.         $className $em->getClassMetadata(get_class($entity))->getName();
  71.         if($className == 'App\Entity\Bot'){
  72.             if($entity->getToken() != '' && $entity->getToken() != null){
  73.                 $host $_SERVER['HTTP_HOST'];
  74.                 $webhook_url = ($_SERVER['HTTPS'] ? 'https://' 'http://') . $host '/api/bot/' $entity->getId() . '/';
  75.                 $paramsSetWebhook = array(
  76.                     'url' =>  $webhook_url
  77.                 );
  78.                 if($entity->getIsActive() == '1'){
  79.                     $setWebhook ProjectUtilsController::requestToBot($entity->getToken(), 'setWebhook'$paramsSetWebhook);
  80.                     if(ProjectUtilsController::isJSON($setWebhook) === true){
  81.                         $r json_decode($setWebhooktrue);
  82.                         if(isset($r['ok']) && isset($r['result'])){
  83.                             if($r['ok'] == true && $r['result'] == true){
  84.                                 $entity->setWebhookUrl($webhook_url);
  85.                                 $em->persist($entity);
  86.                                 $em->flush();
  87.                             }
  88.                         }
  89.                     }
  90.                 } else {
  91.                     $delWebhook ProjectUtilsController::requestToBot($entity->getToken(), 'deleteWebhook', array());
  92.                     if(ProjectUtilsController::isJSON($delWebhook) === true){
  93.                         $r json_decode($delWebhooktrue);
  94.                         if(isset($r['ok']) && isset($r['result'])){
  95.                             if($r['ok'] == true && $r['result'] == true){
  96.                                 $entity->setWebhookUrl(null);
  97.                                 $em->persist($entity);
  98.                                 $em->flush();
  99.                             }
  100.                         }
  101.                     }
  102.                 }
  103.             }
  104.         }
  105.         if($className == 'App\Entity\Transaction'){
  106.             if($entity->getToGroup()){
  107.                 $typeof_reverse null;
  108.                 if($entity->getTypeof() == 'expense'){
  109.                     $typeof_reverse 'income';
  110.                 } elseif($entity->getTypeof() == 'income'){
  111.                     $typeof_reverse 'expense';
  112.                 }
  113.                 $amount_reverse $entity->getAmount();
  114.                 if($typeof_reverse == 'expense'){
  115.                     $amount_reverse '-' $entity->getAmount();
  116.                 }
  117.                 $newTransactionReverse = new Transaction();
  118.                 if($entity->getFromGroup()){
  119.                     $newTransactionReverse->setFromGroup($entity->getFromGroup());
  120.                 }
  121.                 if($entity->getToGroup()){
  122.                     $newTransactionReverse->setToGroup($entity->getToGroup());
  123.                 }
  124.                 if($entity->getOffice()){
  125.                     $newTransactionReverse->setOffice($entity->getOffice());
  126.                 }
  127.                 $newTransactionReverse->setTypeof($typeof_reverse);
  128.                 $newTransactionReverse->setAmount($amount_reverse);
  129.                 $newTransactionReverse->setComment($entity->getComment());
  130.                 $newTransactionReverse->setNote('Добавление транзакции через АРМ');
  131.                 $em->persist($newTransactionReverse);
  132.                 $em->flush();
  133.             }
  134.             $balance false;
  135.             $from_group $entity->getFromGroup();
  136.             if ($entity->getTypeof() == 'expense') {
  137.                 $balance $from_group->getBalance() - $entity->getAmount();
  138.             } elseif ($entity->getTypeof() == 'income') {
  139.                 $balance $from_group->getBalance() + $entity->getAmount();
  140.             }
  141.             if ($balance != $from_group->getBalance()) {
  142.                 $from_group->setBalance($balance);
  143.                 $em->persist($from_group);
  144.                 $em->flush();
  145.             }
  146.             if ($entity->getTypeof() == 'expense' && $entity->getToGroup()) {
  147.                 $to_group $entity->getToGroup();
  148.                 $to_balance $to_group->getBalance() + $entity->getAmount();
  149.                 if ($to_balance != $to_group->getBalance()) {
  150.                     $to_group->setBalance($to_balance);
  151.                     $em->persist($to_group);
  152.                     $em->flush();
  153.                 }
  154.             }
  155.             if($entity->getTypeof() == 'expense' && $entity->getAmount() > 0){
  156.                 $entity->setAmount('-' $entity->getAmount());
  157.             }
  158.             $entity->setNote('Добавление транзакции через АРМ');
  159.             $em->persist($entity);
  160.             $em->flush();
  161.         }
  162.     }
  163.     public function afterEntityUpdated(AfterEntityUpdatedEvent $event)
  164.     {
  165.         $entity $event->getEntityInstance();
  166.         $container $this->kernel->getContainer();
  167.         $em $container->get('doctrine')->getManager();
  168.         $doctrine $container->get('doctrine');
  169.         $className $em->getClassMetadata(get_class($entity))->getName();
  170.         if($className == 'App\Entity\Bot'){
  171.             if($entity->getToken() != '' && $entity->getToken() != null){
  172.                 $host $_SERVER['HTTP_HOST'];
  173.                 $webhook_url = ($_SERVER['HTTPS'] ? 'https://' 'http://') . $host '/api/bot/' $entity->getId() . '/';
  174.                 $paramsSetWebhook = array(
  175.                     'url' =>  $webhook_url
  176.                 );
  177.                 if($entity->getIsActive() == '1'){
  178.                     $setWebhook ProjectUtilsController::requestToBot($entity->getToken(), 'setWebhook'$paramsSetWebhook);
  179.                     if(ProjectUtilsController::isJSON($setWebhook) === true){
  180.                         $r json_decode($setWebhooktrue);
  181.                         if(isset($r['ok']) && isset($r['result'])){
  182.                             if($r['ok'] == true && $r['result'] == true){
  183.                                 $entity->setWebhookUrl($webhook_url);
  184.                                 $em->persist($entity);
  185.                                 $em->flush();
  186.                             }
  187.                         }
  188.                     }
  189.                 } else {
  190.                     $delWebhook ProjectUtilsController::requestToBot($entity->getToken(), 'deleteWebhook', array());
  191.                     if(ProjectUtilsController::isJSON($delWebhook) === true){
  192.                         $r json_decode($delWebhooktrue);
  193.                         if(isset($r['ok']) && isset($r['result'])){
  194.                             if($r['ok'] == true && $r['result'] == true){
  195.                                 $entity->setWebhookUrl(null);
  196.                                 $em->persist($entity);
  197.                                 $em->flush();
  198.                             }
  199.                         }
  200.                     }
  201.                 }
  202.             }
  203.             // $infoWebhook = TelegramApi::requestToBot($bot->getToken(), 'getWebhookInfo', array());
  204.         }
  205.     }
  206.     public function beforeEntityDeleted(BeforeEntityDeletedEvent $event)
  207.     {
  208.         $entity $event->getEntityInstance();
  209.         $container $this->kernel->getContainer();
  210.         $request Request::createFromGlobals();
  211.         $em $container->get('doctrine')->getManager();
  212.         $doctrine $container->get('doctrine');
  213.         $className $em->getClassMetadata(get_class($entity))->getName();
  214.     }
  215.     public function beforeEntityPersisted(BeforeEntityPersistedEvent $event)
  216.     {
  217.         $entity $event->getEntityInstance();
  218.         $container $this->kernel->getContainer();
  219.         $em $container->get('doctrine')->getManager();
  220.         $doctrine $container->get('doctrine');
  221.         $className $em->getClassMetadata(get_class($entity))->getName();
  222.         if($className == 'App\Entity\User'){
  223.             if($entity->getPassword() != '' && $entity->getPassword() != null){
  224.                 $encoded $this->encoder->encodePassword($entity$entity->getPassword());
  225.                 $entity->setPassword($encoded);
  226.             }
  227.             if($entity->getTypeof() == 'admin') {
  228.                 $entity->setRoles(array('ROLE_SUPER_ADMIN'));
  229.             }  else {
  230.                 $entity->setRoles(array('ROLE_USER'));
  231.             }
  232.         }
  233.     }
  234.     public function beforeEntityUpdated(BeforeEntityUpdatedEvent $event)
  235.     {
  236.         $entity $event->getEntityInstance();
  237.         $container $this->kernel->getContainer();
  238.         $em $container->get('doctrine')->getManager();
  239.         $doctrine $container->get('doctrine');
  240.         $className $em->getClassMetadata(get_class($entity))->getName();
  241.         if($className == 'App\Entity\User'){
  242.             $curPassword $this->curPassword($entity->getId());
  243.             if($curPassword != $entity->getPassword() && $entity->getPassword() != '' && $entity->getPassword() != null){
  244.                 $encoded $this->encoder->encodePassword($entity$entity->getPassword());
  245.                 $entity->setPassword($encoded);
  246.             }
  247.             /*
  248.             if($entity->getTypeof() == 'admin') {
  249.                 $entity->setRoles(array('ROLE_SUPER_ADMIN'));
  250.             } elseif($entity->getTypeof() == 'admin_personal') {
  251.                 $entity->setRoles(array('ROLE_ADMIN_LK'));
  252.             } else {
  253.                 $entity->setRoles(array('ROLE_USER'));
  254.             }
  255.             */
  256.         }
  257.     }
  258.     private function curPassword($id){
  259.         $curPassword '';
  260.         $hostname_db '';
  261.         $port_db '';
  262.         $name_db '';
  263.         $user_db '';
  264.         $password_db '';
  265.         $db_url $_ENV["DATABASE_URL"];
  266.         $arr explode('?'str_replace('mysql://'''$db_url));
  267.         if(isset($arr[0])){
  268.             $arr1 explode('/'$arr[0]);
  269.             if(isset($arr1[1])){
  270.                 $name_db $arr1[1];
  271.             }
  272.             if(isset($arr1[0])){
  273.                 $arr2 explode('@'$arr1[0]);
  274.                 if(isset($arr2[0])){
  275.                     $arr3 explode(':'$arr2[0]);
  276.                     if(isset($arr3[0])){
  277.                         $user_db $arr3[0];
  278.                     }
  279.                     if(isset($arr3[1])){
  280.                         $password_db $arr3[1];
  281.                     }
  282.                 }
  283.                 if(isset($arr2[1])){
  284.                     $arr4 explode(':'$arr2[1]);
  285.                     if(isset($arr4[0])){
  286.                         $hostname_db $arr4[0];
  287.                     }
  288.                     if(isset($arr4[1])){
  289.                         $port_db $arr4[1];
  290.                     }
  291.                 }
  292.             }
  293.         }
  294.         $conn mysqli_init();
  295.         $conn->real_connect($hostname_db$user_db$password_db,  $name_db$port_db);
  296.         $q_find_user mysqli_query($conn"SELECT `id`, `password` FROM `user` WHERE `id` = '" $id "'");
  297.         if($q_find_user !== false && mysqli_num_rows($q_find_user) != 0){
  298.             $user_q mysqli_fetch_assoc($q_find_user);
  299.             $curPassword $user_q['password'];
  300.         }
  301.         mysqli_close($conn);
  302.         return $curPassword;
  303.     }
  304. }