<?php
namespace CioLoginProtokoll\Subscriber;
use CioCustomerPermissionGroups\Event\CustomerAclRolesEvent;
use CioCustomerPermissionGroups\Service\CustomerPermissionService;
use Shopware\Administration\Controller\AdministrationController;
use Shopware\Core\Checkout\Customer\CustomerEntity;
use Shopware\Core\Checkout\Customer\Event\CustomerLoginEvent;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Storefront\Controller\AuthController;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpKernel\Event\ControllerEvent;
use Symfony\Component\HttpKernel\KernelEvents;
class LoginProtokollSubscriber implements EventSubscriberInterface
{
private EntityRepository $customerRepository;
private EntityRepository $loginProtokollRepository;
private Session $session;
private CustomerPermissionService $checkCustomerPermissionsService;
public function __construct(EntityRepository $customerRepository, EntityRepository $loginProtokollRepository, Session $session, CustomerPermissionService $checkCustomerPermissionsService)
{
$this->customerRepository = $customerRepository;
$this->loginProtokollRepository = $loginProtokollRepository;
$this->session = $session;
$this->checkCustomerPermissionsService = $checkCustomerPermissionsService;
}
public static function getSubscribedEvents(): array
{
// Return the events to listen to as array like this: <event to listen to> => <method to execute>
return [
KernelEvents::CONTROLLER => 'onLoginProtokoll',
CustomerLoginEvent::class => 'onLogin',
CustomerAclRolesEvent::class => 'onCustomerAclRolesEvent',
];
}
public function onLoginProtokoll(ControllerEvent $event)
{
$request = $event->getRequest();
if (strtoupper($request->getMethod()) === 'POST' && $event->getController()[0] instanceof AuthController && $event->getController()[1] === 'login') {
$criteria = new Criteria();
$username = (string) $request->request->get('username');
$password = (string) $request->request->get('password');
if (filter_var($username, FILTER_VALIDATE_EMAIL)) {
$criteria->addFilter(new EqualsFilter('email', $username));
}else{
$criteria->addFilter(new EqualsFilter('customerNumber', $username));
}
$customer = $this->customerRepository->search($criteria, Context::createDefaultContext())->first();
if ($customer instanceof CustomerEntity && password_verify($password, $customer->getPassword())) {
$loginData = [
'customer_id' => $customer->getId()
];
$this->loginProtokollRepository->create([$loginData], Context::createDefaultContext());
}
}
}
public function onLogin(CustomerLoginEvent $event)
{
$this->session->getFlashBag()->clear();
}
public function onCustomerAclRolesEvent(CustomerAclRolesEvent $event)
{
// add all in this plugin used customer acl roles
$event->addRoles([
[
'title' => 'ALLOW_DOWNLOAD_LOGIN_PROTOKOLL_PDF',
'description' => 'Kunde kann im Profil unter "Meine Vertriebspartner" einen Login-Protokoll für die Mitarbeiter des Partners im PDF-Format erzeugen und downloaden.'
]
]);
}
}