Skip to content

Commit 31f9fc1

Browse files
committed
ACP2E-882: [Magento Cloud] Category permission not working with GraphQL
- fix
1 parent 2e45de7 commit 31f9fc1

File tree

3 files changed

+53
-30
lines changed

3 files changed

+53
-30
lines changed

app/code/Magento/CatalogGraphQl/Model/Resolver/CategoriesQuery.php

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,15 @@
1010
use Magento\CatalogGraphQl\Model\Category\CategoryFilter;
1111
use Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\CategoryTree;
1212
use Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\ExtractDataFromCategoryTree;
13+
use Magento\Framework\Api\Search\SearchCriteriaFactory;
1314
use Magento\Framework\Exception\InputException;
1415
use Magento\Framework\GraphQl\Config\Element\Field;
1516
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
1617
use Magento\Framework\GraphQl\Query\Resolver\ArgumentsProcessorInterface;
1718
use Magento\Framework\GraphQl\Query\ResolverInterface;
1819
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
20+
use Magento\GraphQl\Model\Query\ContextInterface;
21+
use Magento\Store\Api\Data\StoreInterface;
1922

2023
/**
2124
* Categories resolver, used for GraphQL category data request processing.
@@ -42,22 +45,30 @@ class CategoriesQuery implements ResolverInterface
4245
*/
4346
private $argsSelection;
4447

48+
/**
49+
* @var SearchCriteriaFactory
50+
*/
51+
private $searchCriteriaFactory;
52+
4553
/**
4654
* @param CategoryTree $categoryTree
4755
* @param ExtractDataFromCategoryTree $extractDataFromCategoryTree
4856
* @param CategoryFilter $categoryFilter
4957
* @param ArgumentsProcessorInterface $argsSelection
58+
* @param SearchCriteriaFactory $searchCriteriaFactory
5059
*/
5160
public function __construct(
5261
CategoryTree $categoryTree,
5362
ExtractDataFromCategoryTree $extractDataFromCategoryTree,
5463
CategoryFilter $categoryFilter,
55-
ArgumentsProcessorInterface $argsSelection
64+
ArgumentsProcessorInterface $argsSelection,
65+
SearchCriteriaFactory $searchCriteriaFactory
5666
) {
5767
$this->categoryTree = $categoryTree;
5868
$this->extractDataFromCategoryTree = $extractDataFromCategoryTree;
5969
$this->categoryFilter = $categoryFilter;
6070
$this->argsSelection = $argsSelection;
71+
$this->searchCriteriaFactory = $searchCriteriaFactory;
6172
}
6273

6374
/**
@@ -87,7 +98,7 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
8798

8899
$rootCategoryIds = $filterResult['category_ids'] ?? [];
89100

90-
$filterResult['items'] = $this->fetchCategories($rootCategoryIds, $info, (int) $store->getId());
101+
$filterResult['items'] = $this->fetchCategories($rootCategoryIds, $info, $store, $context);
91102
return $filterResult;
92103
}
93104

@@ -96,17 +107,28 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
96107
*
97108
* @param array $categoryIds
98109
* @param ResolveInfo $info
99-
* @param int $storeId
110+
* @param StoreInterface $store
111+
* @param ContextInterface $context
100112
* @return array
101113
*/
102-
private function fetchCategories(array $categoryIds, ResolveInfo $info, int $storeId)
103-
{
114+
private function fetchCategories(
115+
array $categoryIds,
116+
ResolveInfo $info,
117+
StoreInterface $store,
118+
ContextInterface $context
119+
) {
104120
$fetchedCategories = [];
105121
foreach ($categoryIds as $categoryId) {
106-
$categoryTree = $this->categoryTree->getTree($info, $categoryId, $storeId);
107-
if (empty($categoryTree)) {
108-
continue;
109-
}
122+
/* Search Criteria is created for compatibility */
123+
$searchCriteria = $this->searchCriteriaFactory->create();
124+
$categoryTree = $this->categoryTree->getFilteredTree(
125+
$info,
126+
$categoryId,
127+
$searchCriteria,
128+
$store,
129+
[],
130+
$context
131+
);
110132
$fetchedCategories[] = current($this->extractDataFromCategoryTree->execute($categoryTree));
111133
}
112134

app/code/Magento/CatalogGraphQl/Model/Resolver/CategoryList.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
namespace Magento\CatalogGraphQl\Model\Resolver;
99

10+
use Magento\CatalogGraphQl\Model\Category\Filter\SearchCriteria;
1011
use Magento\Store\Api\Data\StoreInterface;
1112
use Magento\GraphQl\Model\Query\ContextInterface;
1213
use Magento\CatalogGraphQl\Model\Category\CategoryFilter;
@@ -45,22 +46,30 @@ class CategoryList implements ResolverInterface
4546
*/
4647
private $argsSelection;
4748

49+
/**
50+
* @var SearchCriteria
51+
*/
52+
private $searchCriteria;
53+
4854
/**
4955
* @param CategoryTree $categoryTree
5056
* @param ExtractDataFromCategoryTree $extractDataFromCategoryTree
5157
* @param CategoryFilter $categoryFilter
5258
* @param ArgumentsProcessorInterface $argsSelection
59+
* @param SearchCriteria $searchCriteria
5360
*/
5461
public function __construct(
5562
CategoryTree $categoryTree,
5663
ExtractDataFromCategoryTree $extractDataFromCategoryTree,
5764
CategoryFilter $categoryFilter,
58-
ArgumentsProcessorInterface $argsSelection
65+
ArgumentsProcessorInterface $argsSelection,
66+
SearchCriteria $searchCriteria
5967
) {
6068
$this->categoryTree = $categoryTree;
6169
$this->extractDataFromCategoryTree = $extractDataFromCategoryTree;
6270
$this->categoryFilter = $categoryFilter;
6371
$this->argsSelection = $argsSelection;
72+
$this->searchCriteria = $searchCriteria;
6473
}
6574

6675
/**
@@ -105,21 +114,19 @@ private function fetchCategories(
105114
array $criteria,
106115
StoreInterface $store,
107116
array $attributeNames,
108-
$context
117+
ContextInterface $context
109118
) : array {
110119
$fetchedCategories = [];
111120
foreach ($categoryIds as $categoryId) {
121+
$searchCriteria = $this->searchCriteria->buildCriteria($criteria, $store);
112122
$categoryTree = $this->categoryTree->getFilteredTree(
113123
$info,
114124
$categoryId,
115-
$criteria,
125+
$searchCriteria,
116126
$store,
117127
$attributeNames,
118128
$context
119129
);
120-
if (empty($categoryTree)) {
121-
continue;
122-
}
123130
$fetchedCategories[] = current($this->extractDataFromCategoryTree->execute($categoryTree));
124131
}
125132

app/code/Magento/CatalogGraphQl/Model/Resolver/Products/DataProvider/CategoryTree.php

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
use GraphQL\Language\AST\FieldNode;
1111
use GraphQL\Language\AST\NodeKind;
12-
1312
use Magento\Store\Api\Data\StoreInterface;
1413
use Magento\Catalog\Api\Data\CategoryInterface;
1514
use Magento\Catalog\Model\Category;
@@ -20,7 +19,7 @@
2019
use Magento\CatalogGraphQl\Model\Category\DepthCalculator;
2120
use Magento\CatalogGraphQl\Model\Category\LevelCalculator;
2221
use Magento\CatalogGraphQl\Model\Resolver\Categories\DataProvider\Category\CollectionProcessorInterface;
23-
use Magento\CatalogGraphQl\Model\Category\Filter\SearchCriteria;
22+
use Magento\Framework\Api\Search\SearchCriteria;
2423
use Magento\Framework\EntityManager\MetadataPool;
2524
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
2625
use Magento\Framework\Exception\LocalizedException;
@@ -29,13 +28,15 @@
2928

3029
/**
3130
* Category tree data provider
31+
*
32+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
3233
*/
3334
class CategoryTree
3435
{
3536
/**
3637
* In depth we need to calculate only children nodes, so the first wrapped node should be ignored
3738
*/
38-
const DEPTH_OFFSET = 1;
39+
public const DEPTH_OFFSET = 1;
3940

4041
/**
4142
* @var CollectionFactory
@@ -67,19 +68,13 @@ class CategoryTree
6768
*/
6869
private $collectionProcessor;
6970

70-
/**
71-
* @var SearchCriteria
72-
*/
73-
private $searchCriteria;
74-
7571
/**
7672
* @param CollectionFactory $collectionFactory
7773
* @param AttributesJoiner $attributesJoiner
7874
* @param DepthCalculator $depthCalculator
7975
* @param LevelCalculator $levelCalculator
8076
* @param MetadataPool $metadata
8177
* @param CollectionProcessorInterface $collectionProcessor
82-
* @param SearchCriteria $searchCriteria
8378
*/
8479
public function __construct(
8580
CollectionFactory $collectionFactory,
@@ -88,15 +83,13 @@ public function __construct(
8883
LevelCalculator $levelCalculator,
8984
MetadataPool $metadata,
9085
CollectionProcessorInterface $collectionProcessor,
91-
SearchCriteria $searchCriteria
9286
) {
9387
$this->collectionFactory = $collectionFactory;
9488
$this->attributesJoiner = $attributesJoiner;
9589
$this->depthCalculator = $depthCalculator;
9690
$this->levelCalculator = $levelCalculator;
9791
$this->metadata = $metadata;
9892
$this->collectionProcessor = $collectionProcessor;
99-
$this->searchCriteria = $searchCriteria;
10093
}
10194

10295
/**
@@ -125,7 +118,8 @@ public function getTree(ResolveInfo $resolveInfo, int $rootCategoryId, int $stor
125118
* @throws LocalizedException
126119
* @throws Exception
127120
*/
128-
private function getCollection(ResolveInfo $resolveInfo, int $rootCategoryId) : Collection {
121+
private function getCollection(ResolveInfo $resolveInfo, int $rootCategoryId) : Collection
122+
{
129123
$categoryQuery = $resolveInfo->fieldNodes[0];
130124
$collection = $this->collectionFactory->create();
131125
$this->joinAttributesRecursively($collection, $categoryQuery, $resolveInfo);
@@ -196,23 +190,23 @@ private function joinAttributesRecursively(
196190
*
197191
* @param ResolveInfo $resolveInfo
198192
* @param int $rootCategoryId
199-
* @param array $criteria
193+
* @param SearchCriteria $searchCriteria
200194
* @param StoreInterface $store
201195
* @param array $attributeNames
202196
* @param ContextInterface $context
203197
* @return Iterator
204198
* @throws LocalizedException
205199
* @throws Exception
200+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
206201
*/
207202
public function getFilteredTree(
208203
ResolveInfo $resolveInfo,
209204
int $rootCategoryId,
210-
array $criteria,
205+
SearchCriteria $searchCriteria,
211206
StoreInterface $store,
212207
array $attributeNames,
213208
ContextInterface $context
214209
): Iterator {
215-
$searchCriteria = $this->searchCriteria->buildCriteria($criteria, $store);
216210
$collection = $this->getCollection($resolveInfo, $rootCategoryId);
217211
$this->collectionProcessor->process($collection, $searchCriteria, $attributeNames, $context);
218212
return $collection->getIterator();

0 commit comments

Comments
 (0)