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
… right
  • Loading branch information
Antoine Trouve committed Jun 22, 2022
1 parent 308651e commit fed8021
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 54 deletions.
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,14 +32,16 @@ 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 +78,18 @@ public function getAction(Request $request, string $identifier): JsonResponse
);
}

/**
* Gets Category tree without user right filter
* @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_tree_get:
path: /category-tree
defaults: { _controller: pim_enrich.controller.rest.channel:listCategoryTreeAction}
methods: [GET]

pim_enrich_channel_edit:
path: /{code}/edit
requirements:
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 All @@ -53,7 +52,8 @@ private function applyPermissions(array $categories): array
*/
private function categoryTrees(
array $categoriesWithPermissions
): array {
): array
{
$translationNormalizer = $this->translationNormalizer;

return array_map(
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_tree_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();
},
});
});

0 comments on commit fed8021

Please sign in to comment.