src/WellCommerce/Bundle/AppBundle/EventListener/AdminSubscriber.php line 45

Open in your IDE?
  1. <?php
  2. declare(strict_types=0);
  3. /*
  4.  * WellCommerce Foundation
  5.  *
  6.  * This file is part of the WellCommerce package.
  7.  *
  8.  * (c) Adam Piotrowski <adam@wellcommerce.org>, Adrian Potepa <adrian@wellcommerce.org>
  9.  *
  10.  * For the full copyright and license information,
  11.  * please view the LICENSE file that was distributed with this source code.
  12.  */
  13. namespace WellCommerce\Bundle\AppBundle\EventListener;
  14. use Carbon\Carbon;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. use Symfony\Component\HttpFoundation\RedirectResponse;
  17. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  18. use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
  19. use Symfony\Component\HttpKernel\KernelEvents;
  20. use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
  21. use WellCommerce\Bundle\AppBundle\Entity\Client;
  22. use WellCommerce\Bundle\AppBundle\Entity\Shop;
  23. use WellCommerce\Bundle\AppBundle\Entity\User;
  24. use WellCommerce\Bundle\CoreBundle\DependencyInjection\AbstractServiceSubscriber;
  25. use WellCommerce\Bundle\CoreBundle\Doctrine\Event\EntityEvent;
  26. /**
  27.  * Class AdminSubscriber
  28.  *
  29.  * @author  Adam Piotrowski <adam@wellcommerce.org>
  30.  */
  31. class AdminSubscriber extends AbstractServiceSubscriber implements EventSubscriberInterface
  32. {
  33.     public static function getSubscribedEvents()
  34.     {
  35.         return [
  36.             'user.pre_create'            => ['onUserPreCreate'0],
  37.             'security.interactive_login' => ['onSecurityInteractiveLogin'0],
  38.             KernelEvents::CONTROLLER     => ['onKernelController'0],
  39.         ];
  40.     }
  41.     public function onKernelController(ControllerEvent $event)
  42.     {
  43.         if ($event->isMasterRequest()) {
  44.             $request $event->getRequest();
  45.             $user    $this->getSecurityHelper()->getCurrentAdmin();
  46.             if ($request->isMethod('POST')) {
  47.                 return;
  48.             }
  49.             if ($user instanceof User) {
  50.                 if ($user->isSessionExpired()) {
  51.                     $redirectUrl $this->getRouterHelper()->generateUrl('admin.user.logout');
  52.                     $event->setController(function () use ($redirectUrl) {
  53.                         return new RedirectResponse($redirectUrl);
  54.                     });
  55.                 }
  56.                 $user->setLastActive(Carbon::now()->toDateTimeImmutable());
  57.                 $user->setSessionExpiresAt(Carbon::now()->addMinutes($user->getSessionLifetime())->toDateTimeImmutable());
  58.                 $this->getEntityManager()->flush();
  59.             }
  60.         }
  61.     }
  62.     public function onSecurityInteractiveLogin(InteractiveLoginEvent $event)
  63.     {
  64.         $user $event->getAuthenticationToken()->getUser();
  65.         if ($user instanceof User) {
  66.             $user->setLastActive(Carbon::now()->toDateTimeImmutable());
  67.             $user->setSessionExpiresAt(null);
  68.             $shop $user->getShop();
  69.             $this->getEntityManager()->flush();
  70.             if ($shop instanceof Shop) {
  71.                 $this->getRequestHelper()->setSessionAttribute('admin/shop/id'$shop->getId());
  72.             }
  73.             $this->getRequestHelper()->setSessionAttribute('global_view'0);
  74.         }
  75.     }
  76.     public function onUserPreCreate(EntityEvent $entityEvent)
  77.     {
  78.         $password $this->getSecurityHelper()->generateRandomPassword();
  79.         $user     $entityEvent->getEntity();
  80.         if ($user instanceof User) {
  81.             $user->setPassword($password);
  82.             $this->getMailerHelper()->sendEmail([
  83.                 'recipient'     => $user->getEmail(),
  84.                 'bcc'           => [],
  85.                 'subject'       => $this->getTranslatorHelper()->trans('user.email.title.register'),
  86.                 'template'      => 'WellCommerceAppBundle:Admin/Email:register.html.twig',
  87.                 'parameters'    => [
  88.                     'user'     => $user,
  89.                     'password' => $password,
  90.                 ],
  91.                 'configuration' => $this->getShopStorage()->getCurrentShop()->getMailerConfiguration(),
  92.             ]);
  93.         }
  94.     }
  95. }