<?php
namespace Plugin\MembersOnlyUserData4;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Eccube\Event\TemplateEvent;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Eccube\Common\EccubeConfig;
use Eccube\Repository\PageRepository;
use Eccube\Entity\Page;
class Event implements EventSubscriberInterface
{
/**
*
* @var type
*/
private $container;
/**
* @var AuthorizationCheckerInterface
*/
private $authorizationChecker;
/**
*
* @var RequestStack
*/
private $requestStack;
/**
*
* @var EccubeConfig
*/
private $eccubeConfig;
/**
*
* @var PageRepository
*/
private $pageRepository;
public function __construct(
ContainerInterface $container,
AuthorizationCheckerInterface $authorizationChecker,
RequestStack $requestStack,
EccubeConfig $eccubeConfig,
PageRepository $pageRepository
)
{
$this->container = $container;
$this->authorizationChecker = $authorizationChecker;
$this->requestStack = $requestStack;
$this->eccubeConfig = $eccubeConfig;
$this->pageRepository = $pageRepository;
}
/**
* @return array
*/
public static function getSubscribedEvents()
{
return [
KernelEvents::RESPONSE => 'onKernelResponse',
'Mypage/login.twig' => 'onTemplateMypageLogin',
];
}
public function onKernelResponse(FilterResponseEvent $event)
{
$request = $this->requestStack->getCurrentRequest();
$adminRoute = $this->eccubeConfig->get('eccube_admin_route');
$path = rawurldecode($request->getPathInfo());
if (preg_match("/^\/({$adminRoute})/i", $path)) {
return;
}
if ($route = $request->get("route")) {
$Page = $this->pageRepository->findOneBy(
[
'url' => $route,
'edit_type' => Page::EDIT_TYPE_USER,
]
);
if ($Page->isPlgMembersOnlyUserData4()) {
if ($this->authorizationChecker->isGranted('ROLE_USER'))
return;
$this->container->get('session')->getFlashBag()->set('members_only_user_data4.mypage.login.message', trans(
'members_only_user_data4.mypage.login.message', [
'PageName' => $Page->getName()]));
$event->setResponse(
new RedirectResponse($this->container->get('router')->generate('mypage_login'))
);
}
}
}
public function onTemplateMypageLogin(TemplateEvent $event)
{
if ($this->container->get('session')->getFlashBag()->has('members_only_user_data4.mypage.login.message')) {
$event->addSnippet('@MembersOnlyUserData4/default/Mypage/login_message.twig');
}
}
}