custom/plugins/CioLoginProtokoll/src/Subscriber/LoginProtokollSubscriber.php line 75

Open in your IDE?
  1. <?php
  2. namespace CioLoginProtokoll\Subscriber;
  3. use CioCustomerPermissionGroups\Event\CustomerAclRolesEvent;
  4. use CioCustomerPermissionGroups\Service\CustomerPermissionService;
  5. use Shopware\Administration\Controller\AdministrationController;
  6. use Shopware\Core\Checkout\Customer\CustomerEntity;
  7. use Shopware\Core\Checkout\Customer\Event\CustomerLoginEvent;
  8. use Shopware\Core\Framework\Context;
  9. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  12. use Shopware\Storefront\Controller\AuthController;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. use Symfony\Component\HttpFoundation\Session\Session;
  15. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  16. use Symfony\Component\HttpKernel\KernelEvents;
  17. class LoginProtokollSubscriber implements EventSubscriberInterface
  18. {
  19.     private EntityRepository $customerRepository;
  20.     private EntityRepository $loginProtokollRepository;
  21.     private Session $session;
  22.     private CustomerPermissionService $checkCustomerPermissionsService;
  23.     public function __construct(EntityRepository $customerRepositoryEntityRepository $loginProtokollRepositorySession $sessionCustomerPermissionService $checkCustomerPermissionsService)
  24.     {
  25.         $this->customerRepository $customerRepository;
  26.         $this->loginProtokollRepository $loginProtokollRepository;
  27.         $this->session $session;
  28.         $this->checkCustomerPermissionsService $checkCustomerPermissionsService;
  29.     }
  30.     public static function getSubscribedEvents(): array
  31.     {
  32.         // Return the events to listen to as array like this:  <event to listen to> => <method to execute>
  33.         return [
  34.             KernelEvents::CONTROLLER => 'onLoginProtokoll',
  35.             CustomerLoginEvent::class => 'onLogin',
  36.             CustomerAclRolesEvent::class => 'onCustomerAclRolesEvent',
  37.         ];
  38.     }
  39.     public function onLoginProtokoll(ControllerEvent $event)
  40.     {
  41.         $request $event->getRequest();
  42.         if (strtoupper($request->getMethod()) === 'POST' && $event->getController()[0] instanceof AuthController && $event->getController()[1] === 'login') {
  43.             $criteria =  new Criteria();
  44.             $username =  (string) $request->request->get('username');
  45.             $password = (string) $request->request->get('password');
  46.             if (filter_var($usernameFILTER_VALIDATE_EMAIL)) {
  47.                 $criteria->addFilter(new EqualsFilter('email'$username));
  48.             }else{
  49.                 $criteria->addFilter(new EqualsFilter('customerNumber'$username));
  50.             }
  51.             $customer $this->customerRepository->search($criteriaContext::createDefaultContext())->first();
  52.             if ($customer instanceof CustomerEntity && password_verify($password$customer->getPassword())) {
  53.                 $loginData = [
  54.                     'customer_id' => $customer->getId()
  55.                 ];
  56.                 $this->loginProtokollRepository->create([$loginData], Context::createDefaultContext());
  57.             }
  58.         }
  59.     }
  60.     public function onLogin(CustomerLoginEvent $event)
  61.     {
  62.         $this->session->getFlashBag()->clear();
  63.     }
  64.     public function onCustomerAclRolesEvent(CustomerAclRolesEvent $event)
  65.     {
  66.         // add all in this plugin used customer acl roles
  67.         $event->addRoles([
  68.             [
  69.                 'title' => 'ALLOW_DOWNLOAD_LOGIN_PROTOKOLL_PDF',
  70.                 'description' => 'Kunde kann im Profil unter "Meine Vertriebspartner" einen Login-Protokoll für die Mitarbeiter des Partners im PDF-Format erzeugen und downloaden.'
  71.             ]
  72.         ]);
  73.     }
  74. }