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/CartForge/PrivateProductRequest/Plugin/Catalog/Product/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : //home/corals/cartforge.co/app/code/CartForge/PrivateProductRequest/Plugin/Catalog/Product/View.php
<?php

namespace CartForge\PrivateProductRequest\Plugin\Catalog\Product;

use Magento\Customer\Api\CustomerRepositoryInterface;
use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\UrlInterface;
use Magento\Catalog\Api\CategoryRepositoryInterface;
use Magento\Catalog\Model\CategoryFactory;

class View
{
    protected $productFactory;
    protected $resultForwardFactory;
    protected $scopeConfig;
    protected $httpContext;
    protected $customerRepository;
    protected $resultFactory;
    protected $url;
    protected $jsonSerializer;
    protected $_messageManager;
    protected $privateProductFactory;
    protected $categoryRepository;
    protected $categoryFactory;

    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,
        CategoryRepositoryInterface                         $categoryRepository,
        CategoryFactory                                     $categoryFactory
    )
    {
        $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;
        $this->categoryRepository = $categoryRepository;
        $this->categoryFactory = $categoryFactory;
    }

    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('/privateproductrequest/request/guestlanding');
                return $result;
            } else {
                $customerId = $this->httpContext->getValue('customer_id');

                $customerGroups = $this->getCustomerGroups($customerId);

                $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('/privateproductrequest/request/landing');
                    return $result;
                }
            }
        }

        $categoryIds = $product->getCategoryIds();

        // Check if any category or its parent is private
        foreach ($categoryIds as $categoryId) {
            $category = $this->categoryFactory->create()->load($categoryId);


            if ($category && $this->isCategoryOrParentPrivate($category)) {
                if (!$this->getIsLoggedIn()) {
                    $result = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
                    $result->setUrl('/privateproductrequest/request/guestlanding');
                    return $result;
                } else {
                    $customerId = $this->httpContext->getValue('customer_id');
                    $customerGroups = $this->getCustomerGroups($customerId);

                    // Check if customer belongs to any parent category's group
                    $parentCategory = $category;
                    $isAuthorized = false;

                    while ($parentCategory) {
                        $catPrivateGroup = $parentCategory->getData('category_private_group');
                        if ($catPrivateGroup) {
                            $catGroups = $this->jsonSerializer->unserialize($catPrivateGroup);

                            $matched = array_intersect($catGroups, $customerGroups);

                            if (!empty($matched)) {
                                $isAuthorized = true;
                                break;
                            }
                        }

                        $parentId = $parentCategory->getParentId();

                        if (!$parentId || $parentId == \Magento\Catalog\Model\Category::TREE_ROOT_ID || $parentId == $parentCategory->getId()) {
                            break;
                        }

                        $parentCategory = $this->categoryRepository->get($parentId);
                    }
                }

                if (!$isAuthorized) {
                    $this->_messageManager->addNotice(__('You are not authorized to view this product.'));
                    $result = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
                    $result->setUrl('/privateproductrequest/request/landing');
                    return $result;
                }
            }
        }

        return $result;
    }

    /**
     * Return is customer logged in
     *
     * @return bool
     */
    private function getIsLoggedIn()
    {
        return $this->httpContext->getValue(\Magento\Customer\Model\Context::CONTEXT_AUTH);
    }

    /**
     * Check if the category or any parent category is private
     *
     * @param \Magento\Catalog\Model\Category $category
     * @return bool
     */
    private function isCategoryOrParentPrivate($category)
    {
        $categoryId = $category->getId();

        // Start with the current category
        while ($category) {
            // Check if the current category is private
            if ($category->getData('is_private_category')) {
                return true;
            }

            // Check if the current category has private groups
            $catPrivateGroup = $category->getData('category_private_group');
            if ($catPrivateGroup) {
                $catGroups = $this->jsonSerializer->unserialize($catPrivateGroup);
                if (count($catGroups) > 0) {
                    return true;
                }
            }

            // Move to the parent category
            $parentId = $category->getParentId();
            if (!$parentId || $parentId == \Magento\Catalog\Model\Category::TREE_ROOT_ID || $parentId == $categoryId) {
                break;
            }

            $category = $this->categoryFactory->create()->load($parentId);
        }
        return false;
    }


    public function getCustomerGroups($customerId)
    {
        $customer = $this->customerRepository->getById($customerId);
        $customerGroups = [];
        $privateGroup = $customer->getCustomAttribute('customer_private_group');
        if ($privateGroup) {
            $customerGroups = $this->jsonSerializer->unserialize(
                $privateGroup->getValue()
            );
        }


        return $customerGroups;
    }
}

Spamworldpro Mini