Skip to content

Commit

Permalink
MM-22: Add logic to process changes in Sales Channel (#46)
Browse files Browse the repository at this point in the history
- Added voter that use to check is sales channel group links to any integration and forbid delete action for this case
- Added logic to process enabling/disabling, removing Sales Channel
  • Loading branch information
alexandr-parkhomenko authored Jun 1, 2020
1 parent 854eab6 commit d9db8bb
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,38 @@ public function findByChannel(SalesChannel $salesChannel)
return $this->aclHelper->apply($qb->getQuery())->getResult();
}

/**
* @param array $salesChannelIds
* @return int[]
*/
public function getProductIdsBySalesChannelIds(array $salesChannelIds): array
{
if (empty($salesChannelIds)) {
return [];
}

$subQb = $this->createQueryBuilder('innerProduct');
$subQb
->select('1')
->innerJoin('innerProduct.channels', 'sc')
->where(
$subQb->expr()->andX(
$subQb->expr()->in('sc.id', ':salesChannelIds'),
$subQb->expr()->eq('innerProduct.id', 'product.id')
)
);

$qb = $this->createQueryBuilder('product');
$qb
->select('product.id')
->where($qb->expr()->exists($subQb))
->setParameter('salesChannelIds', $salesChannelIds);

$result = $this->aclHelper->apply($qb->getQuery())->getResult();

return \array_column($result, 'id');
}

/**
* Return products for specified price list and product IDs
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Marello\Bundle\SalesBundle\Acl\Voter;

use Marello\Bundle\SalesBundle\Entity\Repository\SalesChannelGroupRepository;
use Marello\Bundle\SalesBundle\Entity\SalesChannelGroup;
use Oro\Bundle\SecurityBundle\Acl\BasicPermission;
use Oro\Bundle\SecurityBundle\Acl\Voter\AbstractEntityVoter;

/**
* Disables deleting the sales channel group in case when it has attached integration
*/
class SalesChannelGroupVoter extends AbstractEntityVoter
{
/** @var array */
protected $supportedAttributes = [BasicPermission::DELETE];

/**
* {@inheritdoc}
*/
protected function getPermissionForAttribute($class, $identifier, $attribute)
{
/** @var SalesChannelGroupRepository $scgRepository */
$scgRepository = $this->doctrineHelper->getEntityRepository(SalesChannelGroup::class);

if ($scgRepository->hasAttachedIntegration($identifier)) {
return self::ACCESS_DENIED;
}

return self::ACCESS_ABSTAIN;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,20 @@ public function findSalesChannelGroupAttachedToIntegration(
): ?SalesChannelGroup {
return $this->findOneBy(['integrationChannel' => $integrationChannel]);
}

/**
* @param int $salesChannelGroupId
* @return bool
*/
public function hasAttachedIntegration(int $salesChannelGroupId): bool
{
$qb = $this->createQueryBuilder('scg');
$qb
->select('1')
->where($qb->expr()->eq('scg.id', ':salesChannelGroupId'))
->andWhere($qb->expr()->isNotNull('scg.integrationChannel'))
->setParameter('salesChannelGroupId', $salesChannelGroupId);

return (bool) $qb->getQuery()->getResult();
}
}
9 changes: 9 additions & 0 deletions src/Marello/Bundle/SalesBundle/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -164,3 +164,12 @@ services:
marello_sales.placeholder.filter:
public: true
class: 'Marello\Bundle\SalesBundle\Placeholder\PlaceholderFilter'

marello_sales.security.acl.voter.sales_channel_group:
class: 'Marello\Bundle\SalesBundle\Acl\Voter\SalesChannelGroupVoter'
arguments:
- "@oro_entity.doctrine_helper"
calls:
- [setClassName, ['Marello\Bundle\SalesBundle\Entity\SalesChannelGroup']]
tags:
- { name: security.voter }

0 comments on commit d9db8bb

Please sign in to comment.