Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Categories permissions for "categories" and "categoryList" #29883

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
124 changes: 58 additions & 66 deletions app/code/Magento/CatalogGraphQl/Model/Category/CategoryFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,78 +7,101 @@

namespace Magento\CatalogGraphQl\Model\Category;

use Magento\Catalog\Api\CategoryListInterface;
use Magento\Catalog\Api\Data\CategoryInterface;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Catalog\Api\CategoryRepositoryInterface;
use Magento\Catalog\Api\Data\CategorySearchResultsInterface;
use Magento\Catalog\Api\Data\CategorySearchResultsInterfaceFactory;
use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory;
use Magento\CatalogGraphQl\Model\Resolver\Categories\DataProvider\Category\CollectionProcessorInterface;
use Magento\CatalogGraphQl\Model\Category\Filter\SearchCriteria;
use Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface;
use Magento\Framework\Exception\InputException;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Query\Resolver\Argument\SearchCriteria\ArgumentApplier\Filter;
use Magento\Framework\GraphQl\Query\Resolver\Argument\SearchCriteria\ArgumentApplier\Sort;
use Magento\Search\Model\Query;
use Magento\GraphQl\Model\Query\ContextInterface;
use Magento\Store\Api\Data\StoreInterface;
use Magento\Store\Model\ScopeInterface;
use Magento\Framework\GraphQl\Query\Resolver\Argument\SearchCriteria\Builder;

/**
* Category filter allows filtering category results by attributes.
*/
class CategoryFilter
{
/**
* @var string
* @var CollectionFactory
*/
private const SPECIAL_CHARACTERS = '-+~/\\<>\'":*$#@()!,.?`=%&^';
private $categoryCollectionFactory;

/**
* @var ScopeConfigInterface
* @var CollectionProcessorInterface
*/
private $scopeConfig;
private $collectionProcessor;

/**
* @var CategoryListInterface
* @var JoinProcessorInterface
*/
private $categoryList;
private $extensionAttributesJoinProcessor;

/**
* @var Builder
* @var CategorySearchResultsInterfaceFactory
*/
private $searchCriteriaBuilder;
private $categorySearchResultsFactory;

/**
* @param ScopeConfigInterface $scopeConfig
* @param CategoryListInterface $categoryList
* @param Builder $searchCriteriaBuilder
* @var CategoryRepositoryInterface
*/
private $categoryRepository;

/**
* @var SearchCriteria
*/
private $searchCriteria;

/**
* @param CollectionFactory $categoryCollectionFactory
* @param CollectionProcessorInterface $collectionProcessor
* @param JoinProcessorInterface $extensionAttributesJoinProcessor
* @param CategorySearchResultsInterfaceFactory $categorySearchResultsFactory
* @param CategoryRepositoryInterface $categoryRepository
* @param SearchCriteria $searchCriteria
*/
public function __construct(
ScopeConfigInterface $scopeConfig,
CategoryListInterface $categoryList,
Builder $searchCriteriaBuilder
CollectionFactory $categoryCollectionFactory,
CollectionProcessorInterface $collectionProcessor,
JoinProcessorInterface $extensionAttributesJoinProcessor,
CategorySearchResultsInterfaceFactory $categorySearchResultsFactory,
CategoryRepositoryInterface $categoryRepository,
SearchCriteria $searchCriteria
) {
$this->scopeConfig = $scopeConfig;
$this->categoryList = $categoryList;
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
$this->categoryCollectionFactory = $categoryCollectionFactory;
$this->collectionProcessor = $collectionProcessor;
$this->extensionAttributesJoinProcessor = $extensionAttributesJoinProcessor;
$this->categorySearchResultsFactory = $categorySearchResultsFactory;
$this->categoryRepository = $categoryRepository;
$this->searchCriteria = $searchCriteria;
}

/**
* Search for categories
*
* @param array $criteria
* @param StoreInterface $store
* @param array $attributeNames
* @param ContextInterface $context
* @return int[]
* @throws InputException
*/
public function getResult(array $criteria, StoreInterface $store)
public function getResult(array $criteria, StoreInterface $store, array $attributeNames, ContextInterface $context)
{
$categoryIds = [];
$criteria[Filter::ARGUMENT_NAME] = $this->formatMatchFilters($criteria['filters'], $store);
$criteria[Filter::ARGUMENT_NAME][CategoryInterface::KEY_IS_ACTIVE] = ['eq' => 1];
$criteria[Sort::ARGUMENT_NAME][CategoryInterface::KEY_POSITION] = ['ASC'];
$searchCriteria = $this->searchCriteriaBuilder->build('categoryList', $criteria);
$pageSize = $criteria['pageSize'] ?? 20;
$currentPage = $criteria['currentPage'] ?? 1;
$searchCriteria->setPageSize($pageSize)->setCurrentPage($currentPage);
$searchCriteria = $this->searchCriteria->buildCriteria($criteria, $store);
$collection = $this->categoryCollectionFactory->create();
$this->extensionAttributesJoinProcessor->process($collection);
$this->collectionProcessor->process($collection, $searchCriteria, $attributeNames, $context);

/** @var CategorySearchResultsInterface $searchResult */
$categories = $this->categorySearchResultsFactory->create();
$categories->setSearchCriteria($searchCriteria);
$categories->setItems($collection->getItems());
$categories->setTotalCount($collection->getSize());

$categories = $this->categoryList->getList($searchCriteria);
$categoryIds = [];
foreach ($categories->getItems() as $category) {
$categoryIds[] = (int)$category->getId();
}
Expand Down Expand Up @@ -106,35 +129,4 @@ public function getResult(array $criteria, StoreInterface $store)
]
];
}

/**
* Format match filters to behave like fuzzy match
*
* @param array $filters
* @param StoreInterface $store
* @return array
* @throws InputException
*/
private function formatMatchFilters(array $filters, StoreInterface $store): array
{
$minQueryLength = $this->scopeConfig->getValue(
Query::XML_PATH_MIN_QUERY_LENGTH,
ScopeInterface::SCOPE_STORE,
$store
);

foreach ($filters as $filter => $condition) {
$conditionType = current(array_keys($condition));
if ($conditionType === 'match') {
$searchValue = trim(str_replace(self::SPECIAL_CHARACTERS, '', $condition[$conditionType]));
$matchLength = strlen($searchValue);
if ($matchLength < $minQueryLength) {
throw new InputException(__('Invalid match filter. Minimum length is %1.', $minQueryLength));
}
unset($filters[$filter]['match']);
$filters[$filter]['like'] = '%' . $searchValue . '%';
}
}
return $filters;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\CatalogGraphQl\Model\Category\Filter;

use Magento\Catalog\Api\Data\CategoryInterface;
use Magento\Framework\Api\Search\SearchCriteriaInterface;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\Exception\InputException;
use Magento\Framework\GraphQl\Query\Resolver\Argument\SearchCriteria\ArgumentApplier\Filter;
use Magento\Framework\GraphQl\Query\Resolver\Argument\SearchCriteria\ArgumentApplier\Sort;
use Magento\Framework\GraphQl\Query\Resolver\Argument\SearchCriteria\Builder;
use Magento\Search\Model\Query;
use Magento\Store\Api\Data\StoreInterface;
use Magento\Store\Model\ScopeInterface;

/**
* Utility to help transform raw criteria data into SearchCriteriaInterface
*/
class SearchCriteria
{
/**
* @var string
*/
private const SPECIAL_CHARACTERS = '-+~/\\<>\'":*$#@()!,.?`=%&^';

/**
* @var ScopeConfigInterface
*/
private $scopeConfig;

/**
* @var Builder
*/
private $searchCriteriaBuilder;

/**
* @param ScopeConfigInterface $scopeConfig
* @param Builder $searchCriteriaBuilder
*/
public function __construct(
ScopeConfigInterface $scopeConfig,
Builder $searchCriteriaBuilder
) {
$this->scopeConfig = $scopeConfig;
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
}

/**
* Transform raw criteria data into SearchCriteriaInterface
*
* @param array $criteria
* @param StoreInterface $store
* @return SearchCriteriaInterface
* @throws InputException
*/
public function buildCriteria(array $criteria, StoreInterface $store): SearchCriteriaInterface
{
$criteria[Filter::ARGUMENT_NAME] = $this->formatMatchFilters($criteria['filters'], $store);
$criteria[Filter::ARGUMENT_NAME][CategoryInterface::KEY_IS_ACTIVE] = ['eq' => 1];
$criteria[Sort::ARGUMENT_NAME][CategoryInterface::KEY_POSITION] = ['ASC'];

$searchCriteria = $this->searchCriteriaBuilder->build('categoryList', $criteria);
$pageSize = $criteria['pageSize'] ?? 20;
$currentPage = $criteria['currentPage'] ?? 1;
$searchCriteria->setPageSize($pageSize)->setCurrentPage($currentPage);

return $searchCriteria;
}

/**
* Format match filters to behave like fuzzy match
*
* @param array $filters
* @param StoreInterface $store
* @return array
* @throws InputException
*/
private function formatMatchFilters(array $filters, StoreInterface $store): array
{
$minQueryLength = $this->scopeConfig->getValue(
Query::XML_PATH_MIN_QUERY_LENGTH,
ScopeInterface::SCOPE_STORE,
$store
);

foreach ($filters as $filter => $condition) {
$conditionType = current(array_keys($condition));
if ($conditionType === 'match') {
$searchValue = trim(str_replace(self::SPECIAL_CHARACTERS, '', $condition[$conditionType]));
$matchLength = strlen($searchValue);
if ($matchLength < $minQueryLength) {
throw new InputException(__('Invalid match filter. Minimum length is %1.', $minQueryLength));
}
unset($filters[$filter]['match']);
$filters[$filter]['like'] = '%' . $searchValue . '%';
}
}
return $filters;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\CatalogGraphQl\Model\Resolver\Categories\DataProvider\Category\CollectionProcessor;

use Magento\Catalog\Model\ResourceModel\Category\Collection;
use Magento\CatalogGraphQl\Model\Resolver\Categories\DataProvider\Category\CollectionProcessorInterface;
use Magento\Framework\Api\SearchCriteriaInterface;
use Magento\GraphQl\Model\Query\ContextInterface;
use Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface as SearchCriteriaCollectionProcessor;

/**
* Apply pre-defined catalog filtering
*
* {@inheritdoc}
*/
class CatalogProcessor implements CollectionProcessorInterface
{
/** @var SearchCriteriaCollectionProcessor */
private $collectionProcessor;

/**
* @param SearchCriteriaCollectionProcessor $collectionProcessor
*/
public function __construct(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No DocBlock

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

SearchCriteriaCollectionProcessor $collectionProcessor
) {
$this->collectionProcessor = $collectionProcessor;
}

/**
* Process collection to add additional joins, attributes, and clauses to a category collection.
*
* @param Collection $collection
* @param SearchCriteriaInterface $searchCriteria
* @param array $attributeNames
* @param ContextInterface|null $context
* @return Collection
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function process(
Collection $collection,
SearchCriteriaInterface $searchCriteria,
array $attributeNames,
ContextInterface $context = null
): Collection {
$this->collectionProcessor->process($searchCriteria, $collection);

return $collection;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\CatalogGraphQl\Model\Resolver\Categories\DataProvider\Category;

use Magento\Catalog\Model\ResourceModel\Category\Collection;
use Magento\Framework\Api\SearchCriteriaInterface;
use Magento\GraphQl\Model\Query\ContextInterface;

/**
* Add additional joins, attributes, and clauses to a category collection.
*/
interface CollectionProcessorInterface
{
/**
* Process collection to add additional joins, attributes, and clauses to a category collection.
*
* @param Collection $collection
* @param SearchCriteriaInterface $searchCriteria
* @param array $attributeNames
* @param ContextInterface|null $context
* @return Collection
*/
public function process(
Collection $collection,
SearchCriteriaInterface $searchCriteria,
array $attributeNames,
ContextInterface $context = null
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just a minor observation in comparison to the product processor
Noticed that there we passed attributes as well. I guess here we did not need it here. Not sure because this is meant as an extension point if we need to add it and just not use it ourselves..maybe other extensions will..

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. Now, I have edited to pass attributesNames as well. Currently are not used, but someday maybe someone extends this.

): Collection;
}
Loading