![]() Server : Apache System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64 User : corals ( 1002) PHP Version : 7.4.33 Disable Function : exec,passthru,shell_exec,system Directory : /home/corals/cartforge.co/app/code/Webkul/PrivateShop/Plugin/Catalog/Category/ |
<?php /** * Webkul Software * * @category Webkul * @package Webkul_PrivateShop * @author Webkul Software Private Limited * @copyright Webkul Software Private Limited (https://webkul.com) * @license https://store.webkul.com/license.html */ namespace Webkul\PrivateShop\Plugin\Catalog\Category; use Magento\Customer\Api\CustomerRepositoryInterface; use Magento\Framework\Controller\ResultFactory; use Magento\Framework\UrlInterface; class View { /** * @var \Magento\Catalog\Model\CategoryFactory */ protected $categoryFactory; /** * @var \Magento\Framework\Controller\Result\ForwardFactory */ protected $resultForwardFactory; /** * @var \Magento\Framework\App\Config\ScopeConfigInterface */ protected $scopeConfig; /** * @var \Magento\Framework\App\Http\Context */ protected $httpContext; /** * @var CustomerRepositoryInterface */ protected $customerRepository; /** * @var ResultFactory */ protected $resultFactory; /** * @var UrlInterface */ protected $url; /** * @var \Magento\Framework\Serialize\Serializer\Json */ protected $jsonSerializer; /** * @var \Magento\Framework\Message\ManagerInterface */ protected $_messageManager; /** * * @param \Magento\Catalog\Model\CategoryFactory $categoryFactory * @param \Magento\Framework\Controller\Result\ForwardFactory $resultForwardFactory * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig * @param \Magento\Framework\App\Http\Context $httpContext * @param CustomerRepositoryInterface $customerRepository * @param ResultFactory $resultFactory * @param UrlInterface $url * @param \Magento\Framework\Serialize\Serializer\Json $jsonSerializer * @param \Magento\Framework\Message\ManagerInterface $messageManager */ public function __construct( \Magento\Catalog\Model\CategoryFactory $categoryFactory, \Magento\Framework\Controller\Result\ForwardFactory $resultForwardFactory, \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, \Magento\Framework\App\Http\Context $httpContext, CustomerRepositoryInterface $customerRepository, ResultFactory $resultFactory, UrlInterface $url, \Magento\Framework\Serialize\Serializer\Json $jsonSerializer, \Magento\Framework\Message\ManagerInterface $messageManager ) { $this->categoryFactory = $categoryFactory; $this->scopeConfig = $scopeConfig; $this->resultForwardFactory = $resultForwardFactory; $this->httpContext = $httpContext; $this->customerRepository = $customerRepository; $this->jsonSerializer = $jsonSerializer; $this->resultFactory = $resultFactory; $this->url = $url; $this->_messageManager = $messageManager; } /** * After Execute * * @param \Magento\Catalog\Controller\Category\View $subject * @param [mixed] $result * @return $result */ public function afterExecute(\Magento\Catalog\Controller\Category\View $subject, $result) { $categoryId = (int)$subject->getRequest()->getParam('id', false); $category = $this->categoryFactory->create()->load($categoryId); if ($category->getData('is_private_category')) { if (!$this->getIsLoggedIn()) { $result = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT); $result->setUrl($this->url->getUrl('customer/account')); return $result; } else { $customerId = $this->httpContext->getValue('customer_id'); $customer = $this->customerRepository->getById($customerId); $customerGroups = []; $privateGroup = $customer->getCustomAttribute('customer_private_group'); if ($privateGroup) { $customerGroups = $this->jsonSerializer->unserialize( $privateGroup->getValue() ); } $catPrivateGroup = $category->getData('category_private_group'); $catrGroups = []; if ($catPrivateGroup) { $catrGroups = $this->jsonSerializer->unserialize( $catPrivateGroup ); } $matched = array_intersect($catrGroups, $customerGroups); if (empty($matched)) { $this->_messageManager->addNotice(__('You are not authorized to view this product.')); $result = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT); $result->setUrl($this->url->getUrl('/')); return $result; } } } return $result; } /** * Return is customer logged in * * @return bool */ private function getIsLoggedIn() { return $this->httpContext->getValue(\Magento\Customer\Model\Context::CONTEXT_AUTH); } }