<?php
declare(strict_types=0);
/*
* WellCommerce Foundation
*
* This file is part of the WellCommerce package.
*
* (c) Adam Piotrowski <adam@wellcommerce.org>, Adrian Potepa <adrian@wellcommerce.org>
*
* For the full copyright and license information,
* please view the LICENSE file that was distributed with this source code.
*/
namespace WellCommerce\Bundle\AppBundle\EventListener;
use Carbon\Carbon;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Event\ControllerEvent;
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
use WellCommerce\Bundle\AppBundle\Entity\Client;
use WellCommerce\Bundle\AppBundle\Entity\Shop;
use WellCommerce\Bundle\AppBundle\Entity\User;
use WellCommerce\Bundle\CoreBundle\DependencyInjection\AbstractServiceSubscriber;
use WellCommerce\Bundle\CoreBundle\Doctrine\Event\EntityEvent;
/**
* Class AdminSubscriber
*
* @author Adam Piotrowski <adam@wellcommerce.org>
*/
class AdminSubscriber extends AbstractServiceSubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
return [
'user.pre_create' => ['onUserPreCreate', 0],
'security.interactive_login' => ['onSecurityInteractiveLogin', 0],
KernelEvents::CONTROLLER => ['onKernelController', 0],
];
}
public function onKernelController(ControllerEvent $event)
{
if ($event->isMasterRequest()) {
$request = $event->getRequest();
$user = $this->getSecurityHelper()->getCurrentAdmin();
if ($request->isMethod('POST')) {
return;
}
if ($user instanceof User) {
if ($user->isSessionExpired()) {
$redirectUrl = $this->getRouterHelper()->generateUrl('admin.user.logout');
$event->setController(function () use ($redirectUrl) {
return new RedirectResponse($redirectUrl);
});
}
$user->setLastActive(Carbon::now()->toDateTimeImmutable());
$user->setSessionExpiresAt(Carbon::now()->addMinutes($user->getSessionLifetime())->toDateTimeImmutable());
$this->getEntityManager()->flush();
}
}
}
public function onSecurityInteractiveLogin(InteractiveLoginEvent $event)
{
$user = $event->getAuthenticationToken()->getUser();
if ($user instanceof User) {
$user->setLastActive(Carbon::now()->toDateTimeImmutable());
$user->setSessionExpiresAt(null);
$shop = $user->getShop();
$this->getEntityManager()->flush();
if ($shop instanceof Shop) {
$this->getRequestHelper()->setSessionAttribute('admin/shop/id', $shop->getId());
}
$this->getRequestHelper()->setSessionAttribute('global_view', 0);
}
}
public function onUserPreCreate(EntityEvent $entityEvent)
{
$password = $this->getSecurityHelper()->generateRandomPassword();
$user = $entityEvent->getEntity();
if ($user instanceof User) {
$user->setPassword($password);
$this->getMailerHelper()->sendEmail([
'recipient' => $user->getEmail(),
'bcc' => [],
'subject' => $this->getTranslatorHelper()->trans('user.email.title.register'),
'template' => 'WellCommerceAppBundle:Admin/Email:register.html.twig',
'parameters' => [
'user' => $user,
'password' => $password,
],
'configuration' => $this->getShopStorage()->getCurrentShop()->getMailerConfiguration(),
]);
}
}
}