custom/plugins/CioCustomerPermissionGroups/src/Service/CustomProductRepository.php line 91

Open in your IDE?
  1. <?php
  2. namespace CioCustomerPermissionGroups\Service;
  3. use CioCustomerPermissionGroups\CioCustomerPermissionGroups;
  4. use CioCustomerPermissionGroups\Event\CustomerGroupsLoadedEvent;
  5. use Shopware\Core\Checkout\Customer\CustomerEntity;
  6. use Shopware\Core\Content\Category\CategoryEntity;
  7. use Shopware\Core\Content\Test\Product\Repository\ProductRepositoryTest;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Entity;
  9. use Shopware\Core\Framework\DataAbstractionLayer\EntityCollection;
  10. use Shopware\Core\Framework\DataAbstractionLayer\EntityDefinition;
  11. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  12. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityAggregationResultLoadedEvent;
  14. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
  15. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEventFactory;
  16. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntitySearchResultLoadedEvent;
  17. use Shopware\Core\Framework\DataAbstractionLayer\Exception\InconsistentCriteriaIdsException;
  18. use Shopware\Core\Framework\DataAbstractionLayer\Field\AssociationField;
  19. use Shopware\Core\Framework\DataAbstractionLayer\Read\EntityReaderInterface;
  20. use Shopware\Core\Framework\DataAbstractionLayer\RepositorySearchDetector;
  21. use Shopware\Core\Framework\DataAbstractionLayer\Search\AggregationResult\AggregationResultCollection;
  22. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  23. use Shopware\Core\Framework\DataAbstractionLayer\Search\EntityAggregatorInterface;
  24. use Shopware\Core\Framework\DataAbstractionLayer\Search\EntitySearcherInterface;
  25. use Shopware\Core\Framework\DataAbstractionLayer\Search\EntitySearchResult;
  26. use Shopware\Core\Framework\DataAbstractionLayer\Search\IdSearchResult;
  27. use Shopware\Core\Framework\Struct\ArrayEntity;
  28. use Shopware\Core\System\SalesChannel\Context\AbstractSalesChannelContextFactory;
  29. use Shopware\Core\System\SalesChannel\Context\SalesChannelContextFactory;
  30. use Shopware\Core\System\SalesChannel\Entity\SalesChannelDefinitionInterface;
  31. use Shopware\Core\System\SalesChannel\Entity\SalesChannelEntityIdSearchResultLoadedEvent;
  32. use Shopware\Core\System\SalesChannel\Entity\SalesChannelEntityLoadedEvent;
  33. use Shopware\Core\System\SalesChannel\Entity\SalesChannelEntitySearchResultLoadedEvent;
  34. use Shopware\Core\System\SalesChannel\Entity\SalesChannelRepository;
  35. use Shopware\Core\System\SalesChannel\Entity\SalesChannelRepositoryInterface;
  36. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  37. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  38. class CustomProductRepository extends SalesChannelRepository
  39. {
  40.     private EntityRepositoryInterface $categoryRepository;
  41.     public function __construct(
  42.         EntityDefinition $definition,
  43.         EntityReaderInterface $reader,
  44.         EntitySearcherInterface $searcher,
  45.         EntityAggregatorInterface $aggregator,
  46.         EventDispatcherInterface $eventDispatcher,
  47.         EntityRepositoryInterface $categoryRepository,
  48.         ?EntityLoadedEventFactory $eventFactory
  49.     ) {
  50.         parent::__construct($definition$reader$searcher$aggregator$eventDispatcher$eventFactory);
  51.         $this->categoryRepository $categoryRepository;
  52.     }
  53.     public function searchIds(Criteria $criteriaSalesChannelContext $salesChannelContext): IdSearchResult
  54.     {
  55.         $context $salesChannelContext->getContext();
  56.         $cioAllowedCategories $this->getAllowedCategories($salesChannelContext->getCustomer(), $context);
  57.         $hash md5(json_encode($cioAllowedCategories));
  58.         $context->setExtensions([
  59.             'cioAllowedCategories' => $cioAllowedCategories
  60.         ]);
  61.         if ($criteria->getTitle() === 'cms::product-listing') {
  62.             $criteria->setTitle($criteria->getTitle() . '-' $hash);
  63.         }
  64.         return parent::searchIds($criteria$salesChannelContext);
  65.     }
  66.     public function search(Criteria $criteriaSalesChannelContext $salesChannelContext): EntitySearchResult
  67.     {
  68.         $context $salesChannelContext->getContext();
  69.         $cioAllowedCategories $this->getAllowedCategories($salesChannelContext->getCustomer(), $context);
  70.         $hash md5(json_encode($cioAllowedCategories));
  71.         $context->setExtensions([
  72.             'cioAllowedCategories' => $cioAllowedCategories
  73.         ]);
  74.         return parent::search($criteria$salesChannelContext); // TODO: Change the autogenerated stub
  75.     }
  76.     private function getAllowedCategories($customerEntity$context)
  77.     {
  78.         $allowed = [];
  79.         if (!$customerEntity instanceof CustomerEntity) {
  80.             return $allowed;
  81.         }
  82.         $aclgroupIds = [];
  83.         if (isset($customerEntity->getCustomFields()['custom_acl_roles'])) {
  84.             $aclgroups $customerEntity->getCustomFields()['custom_acl_roles'];
  85.             $aclgroupIds CioCustomerPermissionGroups::getAclIds($aclgroups);
  86.         }
  87.         $customerGroupsLodedEvent = new CustomerGroupsLoadedEvent($aclgroupIds$customerEntity);
  88.         $this->eventDispatcher->dispatch($customerGroupsLodedEvent);
  89.         $aclgroupIds $customerGroupsLodedEvent->getGroups();
  90.         $categories $this->categoryRepository->search(new Criteria(), $context);
  91.         /** @var CategoryEntity $category */
  92.         foreach ($categories->getElements() as $category) {
  93.             if (!$category->getCustomFields()) {
  94.                 $allowed[] = $category->getId();
  95.                 continue;
  96.             }
  97.             if (!key_exists('custom_acl_groups'$category->getCustomFields())) {
  98.                 $allowed[] = $category->getId();
  99.                 continue;
  100.             }
  101.             if (count($category->getCustomFields()['custom_acl_groups']) === 0) {
  102.                 $allowed[] = $category->getId();
  103.                 continue;
  104.             }
  105.             if (count(array_intersect($aclgroupIds$category->getCustomFields()['custom_acl_groups']))) {
  106.                 $allowed[] = $category->getId();
  107.             }
  108.         }
  109.         return $allowed;
  110.     }
  111. }