Account.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Controller\Plugin;
  7. use Magento\Customer\Model\Session;
  8. use Magento\Framework\App\ActionInterface;
  9. use Magento\Framework\App\RequestInterface;
  10. use Magento\Framework\App\ResponseInterface;
  11. use Magento\Framework\App\Action\AbstractAction;
  12. use Magento\Framework\Controller\ResultInterface;
  13. class Account
  14. {
  15. /**
  16. * @var Session
  17. */
  18. protected $session;
  19. /**
  20. * @var array
  21. */
  22. private $allowedActions = [];
  23. /**
  24. * @param Session $customerSession
  25. * @param array $allowedActions List of actions that are allowed for not authorized users
  26. */
  27. public function __construct(
  28. Session $customerSession,
  29. array $allowedActions = []
  30. ) {
  31. $this->session = $customerSession;
  32. $this->allowedActions = $allowedActions;
  33. }
  34. /**
  35. * Dispatch actions allowed for not authorized users
  36. *
  37. * @param AbstractAction $subject
  38. * @param RequestInterface $request
  39. * @return void
  40. */
  41. public function beforeDispatch(AbstractAction $subject, RequestInterface $request)
  42. {
  43. $action = strtolower($request->getActionName());
  44. $pattern = '/^(' . implode('|', $this->allowedActions) . ')$/i';
  45. if (!preg_match($pattern, $action)) {
  46. if (!$this->session->authenticate()) {
  47. $subject->getActionFlag()->set('', ActionInterface::FLAG_NO_DISPATCH, true);
  48. }
  49. } else {
  50. $this->session->setNoReferer(true);
  51. }
  52. }
  53. /**
  54. * Remove No-referer flag from customer session
  55. *
  56. * @param AbstractAction $subject
  57. * @param ResponseInterface|ResultInterface $result
  58. * @param RequestInterface $request
  59. * @return ResponseInterface|ResultInterface
  60. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  61. */
  62. public function afterDispatch(AbstractAction $subject, $result, RequestInterface $request)
  63. {
  64. $this->session->unsNoReferer(false);
  65. return $result;
  66. }
  67. }