Skip to content

Commit

Permalink
PIM-10485 : get category tree for channel context without filter user… (
Browse files Browse the repository at this point in the history
#17263)

* PIM-10485 : get category tree for channel context without filter user right
  • Loading branch information
Antoine T committed Jun 28, 2022
1 parent 7817aea commit bf152d1
Show file tree
Hide file tree
Showing 11 changed files with 129 additions and 53 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
- PIM-10475: Fix option existence validation for numeric option codes
- PIM-10483: Fix slow loading products when filtering by variants
- PIM-10484: Fix job filter on status being incoherent with job interrupted by demon crash
- PIM-10485: Fix Wrong category tree is displayed in channel settings if user has no right on the linked category tree
- PIM-10495: Fix product datagrid by increasing sort_buffer_size
- PIM-10499: Fix MySQL's out of sort memory errors on variant product and product model edit form
- PIM-10500: Fix API not returning quantified associations for products when association type code is numeric
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use Akeneo\Channel\Infrastructure\Component\Model\ChannelInterface;
use Akeneo\Channel\Infrastructure\Component\Repository\ChannelRepositoryInterface;
use Akeneo\Pim\Enrichment\Component\Category\Query\PublicApi\CategoryTree;
use Akeneo\Pim\Enrichment\Component\Category\Query\PublicApi\FindCategoryTrees;
use Akeneo\Platform\Bundle\FrameworkBundle\Security\SecurityFacadeInterface;
use Akeneo\Tool\Component\StorageUtils\Factory\SimpleFactoryInterface;
use Akeneo\Tool\Component\StorageUtils\Remover\RemoverInterface;
Expand All @@ -30,13 +32,14 @@ class ChannelController
{
public function __construct(
private ChannelRepositoryInterface $channelRepository,
private NormalizerInterface $normalizer,
private ObjectUpdaterInterface $updater,
private SaverInterface $saver,
private RemoverInterface $remover,
private SimpleFactoryInterface $channelFactory,
private ValidatorInterface $validator,
private SecurityFacadeInterface $securityFacade,
private NormalizerInterface $normalizer,
private ObjectUpdaterInterface $updater,
private SaverInterface $saver,
private RemoverInterface $remover,
private SimpleFactoryInterface $channelFactory,
private ValidatorInterface $validator,
private SecurityFacadeInterface $securityFacade,
private FindCategoryTrees $findCategoryTrees
) {
}

Expand Down Expand Up @@ -74,6 +77,18 @@ public function getAction(Request $request, string $identifier): JsonResponse
);
}

/**
* Gets Category tree without apply user permission
* @return JsonResponse
*/
public function listCategoryTreeAction(): JsonResponse
{
$categoryTrees = $this->findCategoryTrees->execute(false);
$normalizeCategoryTrees = array_map(fn (CategoryTree $categoryTree) => $categoryTree->normalize(), $categoryTrees);

return new JsonResponse($normalizeCategoryTrees);
}

/**
* Saves new channel
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ services:
- '@pim_catalog.factory.channel'
- '@validator'
- '@oro_security.security_facade'
- '@akeneo.enrichment.public_api.find_category_trees'

pim_api.controller.currency:
public: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ pim_enrich_channel_index:
pim_enrich_channel_create:
path: /create

pim_enrich_channel_category_trees_get:
path: /category-tree
defaults: { _controller: pim_enrich.controller.rest.channel:listCategoryTreeAction}
methods: [GET]

pim_enrich_channel_edit:
path: /{code}/edit
requirements:
Expand Down
4 changes: 4 additions & 0 deletions src/Akeneo/Channel/back/tests/.php_cd.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@
// TIP-1012: Create a Measure component
'Akeneo\Tool\Bundle\MeasureBundle\Manager\MeasureManager',

// PIM-10485: Get Category Tree without apply permission
'Akeneo\Pim\Enrichment\Component\Category\Query\PublicApi\CategoryTree',
'Akeneo\Pim\Enrichment\Component\Category\Query\PublicApi\FindCategoryTrees',

'Akeneo\Connectivity\Connection\Infrastructure\Apps\Security\ScopeMapperInterface',
])->in('Akeneo\Channel\Infrastructure'),
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,35 +17,19 @@
*/
class CategoryController
{
/** @var CategoryRepositoryInterface */
protected $repository;

/** @var CategoryExtension */
protected $twigExtension;

/** @var NormalizerInterface */
protected $normalizer;

/** @var CollectionFilterInterface */
protected $collectionFilter;

public function __construct(
CategoryRepositoryInterface $repository,
CategoryExtension $twigExtension,
NormalizerInterface $normalizer,
CollectionFilterInterface $collectionFilter
protected CategoryRepositoryInterface $repository,
protected CategoryExtension $twigExtension,
protected NormalizerInterface $normalizer,
protected CollectionFilterInterface $collectionFilter
) {
$this->repository = $repository;
$this->twigExtension = $twigExtension;
$this->normalizer = $normalizer;
$this->collectionFilter = $collectionFilter;
}

/**
* List children categories
*
* @param Request $request The request object
* @param int $identifier The parent category identifier
* @param Request $request The request object
* @param int $identifier The parent category identifier
*
* @return JsonResponse
*/
Expand Down Expand Up @@ -83,7 +67,7 @@ public function listSelectedChildrenAction(Request $request, $identifier): JsonR
*
* @return JsonResponse
*/
public function listAction()
public function listAction(): JsonResponse
{
$categories = $this->repository->findBy(
[
Expand All @@ -98,12 +82,7 @@ public function listAction()
);
}

/**
* @param string $identifier
*
* @return JsonResponse
*/
public function getAction($identifier)
public function getAction(string $identifier): JsonResponse
{
$category = $this->repository->findOneByIdentifier($identifier);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,25 @@
*/
class SqlFindCategoryTrees implements FindCategoryTrees
{
private CategoryRepositoryInterface $categoryRepository;
private TranslationNormalizer $translationNormalizer;
private CollectionFilterInterface $collectionFilter;

public function __construct(
CategoryRepositoryInterface $categoryRepository,
TranslationNormalizer $translationNormalizer,
CollectionFilterInterface $collectionFilter
private CategoryRepositoryInterface $categoryRepository,
private TranslationNormalizer $translationNormalizer,
private CollectionFilterInterface $collectionFilter
) {
$this->categoryRepository = $categoryRepository;
$this->translationNormalizer = $translationNormalizer;
$this->collectionFilter = $collectionFilter;
}

public function execute(): array
/**
* @return CategoryTree[]
*/
public function execute(bool $applyPermission = true): array
{
$categories = $this->categoryRepository->findBy(['parent' => null]);
$categoriesWithPermissions = $this->applyPermissions($categories);

return $this->categoryTrees($categoriesWithPermissions);
if ($applyPermission) {
$categories = $this->applyPermissions($categories);
}

return $this->categoryTrees($categories);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,12 @@ config:
urls:
get: pim_enrich_family_variant_rest_get
channel:
module: pim/base-fetcher
module: pim/channel-fetcher
options:
urls:
list: pim_enrich_channel_rest_index
get: pim_enrich_channel_rest_get
list_channel_category_tree: pim_enrich_channel_category_trees_get
locale:
module: pim/locale-fetcher
options:
Expand Down Expand Up @@ -576,6 +577,7 @@ config:
pim/product-fetcher: pimui/js/fetcher/product-fetcher
pim/product-model-fetcher: pimui/js/fetcher/product-model-fetcher
pim/family-fetcher: pimui/js/fetcher/family-fetcher
pim/channel-fetcher: pimui/js/fetcher/channel-fetcher

# Remover
pim/remover/base: pimui/js/remover/base-remover
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ define([
return this;
}

FetcherRegistry.getFetcher('category')
.fetchAll()
FetcherRegistry.getFetcher('channel')
.fetchCategoryTree()
.then(
function (categories) {
if (0 === this.getFormData().category_tree.length) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict';

define(['underscore', 'pim/base-fetcher'], function (_, BaseFetcher) {
return BaseFetcher.extend({
/**
* Fetch only the parent category tree
* User right will not be apply.
* @return {Promise}
*/
fetchCategoryTree: function () {
return this.getJSON(this.options.urls.list_channel_category_tree).then(_.identity).promise();
},
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace AkeneoTest\Pim\Enrichment\Integration\Category;

use Akeneo\Pim\Enrichment\Bundle\Filter\CollectionFilterInterface;
use Akeneo\Pim\Enrichment\Component\Category\Model\Category;
use Akeneo\Pim\Enrichment\Component\Category\Query\PublicApi\CategoryTree;
use Akeneo\Pim\Enrichment\Bundle\Storage\Sql\Category\SqlFindCategoryTrees;
use Akeneo\Test\Integration\Configuration;
Expand All @@ -17,6 +18,7 @@ final class SqlFindCategoryTreesIntegration extends TestCase
public function setUp(): void
{
parent::setUp();
$this->createCategory(['code' => 'sales']);
$this->sqlFindCategoryTrees = $this->createQuery(new AllowAll());
}

Expand All @@ -30,7 +32,12 @@ public function it_fetches_the_category_trees(): void
$masterTree->code = 'master';
$masterTree->labels = ['en_US' => 'Master catalog'];

$expected = [$masterTree];
$saleTree = new CategoryTree();
$saleTree->id = $actual[1]->id;
$saleTree->code = 'sales';
$saleTree->labels = [];

$expected = [$masterTree, $saleTree];
self::assertEquals($expected, $actual);
}

Expand All @@ -44,6 +51,42 @@ public function it_filters_the_category_trees(): void
self::assertEmpty($actual);
}

/** @test */
public function it_does_not_apply_permission_on_category_trees(): void
{
$actual = $this->sqlFindCategoryTrees->execute(false);

$masterTree = new CategoryTree();
$masterTree->id = $actual[0]->id;
$masterTree->code = 'master';
$masterTree->labels = ['en_US' => 'Master catalog'];

$saleTree = new CategoryTree();
$saleTree->id = $actual[1]->id;
$saleTree->code = 'sales';
$saleTree->labels = [];

$expected = [$masterTree, $saleTree];
self::assertEquals($expected, $actual);
}

/** @test */
public function it_applies_permission_on_category_trees(): void
{
$this->sqlFindCategoryTrees = $this->createQuery(new DenySalesCategory());

$actual = $this->sqlFindCategoryTrees->execute();

$masterTree = new CategoryTree();
$masterTree->id = $actual[0]->id;
$masterTree->code = 'master';
$masterTree->labels = ['en_US' => 'Master catalog'];

$expected = [$masterTree];
self::assertEquals($expected, $actual);
}


protected function getConfiguration(): Configuration
{
return $this->catalog->useMinimalCatalog();
Expand Down Expand Up @@ -84,3 +127,16 @@ public function supportsCollection($collection, $type, array $options = [])
return true;
}
}

class DenySalesCategory implements CollectionFilterInterface
{
public function filterCollection($collection, $type, array $options = [])
{
return array_filter($collection, fn (Category $category) => $category->getCode() != 'sales');
}

public function supportsCollection($collection, $type, array $options = [])
{
return true;
}
}

0 comments on commit bf152d1

Please sign in to comment.