Skip to content

Instantly share code, notes, and snippets.

@xurshid29
Last active August 29, 2015 14:21
Show Gist options
  • Save xurshid29/dcc98e071edcda518164 to your computer and use it in GitHub Desktop.
Save xurshid29/dcc98e071edcda518164 to your computer and use it in GitHub Desktop.
Forces the users log out and leave out the personal cabinet whenever the maintenance mode starts.
<?php
/**
* Created by PhpStorm.
* User: xurshid29
* Date: 12/6/14
* Time: 2:17 PM
*/
namespace SL\CoreBundle\Event\Listener;
use SL\CoreBundle\Entity\AbstractPrice;
use SL\CoreBundle\Entity\User;
use SL\CoreBundle\Manager\CompanyReportingMonthManager;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Translation\TranslatorInterface;
/**
* Class KernelRequestListener
* @package SL\CoreBundle\Event\Listener
*/
class KernelRequestListener
{
/**
* @var CompanyReportingMonthManager;
*/
private $companyReportingMonthManager;
/**
* @var RouterInterface
*/
private $router;
/**
* @var \Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface
*/
private $tokenStorage;
/**
* @var TranslatorInterface
*/
private $translator;
/**
* @param \SL\CoreBundle\Manager\CompanyReportingMonthManager $companyReportingMonthManager
* @param \Symfony\Component\Routing\RouterInterface $router
* @param \Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface $tokenStorage
* @param \Symfony\Component\Translation\TranslatorInterface $translator
*/
public function __construct(CompanyReportingMonthManager $companyReportingMonthManager, RouterInterface $router, TokenStorageInterface $tokenStorage, TranslatorInterface $translator)
{
$this->companyReportingMonthManager = $companyReportingMonthManager;
$this->router = $router;
$this->translator = $translator;
$this->tokenStorage = $tokenStorage;
}
/**
* @param \Symfony\Component\HttpKernel\Event\GetResponseEvent $responseEvent
*/
public function onKernelRequest(GetResponseEvent $responseEvent)
{
if (HttpKernelInterface::MASTER_REQUEST != $responseEvent->getRequestType()) {
return;
}
$currentReportingMonth = $this->companyReportingMonthManager->getCurrent();
if ($this->tokenStorage->getToken()->getUser() instanceof User && $currentReportingMonth->isMaintenanceModeOn()) {
$responseEvent->setResponse(new RedirectResponse($this->router->generate('sl_web_front_security_logout')));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment