/** * @param Observer $observer * @return void * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ public function execute(Observer $observer) { /** @var $customer \Magento\Customer\Api\Data\CustomerInterface */ $customer = $observer->getEvent()->getCustomer(); // Check if customer is valid (remove persistent cookie for invalid customer) if (!$customer || !$customer->getId() || !$this->_persistentSession->isRememberMeChecked()) { $this->_sessionFactory->create()->removePersistentCookie(); return; } $persistentLifeTime = $this->_persistentData->getLifeTime(); // Delete persistent session, if persistent could not be applied if ($this->_persistentData->isEnabled() && $persistentLifeTime <= 0) { // Remove current customer persistent session $this->_sessionFactory->create()->deleteByCustomerId($customer->getId()); return; } /** @var $sessionModel \Magento\Persistent\Model\Session */ $sessionModel = $this->_persistentSession->getSession(); // Check if session is wrong or not exists, so create new session if (!$sessionModel->getId() || $sessionModel->getCustomerId() != $customer->getId()) { /** @var \Magento\Persistent\Model\Session $sessionModel */ $sessionModel = $this->_sessionFactory->create(); $sessionModel->setLoadExpired()->loadByCustomerId($customer->getId()); if (!$sessionModel->getId()) { /** @var \Magento\Persistent\Model\Session $sessionModel */ $sessionModel = $this->_sessionFactory->create(); $sessionModel->setCustomerId($customer->getId())->save(); } $this->_persistentSession->setSession($sessionModel); } // Set new cookie if ($sessionModel->getId()) { $sessionModel->setPersistentCookie($persistentLifeTime, $this->_customerSession->getCookiePath()); } }
/** * Get Session model * * @return \Magento\Persistent\Model\Session */ public function getSession() { if ($this->_sessionModel === null) { $this->_sessionModel = $this->_sessionFactory->create(); $this->_sessionModel->loadByCookieKey(); } return $this->_sessionModel; }
/** * Unload persistent session (if set in config) * * @SuppressWarnings(PHPMD.UnusedFormalParameter) * @param Observer $observer * @return void */ public function execute(Observer $observer) { if (!$this->_persistentData->isEnabled() || !$this->_persistentData->getClearOnLogout()) { return; } $this->_sessionFactory->create()->removePersistentCookie(); // Unset persistent session $this->_persistentSession->setSession(null); }
/** * Clear expired persistent sessions * * @param \Magento\Cron\Model\Schedule $schedule * @return $this */ public function execute(\Magento\Cron\Model\Schedule $schedule) { $websiteIds = $this->_websiteCollectionFactory->create()->getAllIds(); if (!is_array($websiteIds)) { return $this; } foreach ($websiteIds as $websiteId) { $this->_sessionFactory->create()->deleteExpired($websiteId); } return $this; }
/** * Renew persistent cookie * * @param Observer $observer * @return void */ public function execute(Observer $observer) { if (!$this->_persistentData->canProcess($observer) || !$this->_persistentData->isEnabled() || !$this->_persistentSession->isPersistent()) { return; } /** @var $request \Magento\Framework\App\RequestInterface */ $request = $observer->getEvent()->getRequest(); if ($this->_customerSession->isLoggedIn() || $request->getFullActionName() == 'customer_account_logout') { $this->_sessionFactory->create()->renewPersistentCookie($this->_persistentData->getLifeTime(), $this->_customerSession->getCookiePath()); } }
/** * Check if such session key allowed (not exists) * * @param string $key * @return bool */ public function isKeyAllowed($key) { $sameSession = $this->_sessionFactory->create()->setLoadExpired(); $sameSession->loadByCookieKey($key); return !$sameSession->getId(); }