src/EventSubscriber/AgentEventSubscriber.php line 107

Open in your IDE?
  1. <?php
  2. // src/EventSubscriber/ExceptionSubscriber.php
  3. namespace App\EventSubscriber;
  4. use App\Entity\Location;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Symfony\Component\HttpKernel\Event\ExceptionEvent;
  7. use Symfony\Component\HttpKernel\KernelEvents;
  8. use Webkul\UVDesk\CoreFrameworkBundle\Workflow\Events as CoreWorkflowEvents;
  9. use App\Event as MyCoreWorkflowEvents;
  10. use Webkul\UVDesk\AutomationBundle\Workflow\Event;
  11. use Symfony\Component\HttpFoundation\RedirectResponse;
  12. use Symfony\Component\HttpClient\CurlHttpClient;
  13. //use Symfony\Component\DependencyInjection\ContainerInterface;
  14. use Doctrine\ORM\EntityManagerInterface;
  15. class AgentEventSubscriber implements EventSubscriberInterface
  16. {
  17.     //protected string $baseUrl;
  18.     //protected $entityManager;
  19.     public function __construct(EntityManagerInterface $entityManagerstring $baseUrl) {
  20.         $this->entityManager $entityManager;
  21.         $this->baseUrl $baseUrl;
  22.     }
  23.     public static function getSubscribedEvents(): array
  24.     {
  25.         // return the subscribed events, their methods and priorities
  26.         return [
  27.             MyCoreWorkflowEvents\Agent\Create::getId() => [
  28.                 ['onAgentCreate'0],
  29.             ],
  30.             MyCoreWorkflowEvents\Agent\Update::getId() => [
  31.                 ['onAgentUpdate'0],
  32.             ],
  33.             CoreWorkflowEvents\Agent\Delete::getId() => [
  34.                 ['onAgentDelete'0],
  35.             ],
  36.             /*'uvdesk.automation.workflow.execute' => [
  37.                 ['onAgentUpdate', -1],
  38.             ]*/
  39.         ];
  40.     }
  41.     public function OnAgentCreate(Event $event)
  42.     {
  43.         $user $event->getUser();
  44.         if (!empty($user) && $user instanceof \Webkul\UVDesk\CoreFrameworkBundle\Entity\User) {
  45.             //$data = ['email' => $user->getEmail(), 'password' => $event->getPassword()];
  46.             $userInfos $event->getUserInfos();
  47.             
  48.             $locationDetails Location::decompose($userInfos->getCommune());
  49.             $roles = [];
  50.             foreach($user->getAgentInstance()->getSupportPrivileges() as $supportPrivileges) {
  51.                 //foreach($supportPrivileges->getPrivileges() as $privileges) {
  52.                     if(!in_array($supportPrivileges$roles)) {
  53.                         $roles[] = [$supportPrivileges->getName() => $supportPrivileges->getPrivileges()];
  54.                     }
  55.                 //}
  56.             }
  57.             $data array_merge($locationDetails, ['email' => $user->getEmail(), 'name' => $user->getFullName(),
  58.                 'password' => "@D'O25!dsGd"'roles' => $roles]) ;
  59.             
  60.             $httpClient = new CurlHttpClient(["verify_peer"=>false,"verify_host"=>false]);
  61.             $url $this->baseUrl.'/api/assign-roles'//'/api/register';
  62.             $response $httpClient->request('POST'$url, [
  63.                 'headers' => [
  64.                     'Accept' => 'application/json',
  65.                 ],
  66.                 // these values are automatically encoded before including them in the URL
  67.                 'body' => $data,
  68.             ]);
  69.             $rep json_decode($response->getContent(), true);
  70.             //dd($rep);
  71.             /*if($rep['success']) {
  72.                 $userInfos->setAccessToken($rep['token']);
  73.                 //on doit persister �a en BD
  74.                 $this->entityManager->persist($userInfos);
  75.                 $this->entityManager->flush();*/
  76.                 /*//on lui assigne les privileges
  77.                 if(!empty($roles)) {
  78.                     $url = $this->baseUrl.'/api/assign-roles';
  79.                     $data = ['email' => $user->getEmail(), 'roles' => $roles];
  80.                     $response = $httpClient->request('POST', $url, [
  81.                         'headers' => [
  82.                             'Accept' => 'application/json',
  83.                         ],
  84.                         // these values are automatically encoded before including them in the URL
  85.                         'body' => $data,
  86.                     ]);
  87.                 }*/
  88.             /*} else {
  89.                 //dd($rep);
  90.                 /* to do flash message * /
  91.             }*/
  92.             
  93.         }
  94.     }
  95.     public function OnAgentUpdate(Event $event)
  96.     {        
  97.         $user $event->getUser();
  98.         if (!empty($user) && $user instanceof \Webkul\UVDesk\CoreFrameworkBundle\Entity\User) {
  99.             //$data = ['email' => $user->getEmail(), 'password' => $event->getPassword()];
  100.             $userInfos $event->getUserInfos();
  101.             
  102.             $locationDetails Location::decompose($userInfos->getCommune());
  103.             $roles = [];
  104.             foreach($user->getAgentInstance()->getSupportPrivileges() as $supportPrivileges) {
  105.                 //foreach($supportPrivileges->getPrivileges() as $privileges) {
  106.                     if(!in_array($supportPrivileges$roles)) {
  107.                         $roles[] = [$supportPrivileges->getName() => $supportPrivileges->getPrivileges()];
  108.                     }
  109.                 //}
  110.             }
  111.             $password $event->getPassword() ? $event->getPassword() : null;
  112.             $httpClient = new CurlHttpClient(["verify_peer"=>false,"verify_host"=>false]);
  113.             $url $this->baseUrl.'/api/assign-roles';
  114.             $data array_merge(['email' => $user->getEmail(), 'roles' => $roles'name' => $user->getFullName(), 'password' => $password], $locationDetails);
  115.             //dd($data);
  116.             $response $httpClient->request('POST'$url, [
  117.                 'headers' => [
  118.                     'Accept' => 'application/json',
  119.                 ],
  120.                 // these values are automatically encoded before including them in the URL
  121.                 'body' => $data,
  122.             ]);
  123.             $rep json_decode($response->getContent(), true);
  124.             //dd($rep);
  125.             if($rep['success']) {
  126.                 $userInfos->setAccessToken($rep['ResponseData']['token']);
  127.                 //on doit persister �a en BD
  128.                 $this->entityManager->persist($userInfos);
  129.                 $this->entityManager->flush();
  130.                 //dd($rep);
  131.             } else {
  132.                 /* to do flash message */
  133.                 //dd($rep);
  134.             }
  135.             //dd($rep);
  136.             /*if(!$rep['success'] && strtolower($rep['message']) == "not found") {
  137.                 $data = array_merge($locationDetails, ['email' => $user->getEmail(), 'name' => $user->getFullName(),
  138.                 'password' => "@D'O25!dsGd", 'password_confirmation' => "@D'O25!dsGd"]) ;
  139.                             
  140.                 $url = $this->baseUrl.'/api/register';
  141.                 $response = $httpClient->request('POST', $url, [
  142.                     'headers' => [
  143.                         'Accept' => 'application/json',
  144.                     ],
  145.                     // these values are automatically encoded before including them in the URL
  146.                     'body' => $data,
  147.                 ]);
  148.                 $rep = json_decode($response->getContent(), true);
  149.                 //dd($rep);
  150.                 if($rep['success']) {
  151.                     $userInfos->setAccessToken($rep['ResponseData']['token']);
  152.                     //on doit persister �a en BD
  153.                     $this->entityManager->persist($userInfos);
  154.                     $this->entityManager->flush();
  155.                     //on lui assigne les privileges
  156.                     if(!empty($roles)) {
  157.                         $url = $this->baseUrl.'/api/assign-permission';
  158.                         $data = ['email' => $user->getEmail(), 'permissions' => $roles];
  159.                         $response = $httpClient->request('POST', $url, [
  160.                             'headers' => [
  161.                                 'Accept' => 'application/json',
  162.                             ],
  163.                             // these values are automatically encoded before including them in the URL
  164.                             'body' => $data,
  165.                         ]);
  166.                     }
  167.                 }
  168.             }*/
  169.                         
  170.         }
  171.         
  172.     }
  173. }