Skip to content

Commit 6161093

Browse files
authored
Merge pull request #820 from algolia/update-b2b-permissions
Update B2B permissions condition
2 parents 9843d6b + 35e0d2c commit 6161093

File tree

5 files changed

+55
-23
lines changed

5 files changed

+55
-23
lines changed

Factory/CatalogPermissionsFactory.php

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

99
class CatalogPermissionsFactory
1010
{
11-
const CATALOG_PERMISSIONS_ENABLED_CONFIG_PATH = 'catalog/magento_catalogpermissions/enabled';
12-
1311
private $scopeConfig;
1412
private $moduleManager;
1513
private $objectManager;
@@ -29,13 +27,8 @@ public function __construct(
2927

3028
public function isCatalogPermissionsEnabled($storeId)
3129
{
32-
$isEnabled = $this->scopeConfig->isSetFlag(
33-
self::CATALOG_PERMISSIONS_ENABLED_CONFIG_PATH,
34-
\Magento\Store\Model\ScopeInterface::SCOPE_STORE,
35-
$storeId
36-
);
37-
38-
return $isEnabled && $this->isCatalogPermissionsModuleEnabled();
30+
return $this->isCatalogPermissionsModuleEnabled()
31+
&& $this->getCatalogPermissionsConfig()->isEnabled($storeId);
3932
}
4033

4134
private function isCatalogPermissionsModuleEnabled()
@@ -53,6 +46,11 @@ public function getCatalogPermissionsHelper()
5346
return $this->objectManager->create('\Magento\CatalogPermissions\Helper\Data');
5447
}
5548

49+
public function getCatalogPermissionsConfig()
50+
{
51+
return $this->objectManager->create('\Magento\CatalogPermissions\App\Config');
52+
}
53+
5654
public function getCategoryPermissionsCollection()
5755
{
5856
if (!$this->categoryPermissionsCollection) {

Factory/SharedCatalogFactory.php

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88

99
class SharedCatalogFactory
1010
{
11-
const SHARED_CATALOG_ENABLED_CONFIG_PATH = 'btob/website_configuration/sharedcatalog_active';
12-
1311
private $scopeConfig;
1412
private $moduleManager;
1513
private $objectManager;
@@ -29,13 +27,11 @@ public function __construct(
2927

3028
public function isSharedCatalogEnabled($storeId)
3129
{
32-
$isEnabled = $this->scopeConfig->isSetFlag(
33-
self::SHARED_CATALOG_ENABLED_CONFIG_PATH,
34-
\Magento\Store\Model\ScopeInterface::SCOPE_STORE,
35-
$storeId
36-
);
37-
38-
return $isEnabled && $this->isSharedCatalogModuleEnabled();
30+
return $this->isSharedCatalogModuleEnabled()
31+
&& $this->getSharedCatalogConfig()->isActive(
32+
\Magento\Store\Model\ScopeInterface::SCOPE_STORE,
33+
$storeId
34+
);
3935
}
4036

4137
private function isSharedCatalogModuleEnabled()
@@ -58,6 +54,11 @@ public function getSharedCatalogResource()
5854
return $this->objectManager->create('\Magento\SharedCatalog\Model\ResourceModel\SharedCatalog');
5955
}
6056

57+
public function getSharedCatalogConfig()
58+
{
59+
return $this->objectManager->create('\Magento\SharedCatalog\Model\Config');
60+
}
61+
6162
public function getSharedCategoryCollection()
6263
{
6364
if (!$this->sharedCategoryCollection) {

Model/Observer/CatalogPermissions/ApplyProductPermissionsFilter.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,44 @@
33
namespace Algolia\AlgoliaSearch\Model\Observer\CatalogPermissions;
44

55
use Algolia\AlgoliaSearch\Factory\CatalogPermissionsFactory;
6+
use Algolia\AlgoliaSearch\Factory\SharedCatalogFactory;
67
use Magento\Framework\Event\Observer;
78
use Magento\Framework\Event\ObserverInterface;
89
use Magento\Store\Model\StoreManager;
910

1011
class ApplyProductPermissionsFilter implements ObserverInterface
1112
{
13+
/** @var CatalogPermissionsFactory */
1214
private $permissionsFactory;
15+
16+
/** @var SharedCatalogFactory */
17+
private $sharedCatalogFactory;
18+
19+
/** @var StoreManager */
1320
private $storeManager;
1421

22+
/**
23+
* @param CatalogPermissionsFactory $permissionsFactory
24+
* @param SharedCatalogFactory $sharedCatalogFactory
25+
* @param StoreManager $storeManager
26+
*/
1527
public function __construct(
1628
CatalogPermissionsFactory $permissionsFactory,
29+
SharedCatalogFactory $sharedCatalogFactory,
1730
StoreManager $storeManager
1831
) {
1932
$this->permissionsFactory = $permissionsFactory;
33+
$this->sharedCatalogFactory = $sharedCatalogFactory;
2034
$this->storeManager = $storeManager;
2135
}
2236

2337
public function execute(Observer $observer)
2438
{
2539
$storeId = $this->storeManager->getStore()->getId();
26-
if (!$this->permissionsFactory->isCatalogPermissionsEnabled($storeId)) {
40+
if (!$this->permissionsFactory->isCatalogPermissionsEnabled($storeId)
41+
|| ($this->permissionsFactory->getCatalogPermissionsHelper()->isAllowedCategoryView($storeId)
42+
&& !$this->sharedCatalogFactory->isSharedCatalogEnabled($storeId))
43+
) {
2744
return $this;
2845
}
2946

Model/Observer/CatalogPermissions/CategoryCollectionAddPermissions.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,11 @@ protected function addCatalogPermissionsData($collection, $categoryIds)
5454
foreach ($permissionsCollection as $categoryId => $permissions) {
5555
$permissions = explode(',', $permissions);
5656
foreach ($permissions as $permission) {
57-
list($customerGroupId, $level) = explode('_', $permission);
57+
$permission = explode('_', $permission);
58+
if (count($permission) < 2) { // prevent undefined
59+
continue;
60+
}
61+
list($customerGroupId, $level) = $permission;
5862
if ($category = $collection->getItemById($categoryId)) {
5963
$category->setData('customer_group_permission_' . $customerGroupId, (($level == -2 || $level != -1
6064
&& !$catalogPermissionsHelper->isAllowedCategoryView()) ? 0 : 1));
@@ -79,7 +83,11 @@ protected function addSharedCatalogData($collection, $categoryIds, $storeId)
7983
foreach ($sharedCollection as $categoryId => $permissions) {
8084
$permissions = explode(',', $permissions);
8185
foreach ($permissions as $permission) {
82-
list($customerGroupId, $level) = explode('_', $permission);
86+
$permission = explode('_', $permission);
87+
if (count($permission) < 2) { // prevent undefined
88+
continue;
89+
}
90+
list($customerGroupId, $level) = $permission;
8391
if ($category = $collection->getItemById($categoryId)) {
8492
$category->setData('shared_catalog_permission_' . $customerGroupId, $level == -1 ? 1 : 0);
8593
}

Model/Observer/CatalogPermissions/ProductCollectionAddPermissions.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,11 @@ protected function addProductPermissionsData($additionalData, $productIds, $stor
6363
foreach ($permissionsCollection as $productId => $permissions) {
6464
$permissions = explode(',', $permissions);
6565
foreach ($permissions as $permission) {
66-
list($permissionStoreId, $customerGroupId, $level) = explode('_', $permission);
66+
$permission = explode('_', $permission);
67+
if (count($permission) < 3) { // prevent undefined
68+
continue;
69+
}
70+
list($permissionStoreId, $customerGroupId, $level) = $permission;
6771
if ($permissionStoreId == $storeId) {
6872
$additionalData->addProductData($productId, [
6973
'customer_group_permission_' . $customerGroupId => (($level == -2 || $level != -1
@@ -89,7 +93,11 @@ protected function addSharedCatalogData($additionalData, $productIds)
8993
foreach ($sharedCollection as $productId => $permissions) {
9094
$permissions = explode(',', $permissions);
9195
foreach ($permissions as $permission) {
92-
list($customerGroupId, $level) = explode('_', $permission);
96+
$permission = explode('_', $permission);
97+
if (count($permission) < 2) { // prevent undefined
98+
continue;
99+
}
100+
list($customerGroupId, $level) = $permission;
93101
$additionalData->addProductData($productId, [
94102
'shared_catalog_permission_' . $customerGroupId => $level,
95103
]);

0 commit comments

Comments
 (0)