src/Eccube/Controller/AbstractController.php line 217

Open in your IDE?
  1. <?php
  2. /*
  3. * This file is part of EC-CUBE
  4. *
  5. * Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
  6. *
  7. * http://www.ec-cube.co.jp/
  8. *
  9. * For the full copyright and license information, please view the LICENSE
  10. * file that was distributed with this source code.
  11. */
  12. namespace Eccube\Controller;
  13. use Doctrine\ORM\EntityManagerInterface;
  14. use Eccube\Common\Constant;
  15. use Eccube\Common\EccubeConfig;
  16. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController as Controller;
  17. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  18. use Symfony\Component\Form\FormFactoryInterface;
  19. use Symfony\Component\HttpFoundation\Request;
  20. use Symfony\Component\HttpFoundation\Session\Session;
  21. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  22. use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
  23. use Symfony\Component\Routing\Exception\RouteNotFoundException;
  24. use Symfony\Contracts\Translation\TranslatorInterface;
  25. class AbstractController extends Controller
  26. {
  27. /**
  28. * @var EccubeConfig
  29. */
  30. protected $eccubeConfig;
  31. /**
  32. * @var EntityManagerInterface
  33. */
  34. protected $entityManager;
  35. /**
  36. * @var TranslatorInterface
  37. */
  38. protected $translator;
  39. /**
  40. * @var FormFactoryInterface
  41. */
  42. protected $formFactory;
  43. /**
  44. * @var EventDispatcherInterface
  45. */
  46. protected $eventDispatcher;
  47. /**
  48. * @var Session
  49. */
  50. protected $session;
  51. /**
  52. * @param EccubeConfig $eccubeConfig
  53. * @required
  54. */
  55. public function setEccubeConfig(EccubeConfig $eccubeConfig)
  56. {
  57. $this->eccubeConfig = $eccubeConfig;
  58. }
  59. /**
  60. * @param EntityManagerInterface $entityManager
  61. * @required
  62. */
  63. public function setEntityManager(EntityManagerInterface $entityManager)
  64. {
  65. $this->entityManager = $entityManager;
  66. }
  67. /**
  68. * @param TranslatorInterface $translator
  69. * @required
  70. */
  71. public function setTranslator(TranslatorInterface $translator)
  72. {
  73. $this->translator = $translator;
  74. }
  75. /**
  76. * @param SessionInterface $session
  77. * @required
  78. */
  79. public function setSession(SessionInterface $session)
  80. {
  81. $this->session = $session;
  82. }
  83. /**
  84. * @param FormFactoryInterface $formFactory
  85. * @required
  86. */
  87. public function setFormFactory(FormFactoryInterface $formFactory)
  88. {
  89. $this->formFactory = $formFactory;
  90. }
  91. /**
  92. * @param EventDispatcherInterface $eventDispatcher
  93. * @required
  94. */
  95. public function setEventDispatcher(EventDispatcherInterface $eventDispatcher)
  96. {
  97. $this->eventDispatcher = $eventDispatcher;
  98. }
  99. public function addSuccess($message, $namespace = 'front')
  100. {
  101. $this->addFlash('eccube.'.$namespace.'.success', $message);
  102. }
  103. public function addSuccessOnce($message, $namespace = 'front')
  104. {
  105. $this->addFlashOnce('eccube.'.$namespace.'.success', $message);
  106. }
  107. public function addError($message, $namespace = 'front')
  108. {
  109. $this->addFlash('eccube.'.$namespace.'.error', $message);
  110. }
  111. public function addErrorOnce($message, $namespace = 'front')
  112. {
  113. $this->addFlashOnce('eccube.'.$namespace.'.error', $message);
  114. }
  115. public function addDanger($message, $namespace = 'front')
  116. {
  117. $this->addFlash('eccube.'.$namespace.'.danger', $message);
  118. }
  119. public function addDangerOnce($message, $namespace = 'front')
  120. {
  121. $this->addFlashOnce('eccube.'.$namespace.'.danger', $message);
  122. }
  123. public function addWarning($message, $namespace = 'front')
  124. {
  125. $this->addFlash('eccube.'.$namespace.'.warning', $message);
  126. }
  127. public function addWarningOnce($message, $namespace = 'front')
  128. {
  129. $this->addFlashOnce('eccube.'.$namespace.'.warning', $message);
  130. }
  131. public function addInfo($message, $namespace = 'front')
  132. {
  133. $this->addFlash('eccube.'.$namespace.'.info', $message);
  134. }
  135. public function addInfoOnce($message, $namespace = 'front')
  136. {
  137. $this->addFlashOnce('eccube.'.$namespace.'.info', $message);
  138. }
  139. public function addRequestError($message, $namespace = 'front')
  140. {
  141. $this->addFlash('eccube.'.$namespace.'.request.error', $message);
  142. }
  143. public function addRequestErrorOnce($message, $namespace = 'front')
  144. {
  145. $this->addFlashOnce('eccube.'.$namespace.'.request.error', $message);
  146. }
  147. public function clearMessage()
  148. {
  149. $this->session->getFlashBag()->clear();
  150. }
  151. public function deleteMessage()
  152. {
  153. $this->clearMessage();
  154. $this->addWarning('admin.common.delete_error_already_deleted', 'admin');
  155. }
  156. public function hasMessage(string $type): bool
  157. {
  158. return $this->session->getFlashBag()->has($type);
  159. }
  160. public function addFlashOnce(string $type, $message): void
  161. {
  162. if (!$this->hasMessage($type)) {
  163. $this->addFlash($type, $message);
  164. }
  165. }
  166. /**
  167. * {@inheritdoc}
  168. */
  169. protected function addFlash(string $type, $message): void
  170. {
  171. try {
  172. parent::addFlash($type, $message);
  173. } catch (\LogicException $e) {
  174. // fallback session
  175. $this->session->getFlashBag()->add($type, $message);
  176. }
  177. }
  178. /**
  179. * @param string $targetPath
  180. */
  181. public function setLoginTargetPath($targetPath, $namespace = null)
  182. {
  183. if (is_null($namespace)) {
  184. $this->session->getFlashBag()->set('eccube.login.target.path', $targetPath);
  185. } else {
  186. $this->session->getFlashBag()->set('eccube.'.$namespace.'.login.target.path', $targetPath);
  187. }
  188. }
  189. /**
  190. * Forwards the request to another controller.
  191. *
  192. * @param string $route The name of the route
  193. * @param array $path An array of path parameters
  194. * @param array $query An array of query parameters
  195. *
  196. * @return \Symfony\Component\HttpFoundation\Response A Response instance
  197. */
  198. public function forwardToRoute($route, array $path = [], array $query = [])
  199. {
  200. $Route = $this->get('router')->getRouteCollection()->get($route);
  201. if (!$Route) {
  202. throw new RouteNotFoundException(sprintf('The named route "%s" as such route does not exist.', $route));
  203. }
  204. return $this->forward($Route->getDefault('_controller'), $path, $query);
  205. }
  206. /**
  207. * Checks the validity of a CSRF token.
  208. *
  209. * if token is invalid, throws AccessDeniedHttpException.
  210. *
  211. * @return bool
  212. *
  213. * @throws AccessDeniedHttpException
  214. */
  215. protected function isTokenValid()
  216. {
  217. /** @var Request $request */
  218. $request = $this->container->get('request_stack')->getCurrentRequest();
  219. $token = $request->get(Constant::TOKEN_NAME)
  220. ? $request->get(Constant::TOKEN_NAME)
  221. : $request->headers->get('ECCUBE-CSRF-TOKEN');
  222. if (!$this->isCsrfTokenValid(Constant::TOKEN_NAME, $token)) {
  223. throw new AccessDeniedHttpException('CSRF token is invalid.');
  224. }
  225. return true;
  226. }
  227. }