-
Notifications
You must be signed in to change notification settings - Fork 9.3k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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( | ||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. just a minor observation in comparison to the product processor There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No DocBlock
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done