<?php
namespace CioCustomerPermissionGroups\Service;
use CioCustomerPermissionGroups\CioCustomerPermissionGroups;
use CioCustomerPermissionGroups\Event\CustomerGroupsLoadedEvent;
use Shopware\Core\Checkout\Customer\CustomerEntity;
use Shopware\Core\Content\Category\CategoryEntity;
use Shopware\Core\Content\Test\Product\Repository\ProductRepositoryTest;
use Shopware\Core\Framework\DataAbstractionLayer\Entity;
use Shopware\Core\Framework\DataAbstractionLayer\EntityCollection;
use Shopware\Core\Framework\DataAbstractionLayer\EntityDefinition;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityAggregationResultLoadedEvent;
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEventFactory;
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntitySearchResultLoadedEvent;
use Shopware\Core\Framework\DataAbstractionLayer\Exception\InconsistentCriteriaIdsException;
use Shopware\Core\Framework\DataAbstractionLayer\Field\AssociationField;
use Shopware\Core\Framework\DataAbstractionLayer\Read\EntityReaderInterface;
use Shopware\Core\Framework\DataAbstractionLayer\RepositorySearchDetector;
use Shopware\Core\Framework\DataAbstractionLayer\Search\AggregationResult\AggregationResultCollection;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\EntityAggregatorInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\EntitySearcherInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\EntitySearchResult;
use Shopware\Core\Framework\DataAbstractionLayer\Search\IdSearchResult;
use Shopware\Core\Framework\Struct\ArrayEntity;
use Shopware\Core\System\SalesChannel\Context\AbstractSalesChannelContextFactory;
use Shopware\Core\System\SalesChannel\Context\SalesChannelContextFactory;
use Shopware\Core\System\SalesChannel\Entity\SalesChannelDefinitionInterface;
use Shopware\Core\System\SalesChannel\Entity\SalesChannelEntityIdSearchResultLoadedEvent;
use Shopware\Core\System\SalesChannel\Entity\SalesChannelEntityLoadedEvent;
use Shopware\Core\System\SalesChannel\Entity\SalesChannelEntitySearchResultLoadedEvent;
use Shopware\Core\System\SalesChannel\Entity\SalesChannelRepository;
use Shopware\Core\System\SalesChannel\Entity\SalesChannelRepositoryInterface;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
class CustomProductRepository extends SalesChannelRepository
{
private EntityRepositoryInterface $categoryRepository;
public function __construct(
EntityDefinition $definition,
EntityReaderInterface $reader,
EntitySearcherInterface $searcher,
EntityAggregatorInterface $aggregator,
EventDispatcherInterface $eventDispatcher,
EntityRepositoryInterface $categoryRepository,
?EntityLoadedEventFactory $eventFactory
) {
parent::__construct($definition, $reader, $searcher, $aggregator, $eventDispatcher, $eventFactory);
$this->categoryRepository = $categoryRepository;
}
public function searchIds(Criteria $criteria, SalesChannelContext $salesChannelContext): IdSearchResult
{
$context = $salesChannelContext->getContext();
$cioAllowedCategories = $this->getAllowedCategories($salesChannelContext->getCustomer(), $context);
$hash = md5(json_encode($cioAllowedCategories));
$context->setExtensions([
'cioAllowedCategories' => $cioAllowedCategories
]);
if ($criteria->getTitle() === 'cms::product-listing') {
$criteria->setTitle($criteria->getTitle() . '-' . $hash);
}
return parent::searchIds($criteria, $salesChannelContext);
}
public function search(Criteria $criteria, SalesChannelContext $salesChannelContext): EntitySearchResult
{
$context = $salesChannelContext->getContext();
$cioAllowedCategories = $this->getAllowedCategories($salesChannelContext->getCustomer(), $context);
$hash = md5(json_encode($cioAllowedCategories));
$context->setExtensions([
'cioAllowedCategories' => $cioAllowedCategories
]);
return parent::search($criteria, $salesChannelContext); // TODO: Change the autogenerated stub
}
private function getAllowedCategories($customerEntity, $context)
{
$allowed = [];
if (!$customerEntity instanceof CustomerEntity) {
return $allowed;
}
$aclgroupIds = [];
if (isset($customerEntity->getCustomFields()['custom_acl_roles'])) {
$aclgroups = $customerEntity->getCustomFields()['custom_acl_roles'];
$aclgroupIds = CioCustomerPermissionGroups::getAclIds($aclgroups);
}
$customerGroupsLodedEvent = new CustomerGroupsLoadedEvent($aclgroupIds, $customerEntity);
$this->eventDispatcher->dispatch($customerGroupsLodedEvent);
$aclgroupIds = $customerGroupsLodedEvent->getGroups();
$categories = $this->categoryRepository->search(new Criteria(), $context);
/** @var CategoryEntity $category */
foreach ($categories->getElements() as $category) {
if (!$category->getCustomFields()) {
$allowed[] = $category->getId();
continue;
}
if (!key_exists('custom_acl_groups', $category->getCustomFields())) {
$allowed[] = $category->getId();
continue;
}
if (count($category->getCustomFields()['custom_acl_groups']) === 0) {
$allowed[] = $category->getId();
continue;
}
if (count(array_intersect($aclgroupIds, $category->getCustomFields()['custom_acl_groups']))) {
$allowed[] = $category->getId();
}
}
return $allowed;
}
}