Example #1
0
 public function testSendVaryEmptyData()
 {
     $this->contextMock->expects($this->once())->method('getVaryString')->willReturn(null);
     $this->cookieMetadataFactoryMock->expects($this->never())->method('createSensitiveCookieMetadata');
     $this->requestMock->expects($this->once())->method('get')->willReturn(null);
     $this->model->sendVary();
 }
Example #2
0
 /**
  * Handle private content version cookie
  * Set cookie if it is not set.
  * Increment version on post requests.
  * In all other cases do nothing.
  *
  * @return void
  */
 public function process()
 {
     if ($this->request->isPost()) {
         $publicCookieMetadata = $this->cookieMetadataFactory->createPublicCookieMetadata()->setDuration(self::COOKIE_PERIOD)->setPath('/')->setHttpOnly(false);
         $this->cookieManager->setPublicCookie(self::COOKIE_NAME, $this->generateValue(), $publicCookieMetadata);
     }
 }
Example #3
0
 /**
  * Delete frontend session cookie if customer session is expired
  *
  * @param SessionManager $sessionManager
  */
 public function beforeStart(SessionManager $sessionManager)
 {
     if (!$this->cookieManager->getCookie($sessionManager->getName()) && $this->cookieManager->getCookie('mage-cache-sessid')) {
         $metadata = $this->cookieMetadataFactory->createCookieMetadata();
         $metadata->setPath('/');
         $this->cookieManager->deleteCookie('mage-cache-sessid', $metadata);
     }
 }
 /**
  * Set Cookie for msg box when it displays first
  *
  * @param FrontController $subject
  * @param \Magento\Framework\App\ResponseInterface|\Magento\Framework\Controller\ResultInterface $result
  *
  * @return \Magento\Framework\App\ResponseInterface|\Magento\Framework\Controller\ResultInterface
  * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  */
 public function afterDispatch(FrontController $subject, $result)
 {
     if ($this->request->isPost() && $this->messageManager->hasMessages()) {
         $publicCookieMetadata = $this->cookieMetadataFactory->createPublicCookieMetadata()->setDuration(self::COOKIE_PERIOD)->setPath('/')->setHttpOnly(false);
         $this->cookieManager->setPublicCookie(self::COOKIE_NAME, 1, $publicCookieMetadata);
     }
     return $result;
 }
 /**
  * @param string $formKey
  * @return void
  */
 private function updateCookieFormKey($formKey)
 {
     $cookieMetadata = $this->cookieMetadataFactory->createPublicCookieMetadata();
     $cookieMetadata->setDomain($this->sessionConfig->getCookieDomain());
     $cookieMetadata->setPath($this->sessionConfig->getCookiePath());
     $cookieMetadata->setDuration($this->sessionConfig->getCookieLifetime());
     $this->cookieFormKey->set($formKey, $cookieMetadata);
 }
Example #6
0
 public function testSendVaryEmptyData()
 {
     $expectedCookieName = Http::COOKIE_VARY_STRING;
     $cookieMetadataMock = $this->getMock('Magento\\Framework\\Stdlib\\Cookie\\CookieMetadata');
     $cookieMetadataMock->expects($this->once())->method('setPath')->with('/')->will($this->returnSelf());
     $this->cookieMetadataFactoryMock->expects($this->once())->method('createCookieMetadata')->with()->will($this->returnValue($cookieMetadataMock));
     $this->cookieManagerMock->expects($this->once())->method('deleteCookie')->with($expectedCookieName, $cookieMetadataMock);
     $this->model->sendVary();
 }
Example #7
0
 /**
  * @param ResultInterface $subject
  * @param ResultInterface $result
  * @return ResultInterface
  */
 public function afterRenderResult(ResultInterface $subject, ResultInterface $result)
 {
     if (!$subject instanceof Json) {
         $publicCookieMetadata = $this->cookieMetadataFactory->createPublicCookieMetadata();
         $publicCookieMetadata->setDurationOneYear();
         $publicCookieMetadata->setPath('/');
         $publicCookieMetadata->setHttpOnly(false);
         $this->cookieManager->setPublicCookie(self::MESSAGES_COOKIES_NAME, $this->jsonHelper->jsonEncode($this->getMessages()), $publicCookieMetadata);
     }
     return $result;
 }
 /**
  * @param bool $isPost
  * @param int $numOfCalls
  * @dataProvider afterDispatchTestDataProvider
  */
 public function testAfterDispatch($isPost, $numOfCalls)
 {
     $this->messageManagerMock->expects($this->exactly($numOfCalls))->method('hasMessages')->will($this->returnValue(true));
     $this->requestMock->expects($this->once())->method('isPost')->will($this->returnValue($isPost));
     $this->cookieMetadataFactoryMock->expects($this->exactly($numOfCalls))->method('createPublicCookieMetadata')->will($this->returnValue($this->publicCookieMetadataMock));
     $this->publicCookieMetadataMock->expects($this->exactly($numOfCalls))->method('setDuration')->with(MessageBox::COOKIE_PERIOD)->will($this->returnValue($this->publicCookieMetadataMock));
     $this->publicCookieMetadataMock->expects($this->exactly($numOfCalls))->method('setPath')->with('/')->will($this->returnValue($this->publicCookieMetadataMock));
     $this->publicCookieMetadataMock->expects($this->exactly($numOfCalls))->method('setHttpOnly')->with(false)->will($this->returnValue($this->publicCookieMetadataMock));
     $this->cookieManagerMock->expects($this->exactly($numOfCalls))->method('setPublicCookie')->with(MessageBox::COOKIE_NAME, 1, $this->publicCookieMetadataMock);
     $this->assertSame($this->responseMock, $this->msgBox->afterDispatch($this->objectMock, $this->responseMock));
 }
 /**
  * @param bool $result
  * @param string $callCount
  * @return void
  * @dataProvider testBeforeStartDataProvider
  */
 public function testBeforeStart($result, $callCount)
 {
     $phpSessionCookieName = 'PHPSESSID';
     $frontendSessionCookieName = 'mage-cache-sessid';
     $this->sessionManager->expects($this->once())->method('getName')->willReturn($phpSessionCookieName);
     $this->cookieManager->expects($this->exactly(2))->method('getCookie')->withConsecutive([$phpSessionCookieName], [$frontendSessionCookieName])->willReturnOnConsecutiveCalls(false, $result);
     $this->metadataFactory->expects($this->{$callCount}())->method('createCookieMetadata')->willReturn($this->metadata);
     $this->metadata->expects($this->{$callCount}())->method('setPath')->with('/');
     $this->cookieManager->expects($this->{$callCount}())->method('deleteCookie')->with('mage-cache-sessid', $this->metadata);
     $this->plugin->beforeStart($this->sessionManager);
 }
Example #10
0
 /**
  * Send Vary cookie
  *
  * @return void
  */
 public function sendVary()
 {
     $varyString = $this->context->getVaryString();
     if ($varyString) {
         $sensitiveCookMetadata = $this->cookieMetadataFactory->createSensitiveCookieMetadata()->setPath('/');
         $this->cookieManager->setSensitiveCookie(self::COOKIE_VARY_STRING, $varyString, $sensitiveCookMetadata);
     } elseif ($this->request->get(self::COOKIE_VARY_STRING)) {
         $cookieMetadata = $this->cookieMetadataFactory->createSensitiveCookieMetadata()->setPath('/');
         $this->cookieManager->deleteCookie(self::COOKIE_VARY_STRING, $cookieMetadata);
     }
 }
Example #11
0
 /**
  * @dataProvider processProvider
  * @param bool $isPost
  */
 public function testProcess($isPost)
 {
     $this->requestMock->expects($this->once())->method('isPost')->will($this->returnValue($isPost));
     if ($isPost) {
         $publicCookieMetadataMock = $this->getMock('Magento\\Framework\\Stdlib\\Cookie\\PublicCookieMetadata');
         $publicCookieMetadataMock->expects($this->once())->method('setPath')->with('/')->will($this->returnSelf());
         $publicCookieMetadataMock->expects($this->once())->method('setDuration')->with(Version::COOKIE_PERIOD)->will($this->returnSelf());
         $this->cookieMetadataFactoryMock->expects($this->once())->method('createPublicCookieMetadata')->with()->will($this->returnValue($publicCookieMetadataMock));
         $this->cookieManagerMock->expects($this->once())->method('setPublicCookie');
     }
     $this->version->process();
 }
Example #12
0
 /**
  * Send Vary cookie
  *
  * @return void
  */
 public function sendVary()
 {
     $data = $this->context->getData();
     if (!empty($data)) {
         ksort($data);
         $cookieValue = sha1(serialize($data));
         $sensitiveCookMetadata = $this->cookieMetadataFactory->createSensitiveCookieMetadata()->setPath('/');
         $this->cookieManager->setSensitiveCookie(self::COOKIE_VARY_STRING, $cookieValue, $sensitiveCookMetadata);
     } else {
         $cookieMetadata = $this->cookieMetadataFactory->createCookieMetadata()->setPath('/');
         $this->cookieManager->deleteCookie(self::COOKIE_VARY_STRING, $cookieMetadata);
     }
 }
Example #13
0
 public function testDelete()
 {
     $cookiePath = '/';
     $cookieDomain = 'example.com';
     /** @var PublicCookieMetadata|\PHPUnit_Framework_MockObject_MockObject $metadata */
     $metadata = $this->getMockBuilder('Magento\\Framework\\Stdlib\\Cookie\\PublicCookieMetadata')->disableOriginalConstructor()->getMock();
     $this->cookieMetadataFactory->expects(static::once())->method('createCookieMetadata')->willReturn($metadata);
     $this->sessionManager->expects(static::once())->method('getCookiePath')->willReturn($cookiePath);
     $metadata->expects(static::once())->method('setPath')->with($cookiePath)->willReturnSelf();
     $this->sessionManager->expects(static::once())->method('getCookieDomain')->willReturn($cookieDomain);
     $metadata->expects(static::once())->method('setDomain')->with($cookieDomain)->willReturnSelf();
     $this->cookieManagerMock->expects(static::once())->method('deleteCookie')->with(FormKey::COOKIE_NAME, $metadata);
     $this->formKey->delete();
 }
 public function testAfterRenderResultWithWrongArray()
 {
     $messageType = 'message1type';
     $messageText = 'message1text';
     $messages = [['type' => $messageType, 'text' => $messageText]];
     /** @var Redirect|\PHPUnit_Framework_MockObject_MockObject $resultMock */
     $resultMock = $this->getMockBuilder(Redirect::class)->disableOriginalConstructor()->getMock();
     /** @var PublicCookieMetadata|\PHPUnit_Framework_MockObject_MockObject $cookieMetadataMock */
     $cookieMetadataMock = $this->getMockBuilder(PublicCookieMetadata::class)->disableOriginalConstructor()->getMock();
     $this->cookieMetadataFactoryMock->expects($this->once())->method('createPublicCookieMetadata')->willReturn($cookieMetadataMock);
     $this->cookieManagerMock->expects($this->once())->method('setPublicCookie')->with(MessagePlugin::MESSAGES_COOKIES_NAME, \Zend_Json::encode($messages), $cookieMetadataMock);
     $this->cookieManagerMock->expects($this->once())->method('getCookie')->with(MessagePlugin::MESSAGES_COOKIES_NAME, \Zend_Json::encode([]))->willReturn(\Zend_Json::encode('string'));
     $this->dataMock->expects($this->any())->method('jsonDecode')->willReturnCallback(function ($data) {
         return \Zend_Json::decode($data);
     });
     $this->dataMock->expects($this->any())->method('jsonEncode')->willReturnCallback(function ($data) {
         return \Zend_Json::encode($data);
     });
     /** @var MessageInterface|\PHPUnit_Framework_MockObject_MockObject $messageMock */
     $messageMock = $this->getMockBuilder(MessageInterface::class)->getMock();
     $messageMock->expects($this->once())->method('getType')->willReturn($messageType);
     $this->interpretationStrategyMock->expects($this->once())->method('interpret')->with($messageMock)->willReturn($messageText);
     /** @var Collection|\PHPUnit_Framework_MockObject_MockObject $collectionMock */
     $collectionMock = $this->getMockBuilder(Collection::class)->disableOriginalConstructor()->getMock();
     $collectionMock->expects($this->once())->method('getItems')->willReturn([$messageMock]);
     $this->managerMock->expects($this->once())->method('getMessages')->with(true, null)->willReturn($collectionMock);
     $this->assertEquals($resultMock, $this->model->afterRenderResult($resultMock, $resultMock));
 }
Example #15
0
 public function testExecute()
 {
     $customerId = 1;
     $refererUrl = 'http://referer.url';
     $this->sessionMock->expects($this->once())->method('getId')->willReturn($customerId);
     $this->sessionMock->expects($this->once())->method('logout')->willReturnSelf();
     $this->redirect->expects($this->once())->method('getRefererUrl')->willReturn($refererUrl);
     $this->sessionMock->expects($this->once())->method('setBeforeAuthUrl')->with($refererUrl)->willReturnSelf();
     $this->sessionMock->expects($this->once())->method('setLastCustomerId')->with($customerId);
     $this->cookieManager->expects($this->once())->method('getCookie')->with('mage-cache-sessid')->willReturn(true);
     $this->cookieMetadataFactory->expects($this->once())->method('createCookieMetadata')->willReturn($this->cookieMetadata);
     $this->cookieMetadata->expects($this->once())->method('setPath')->with('/');
     $this->cookieManager->expects($this->once())->method('deleteCookie')->with('mage-cache-sessid', $this->cookieMetadata);
     $this->redirectFactory->expects($this->once())->method('create')->willReturn($this->resultRedirect);
     $this->resultRedirect->expects($this->once())->method('setPath')->with('*/*/logoutSuccess');
     $this->assertSame($this->resultRedirect, $this->controller->execute());
 }
 public function testExecute()
 {
     $formKey = 'form_key';
     $escapedFormKey = 'escaped_form_key';
     $cookieDomain = 'example.com';
     $cookiePath = '/';
     $cookieLifetime = 3600;
     $cookieMetadata = $this->getMockBuilder('Magento\\Framework\\Stdlib\\Cookie\\PublicCookieMetadata')->disableOriginalConstructor()->getMock();
     $this->cookieFormKey->expects(static::any())->method('get')->willReturn($formKey);
     $this->cookieMetadataFactory->expects(static::once())->method('createPublicCookieMetadata')->willReturn($cookieMetadata);
     $this->sessionConfig->expects(static::once())->method('getCookieDomain')->willReturn($cookieDomain);
     $cookieMetadata->expects(static::once())->method('setDomain')->with($cookieDomain);
     $this->sessionConfig->expects(static::once())->method('getCookiePath')->willReturn($cookiePath);
     $cookieMetadata->expects(static::once())->method('setPath')->with($cookiePath);
     $this->sessionConfig->expects(static::once())->method('getCookieLifetime')->willReturn($cookieLifetime);
     $cookieMetadata->expects(static::once())->method('setDuration')->with($cookieLifetime);
     $this->cookieFormKey->expects(static::once())->method('set')->with($formKey, $cookieMetadata);
     $this->escaper->expects(static::once())->method('escapeHtml')->with($formKey)->willReturn($escapedFormKey);
     $this->sessionFormKey->expects(static::once())->method('set')->with($escapedFormKey);
     $this->observer->execute($this->observerMock);
 }
 /**
  * Expire the session cookie
  *
  * Sends a session cookie with no value, and with an expiry in the past.
  *
  * @return void
  */
 public function expireSessionCookie()
 {
     if (!$this->sessionConfig->getUseCookies()) {
         return;
     }
     $metadata = $this->cookieMetadataFactory->createPublicCookieMetadata();
     $metadata->setPath($this->sessionConfig->getCookiePath());
     $metadata->setDomain($this->sessionConfig->getCookieDomain());
     $metadata->setSecure($this->sessionConfig->getCookieSecure());
     $metadata->setHttpOnly($this->sessionConfig->getCookieHttpOnly());
     $this->cookieManager->deleteCookie($this->getName(), $metadata);
     $this->clearSubDomainSessionCookie();
 }
Example #18
0
 public function testLoadValidOrderStoredCookie()
 {
     $this->sessionMock->expects($this->once())->method('isLoggedIn')->will($this->returnValue(false));
     $orderMock = $this->getMock('Magento\\Sales\\Model\\Order', ['getProtectCode', 'loadByIncrementId', 'getId', 'getBillingAddress', '__wakeup'], [], '', false);
     $protectedCode = 'protectedCode';
     $incrementId = 1;
     $cookieData = $protectedCode . ':' . $incrementId;
     $cookieDataHash = base64_encode($cookieData);
     $this->orderFactoryMock->expects($this->once())->method('create')->will($this->returnValue($orderMock));
     $this->cookieManagerMock->expects($this->once())->method('getCookie')->with(Guest::COOKIE_NAME)->will($this->returnValue($cookieDataHash));
     $orderMock->expects($this->once())->method('loadByIncrementId')->with($incrementId);
     $orderMock->expects($this->exactly(1))->method('getId')->will($this->returnValue($incrementId));
     $orderMock->expects($this->once())->method('getProtectCode')->will($this->returnValue($protectedCode));
     $metaData = new \Magento\Framework\Stdlib\Cookie\PublicCookieMetadata();
     $this->cookieMetadataFactoryMock->expects($this->once())->method('createPublicCookieMetadata')->will($this->returnValue($metaData));
     $this->cookieManagerMock->expects($this->once())->method('setPublicCookie')->with(Guest::COOKIE_NAME, $this->anything(), $metaData);
     $requestMock = $this->getMock('Magento\\Framework\\App\\Request\\Http', [], [], '', false);
     $responseMock = $this->getMock('Magento\\Framework\\App\\Response\\Http', [], [], '', false);
     $this->assertTrue($this->guest->loadValidOrder($requestMock, $responseMock));
 }
Example #19
0
 public function testLoadValidOrderStoredCookie()
 {
     $protectedCode = 'protectedCode';
     $incrementId = 1;
     $cookieData = $protectedCode . ':' . $incrementId;
     $cookieDataHash = base64_encode($cookieData);
     $this->sessionMock->expects($this->once())->method('isLoggedIn')->willReturn(false);
     $this->orderFactoryMock->expects($this->once())->method('create')->willReturn($this->salesOrderMock);
     $this->cookieManagerMock->expects($this->once())->method('getCookie')->with(Guest::COOKIE_NAME)->willReturn($cookieDataHash);
     $this->salesOrderMock->expects($this->once())->method('loadByIncrementId')->with($incrementId)->willReturnSelf();
     $this->salesOrderMock->expects($this->exactly(1))->method('getId')->willReturn($incrementId);
     $this->salesOrderMock->expects($this->once())->method('getProtectCode')->willReturn($protectedCode);
     $metaDataMock = $this->getMock('Magento\\Framework\\Stdlib\\Cookie\\PublicCookieMetadata', [], [], '', false);
     $metaDataMock->expects($this->once())->method('setPath')->with(Guest::COOKIE_PATH)->willReturnSelf();
     $metaDataMock->expects($this->once())->method('setHttpOnly')->with(true)->willReturnSelf();
     $this->cookieMetadataFactoryMock->expects($this->once())->method('createPublicCookieMetadata')->willReturn($metaDataMock);
     $this->cookieManagerMock->expects($this->once())->method('setPublicCookie')->with(Guest::COOKIE_NAME, $this->anything(), $metaDataMock);
     $requestMock = $this->getMock('Magento\\Framework\\App\\Request\\Http', [], [], '', false);
     $this->assertTrue($this->guest->loadValidOrder($requestMock));
 }
Example #20
0
 /**
  * Set persistent shopping cart cookie.
  *
  * @param string $value
  * @param int $duration
  * @param string $path
  * @return void
  */
 private function setCookie($value, $duration, $path)
 {
     $publicCookieMetadata = $this->_cookieMetadataFactory->createPublicCookieMetadata()->setDuration($duration)->setPath($path)->setHttpOnly(true);
     $this->_cookieManager->setPublicCookie(self::COOKIE_NAME, $value, $publicCookieMetadata);
 }
Example #21
0
 /**
  * Perform custom url rewrites
  *
  * @param \Magento\Framework\App\RequestInterface $request
  * @return bool
  */
 public function rewrite(\Magento\Framework\App\RequestInterface $request = null)
 {
     if (!$this->_appState->isInstalled()) {
         return false;
     }
     if (is_null($this->getStoreId()) || false === $this->getStoreId()) {
         $this->setStoreId($this->_storeManager->getStore()->getId());
     }
     /**
      * We have two cases of incoming paths - with and without slashes at the end ("/somepath/" and "/somepath").
      * Each of them matches two url rewrite request paths - with and without slashes at the end
      * ("/somepath/" and "/somepath").
      * Choose any matched rewrite, but in priority order that depends on same presence of slash and query params.
      */
     $requestCases = array();
     $pathInfo = $request->getPathInfo();
     $origSlash = substr($pathInfo, -1) == '/' ? '/' : '';
     $requestPath = trim($pathInfo, '/');
     // If there were final slash - add nothing to less priority paths. And vice versa.
     $altSlash = $origSlash ? '' : '/';
     $queryString = $this->_getQueryString();
     // Query params in request, matching "path + query" has more priority
     if ($queryString) {
         $requestCases[] = $requestPath . $origSlash . '?' . $queryString;
         $requestCases[] = $requestPath . $altSlash . '?' . $queryString;
     }
     $requestCases[] = $requestPath . $origSlash;
     $requestCases[] = $requestPath . $altSlash;
     $this->loadByRequestPath($requestCases);
     $targetUrl = $request->getBaseUrl();
     /**
      * Try to find rewrite by request path at first, if no luck - try to find by id_path
      */
     if (!$this->getId() && isset($_GET['___from_store'])) {
         try {
             $fromStoreId = $this->_storeManager->getStore($_GET['___from_store'])->getId();
         } catch (\Exception $e) {
             return false;
         }
         $this->setStoreId($fromStoreId)->loadByRequestPath($requestCases);
         if (!$this->getId()) {
             return false;
         }
         $currentStore = $this->_storeManager->getStore();
         $this->setStoreId($currentStore->getId())->loadByIdPath($this->getIdPath());
         $cookieMetadata = $this->_cookieMetadataFactory->createPublicCookieMetadata()->setDurationOneYear();
         $this->_cookieManager->setPublicCookie(\Magento\Store\Model\Store::COOKIE_NAME, $currentStore->getCode(), $cookieMetadata);
         $targetUrl .= '/' . $this->getRequestPath();
         $this->_sendRedirectHeaders($targetUrl, true);
     }
     if (!$this->getId()) {
         return false;
     }
     $request->setAlias(\Magento\Framework\Url::REWRITE_REQUEST_PATH_ALIAS, $this->getRequestPath());
     $external = substr($this->getTargetPath(), 0, 6);
     $isPermanentRedirectOption = $this->hasOption('RP');
     if ($external === 'http:/' || $external === 'https:') {
         $destinationStoreCode = $this->_storeManager->getStore($this->getStoreId())->getCode();
         $cookieMetadata = $this->_cookieMetadataFactory->createPublicCookieMetadata()->setDurationOneYear();
         $this->_cookieManager->setPublicCookie(\Magento\Store\Model\Store::COOKIE_NAME, $destinationStoreCode, $cookieMetadata);
         $this->_sendRedirectHeaders($this->getTargetPath(), $isPermanentRedirectOption);
     } else {
         $targetUrl .= '/' . $this->getTargetPath();
     }
     $isRedirectOption = $this->hasOption('R');
     $isStoreInUrl = $this->_scopeConfig->getValue(\Magento\Store\Model\Store::XML_PATH_STORE_IN_URL, \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
     if ($isRedirectOption || $isPermanentRedirectOption) {
         if ($isStoreInUrl && ($storeCode = $this->_storeManager->getStore()->getCode())) {
             $targetUrl .= '/' . $storeCode . '/' . $this->getTargetPath();
         }
         $this->_sendRedirectHeaders($targetUrl, $isPermanentRedirectOption);
     }
     if ($isStoreInUrl && ($storeCode = $this->_storeManager->getStore()->getCode())) {
         $targetUrl .= '/' . $storeCode . '/' . $this->getTargetPath();
     }
     $queryString = $this->_getQueryString();
     if ($queryString) {
         $targetUrl .= '?' . $queryString;
     }
     $request->setRequestUri($targetUrl);
     $request->setPathInfo($this->getTargetPath());
     return true;
 }
Example #22
0
 /**
  * Delete store cookie.
  *
  * @return $this
  */
 public function deleteCookie()
 {
     $cookieMetadata = $this->_cookieMetadataFactory->createPublicCookieMetadata()->setPath($this->getStorePath());
     $this->_cookieManager->deleteCookie(self::COOKIE_NAME, $cookieMetadata);
     return $this;
 }
 /**
  * {@inheritdoc}
  */
 public function deleteStoreCookie(StoreInterface $store)
 {
     $cookieMetadata = $this->cookieMetadataFactory->createPublicCookieMetadata()->setPath($store->getStorePath());
     $this->cookieManager->deleteCookie(self::COOKIE_NAME, $cookieMetadata);
 }
Example #24
0
 /**
  * @return void
  */
 public function delete()
 {
     $this->cookieManager->deleteCookie(self::COOKIE_NAME, $this->cookieMetadataFactory->createCookieMetadata()->setPath($this->sessionManager->getCookiePath())->setDomain($this->sessionManager->getCookieDomain()));
 }
Example #25
0
 /**
  * Try to load valid order by $_POST or $_COOKIE
  *
  * @param App\RequestInterface $request
  * @param App\ResponseInterface $response
  * @return bool
  * 
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  * @SuppressWarnings(PHPMD.NPathComplexity)
  */
 public function loadValidOrder(App\RequestInterface $request, App\ResponseInterface $response)
 {
     if ($this->customerSession->isLoggedIn()) {
         $response->setRedirect($this->_urlBuilder->getUrl('sales/order/history'));
         return false;
     }
     $post = $request->getPost();
     $errors = false;
     /** @var $order \Magento\Sales\Model\Order */
     $order = $this->orderFactory->create();
     $fromCookie = $this->cookieManager->getCookie(self::COOKIE_NAME);
     if (empty($post) && !$fromCookie) {
         $response->setRedirect($this->_urlBuilder->getUrl('sales/guest/form'));
         return false;
     } elseif (!empty($post) && isset($post['oar_order_id']) && isset($post['oar_type'])) {
         $type = $post['oar_type'];
         $incrementId = $post['oar_order_id'];
         $lastName = $post['oar_billing_lastname'];
         $email = $post['oar_email'];
         $zip = $post['oar_zip'];
         if (empty($incrementId) || empty($lastName) || empty($type) || !in_array($type, array('email', 'zip')) || $type == 'email' && empty($email) || $type == 'zip' && empty($zip)) {
             $errors = true;
         }
         if (!$errors) {
             $order->loadByIncrementId($incrementId);
         }
         $errors = true;
         if ($order->getId()) {
             $billingAddress = $order->getBillingAddress();
             if (strtolower($lastName) == strtolower($billingAddress->getLastname()) && ($type == 'email' && strtolower($email) == strtolower($billingAddress->getEmail()) || $type == 'zip' && strtolower($zip) == strtolower($billingAddress->getPostcode()))) {
                 $errors = false;
             }
         }
         if (!$errors) {
             $toCookie = base64_encode($order->getProtectCode() . ':' . $incrementId);
             $metadata = $this->cookieMetadataFactory->createPublicCookieMetadata();
             $metadata->setPath(self::COOKIE_PATH);
             $metadata->setDuration(self::COOKIE_LIFETIME);
             $this->cookieManager->setPublicCookie(self::COOKIE_NAME, $toCookie, $metadata);
         }
     } elseif ($fromCookie) {
         $cookieData = explode(':', base64_decode($fromCookie));
         $protectCode = isset($cookieData[0]) ? $cookieData[0] : null;
         $incrementId = isset($cookieData[1]) ? $cookieData[1] : null;
         $errors = true;
         if (!empty($protectCode) && !empty($incrementId)) {
             $order->loadByIncrementId($incrementId);
             if ($order->getProtectCode() == $protectCode) {
                 // renew cookie
                 $metadata = $this->cookieMetadataFactory->createPublicCookieMetadata();
                 $metadata->setPath(self::COOKIE_PATH);
                 $metadata->setDuration(self::COOKIE_LIFETIME);
                 $this->cookieManager->setPublicCookie(self::COOKIE_NAME, $fromCookie, $metadata);
                 $errors = false;
             }
         }
     }
     if (!$errors && $order->getId()) {
         $this->coreRegistry->register('current_order', $order);
         return true;
     }
     $this->messageManager->addError(__('You entered incorrect data. Please try again.'));
     $response->setRedirect($this->_urlBuilder->getUrl('sales/guest/form'));
     return false;
 }
Example #26
0
 /**
  * Set guest-view cookie
  *
  * @param string $cookieValue
  * @return void
  */
 private function setGuestViewCookie($cookieValue)
 {
     $metadata = $this->cookieMetadataFactory->createPublicCookieMetadata()->setPath(self::COOKIE_PATH)->setHttpOnly(true);
     $this->cookieManager->setPublicCookie(self::COOKIE_NAME, $cookieValue, $metadata);
 }
 /**
  * Delete debug cookie.
  *
  * @return $this
  */
 public function deleteDebugCookie()
 {
     $cookieMetadata = $this->_cookieMetadataFactory->createPublicCookieMetadata();
     $this->_cookieManager->deleteCookie(self::COOKIE_NAME, $cookieMetadata);
     return $this;
 }
Example #28
0
 /**
  * @param \Magento\Store\Model\StoreManagerInterface $storage
  * @param string $scopeType
  * @return void
  */
 protected function _checkRequestStore(\Magento\Store\Model\StoreManagerInterface $storage, $scopeType)
 {
     $storeCode = $this->request->getParam('___store');
     if (empty($storeCode)) {
         return;
     }
     if (!$this->setCurrentStore($storage, $storeCode, $scopeType)) {
         return;
     }
     if ($storage->getStore()->getCode() == $storeCode) {
         $store = $storage->getStore($storeCode);
         if ($store->getWebsite()->getDefaultStore()->getId() == $store->getId()) {
             $this->_cookieManager->deleteCookie(Store::COOKIE_NAME);
         } else {
             $publicCookieMetadata = $this->cookieMetadataFactory->createPublicCookieMetadata()->setDurationOneYear();
             $this->_cookieManager->setPublicCookie(Store::COOKIE_NAME, $storage->getStore()->getCode(), $publicCookieMetadata);
             $this->_httpContext->setValue(Store::ENTITY, $storage->getStore()->getCode(), \Magento\Store\Model\Store::DEFAULT_CODE);
         }
     }
     return;
 }
Example #29
0
 /**
  * Set store cookie with this store's code for a year.
  *
  * @return $this
  */
 public function setCookie()
 {
     $cookieMetadata = $this->_cookieMetadataFactory->createPublicCookieMetadata()->setHttpOnly(true)->setDurationOneYear();
     $this->_cookieManager->setPublicCookie(self::COOKIE_NAME, $this->getCode(), $cookieMetadata);
     return $this;
 }
Example #30
0
 /**
  * Set current store currency code
  *
  * @param   string $code
  * @return  string
  */
 public function setCurrentCurrencyCode($code)
 {
     $code = strtoupper($code);
     if (in_array($code, $this->getAvailableCurrencyCodes())) {
         $this->_getSession()->setCurrencyCode($code);
         $path = $this->_getSession()->getCookiePath();
         $sensitiveCookieMetadata = $this->_cookieMetadataFactory->createSensitiveCookieMetadata()->setPath($path);
         if ($code == $this->getDefaultCurrency()->getCurrencyCode()) {
             $this->_cookieManager->deleteCookie(self::COOKIE_CURRENCY, $sensitiveCookieMetadata);
         } else {
             $this->_cookieManager->setSensitiveCookie(self::COOKIE_CURRENCY, $code, $sensitiveCookieMetadata);
         }
         $this->_httpContext->setValue(\Magento\Core\Helper\Data::CONTEXT_CURRENCY, $code, $this->_storeManager->getWebsite()->getDefaultStore()->getDefaultCurrency()->getCode());
     }
     return $this;
 }