DepersonalizePlugin.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. /**
  3. * Depersonalize customer session data
  4. *
  5. * Copyright © Magento, Inc. All rights reserved.
  6. * See COPYING.txt for license details.
  7. */
  8. namespace Magento\Customer\Model\Layout;
  9. use Magento\PageCache\Model\DepersonalizeChecker;
  10. /**
  11. * Class DepersonalizePlugin
  12. */
  13. class DepersonalizePlugin
  14. {
  15. /**
  16. * @var DepersonalizeChecker
  17. */
  18. protected $depersonalizeChecker;
  19. /**
  20. * @var \Magento\Framework\Session\SessionManagerInterface
  21. */
  22. protected $session;
  23. /**
  24. * @var \Magento\Customer\Model\Session
  25. */
  26. protected $customerSession;
  27. /**
  28. * @var \Magento\Customer\Model\CustomerFactory
  29. */
  30. protected $customerFactory;
  31. /**
  32. * @var \Magento\Customer\Model\Visitor
  33. */
  34. protected $visitor;
  35. /**
  36. * @var int
  37. */
  38. protected $customerGroupId;
  39. /**
  40. * @var string
  41. */
  42. protected $formKey;
  43. /**
  44. * @param DepersonalizeChecker $depersonalizeChecker
  45. * @param \Magento\Framework\Session\SessionManagerInterface $session
  46. * @param \Magento\Customer\Model\Session $customerSession
  47. * @param \Magento\Customer\Model\CustomerFactory $customerFactory
  48. * @param \Magento\Customer\Model\Visitor $visitor
  49. */
  50. public function __construct(
  51. DepersonalizeChecker $depersonalizeChecker,
  52. \Magento\Framework\Session\SessionManagerInterface $session,
  53. \Magento\Customer\Model\Session $customerSession,
  54. \Magento\Customer\Model\CustomerFactory $customerFactory,
  55. \Magento\Customer\Model\Visitor $visitor
  56. ) {
  57. $this->session = $session;
  58. $this->customerSession = $customerSession;
  59. $this->customerFactory = $customerFactory;
  60. $this->visitor = $visitor;
  61. $this->depersonalizeChecker = $depersonalizeChecker;
  62. }
  63. /**
  64. * Before generate Xml
  65. *
  66. * @param \Magento\Framework\View\LayoutInterface $subject
  67. * @return array
  68. */
  69. public function beforeGenerateXml(\Magento\Framework\View\LayoutInterface $subject)
  70. {
  71. if ($this->depersonalizeChecker->checkIfDepersonalize($subject)) {
  72. $this->customerGroupId = $this->customerSession->getCustomerGroupId();
  73. $this->formKey = $this->session->getData(\Magento\Framework\Data\Form\FormKey::FORM_KEY);
  74. }
  75. return [];
  76. }
  77. /**
  78. * After generate Xml
  79. *
  80. * @param \Magento\Framework\View\LayoutInterface $subject
  81. * @param \Magento\Framework\View\LayoutInterface $result
  82. * @return \Magento\Framework\View\LayoutInterface
  83. */
  84. public function afterGenerateXml(\Magento\Framework\View\LayoutInterface $subject, $result)
  85. {
  86. if ($this->depersonalizeChecker->checkIfDepersonalize($subject)) {
  87. $this->visitor->setSkipRequestLogging(true);
  88. $this->visitor->unsetData();
  89. $this->session->clearStorage();
  90. $this->customerSession->clearStorage();
  91. $this->session->setData(\Magento\Framework\Data\Form\FormKey::FORM_KEY, $this->formKey);
  92. $this->customerSession->setCustomerGroupId($this->customerGroupId);
  93. $this->customerSession->setCustomer($this->customerFactory->create()->setGroupId($this->customerGroupId));
  94. }
  95. return $result;
  96. }
  97. }