-
Notifications
You must be signed in to change notification settings - Fork 347
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
586009b
commit cd21bba
Showing
7 changed files
with
321 additions
and
0 deletions.
There are no files selected for viewing
80 changes: 80 additions & 0 deletions
80
src/module-elasticsuite-catalog/Model/CategoryPermissions/Filter/Provider.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
<?php | ||
/** | ||
* DISCLAIMER | ||
* Do not edit or add to this file if you wish to upgrade Smile ElasticSuite to newer | ||
* versions in the future. | ||
* | ||
* @category Smile | ||
* @package Smile\ElasticsuiteCatalog | ||
* @author Romain Ruaud <[email protected]> | ||
* @copyright 2025 Smile | ||
* @license Open Software License ("OSL") v. 3.0 | ||
*/ | ||
namespace Smile\ElasticsuiteCatalog\Model\CategoryPermissions\Filter; | ||
|
||
use Smile\ElasticsuiteCore\Search\Request\Query\QueryFactory; | ||
use Smile\ElasticsuiteCore\Search\Request\QueryInterface; | ||
|
||
/** | ||
* Query Provider for Catalog permissions | ||
* | ||
* @category Smile | ||
* @package Smile\ElasticsuiteCatalog | ||
* @author Romain Ruaud <[email protected]> | ||
*/ | ||
class Provider | ||
{ | ||
/** | ||
* @var \Smile\ElasticsuiteCore\Search\Request\Query\QueryFactory | ||
*/ | ||
private $queryFactory; | ||
|
||
/** | ||
* @param \Smile\ElasticsuiteCore\Search\Request\Query\QueryFactory $queryFactory Query Factory | ||
*/ | ||
public function __construct(QueryFactory $queryFactory) | ||
{ | ||
$this->queryFactory = $queryFactory; | ||
} | ||
|
||
/** | ||
* Build a clause to filter on products which are not available for the current customer group id. | ||
* By default, the clause is a "NOT equal to -2" in legacy Magento.s | ||
* | ||
* @param int $customerGroupId Customer Group Id | ||
* @param int $value Permission value | ||
* @param string $operator Operator to use (default is mustNot because the legacy query is "is not denied") | ||
* | ||
* @return \Smile\ElasticsuiteCore\Search\Request\QueryInterface|null | ||
*/ | ||
public function getQueryFilter(int $customerGroupId, int $value, string $operator = 'mustNot') : ?QueryInterface | ||
{ | ||
$query = $this->queryFactory->create( | ||
QueryInterface::TYPE_NESTED, | ||
[ | ||
'path' => 'category_permissions', | ||
'query' => $this->queryFactory->create( | ||
QueryInterface::TYPE_BOOL, | ||
[ | ||
'must' => [ | ||
$this->queryFactory->create( | ||
QueryInterface::TYPE_TERM, | ||
['field' => 'category_permissions.customer_group_id', 'value' => $customerGroupId] | ||
), | ||
$this->queryFactory->create( | ||
QueryInterface::TYPE_TERM, | ||
['field' => 'category_permissions.permission', 'value' => $value] | ||
), | ||
], | ||
] | ||
), | ||
] | ||
); | ||
|
||
if ('mustNot' === $operator) { | ||
$query = $this->queryFactory->create(QueryInterface::TYPE_NOT, ['query' => $query]); | ||
} | ||
|
||
return $query; | ||
} | ||
} |
115 changes: 115 additions & 0 deletions
115
...le-elasticsuite-catalog/Model/Product/Indexer/Fulltext/Datasource/CategoryPermissions.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
<?php | ||
/** | ||
* DISCLAIMER | ||
* | ||
* Do not edit or add to this file if you wish to upgrade to newer versions in the future. | ||
* | ||
* @category Smile | ||
* @package Smile\ElasticsuiteCatalog | ||
* @author Romain Ruaud <[email protected]> | ||
* @copyright 2025 Smile | ||
* @license Open Software License ("OSL") v. 3.0 | ||
*/ | ||
|
||
declare(strict_types = 1); | ||
|
||
namespace Smile\ElasticsuiteCatalog\Model\Product\Indexer\Fulltext\Datasource; | ||
|
||
use Magento\Framework\ObjectManagerInterface; | ||
use Smile\ElasticsuiteCore\Api\Index\DatasourceInterface; | ||
|
||
/** | ||
* Category Permissions data source. | ||
* | ||
* @category Smile | ||
* @package Smile\ElasticsuiteCatalog | ||
* @author Romain Ruaud <[email protected]> | ||
*/ | ||
class CategoryPermissions implements DatasourceInterface | ||
{ | ||
/** | ||
* @var \Magento\Framework\ObjectManagerInterface | ||
*/ | ||
private $objectManager; | ||
|
||
/** | ||
* @var \Magento\CatalogPermissions\Model\ResourceModel\Permission\Index|null|false | ||
*/ | ||
private $categoryPermissionsIndex; | ||
|
||
/** | ||
* @var null|boolean | ||
*/ | ||
private $isEnabled = null; | ||
|
||
/** | ||
* @param \Magento\Framework\ObjectManagerInterface $objectManager Object Manager | ||
*/ | ||
public function __construct( | ||
ObjectManagerInterface $objectManager | ||
) { | ||
$this->objectManager = $objectManager; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function addData($storeId, array $indexData) | ||
{ | ||
if ($this->isEnabled() && $this->getPermissionsIndex() !== false) { | ||
$permissionData = $this->getPermissionsIndex()->getIndexForProduct(array_keys($indexData), null, $storeId); | ||
|
||
foreach ($permissionData as $permission) { | ||
$indexData[(int) $permission['product_id']]['category_permissions'][] = [ | ||
'customer_group_id' => (int) $permission['customer_group_id'], | ||
'permission' => (int) $permission['grant_catalog_category_view'], | ||
]; | ||
} | ||
} | ||
|
||
return $indexData ?? []; | ||
} | ||
|
||
/** | ||
* Fetch CategoryPermissions resource model, if the class exist. | ||
* | ||
* @return false|\Magento\CatalogPermissions\Model\ResourceModel\Permission\Index | ||
*/ | ||
private function getPermissionsIndex() | ||
{ | ||
if (null === $this->categoryPermissionsIndex) { | ||
$this->categoryPermissionsIndex = false; | ||
try { | ||
// Class will be missing if not using Adobe Commerce. | ||
$this->categoryPermissionsIndex = $this->objectManager->get( | ||
\Magento\CatalogPermissions\Model\ResourceModel\Permission\Index::class | ||
); | ||
} catch (\Exception $exception) { | ||
; // Nothing to do, it's already kinda hacky to allow this to happen. | ||
} | ||
} | ||
|
||
return $this->categoryPermissionsIndex; | ||
} | ||
|
||
/** | ||
* Check if category permissions feature is enabled. | ||
* | ||
* @return bool | ||
*/ | ||
private function isEnabled() | ||
{ | ||
if (null === $this->isEnabled) { | ||
$this->isEnabled = false; | ||
try { | ||
// Class will be missing if not using Adobe Commerce. | ||
$config = $this->objectManager->get(\Magento\CatalogPermissions\App\ConfigInterface::class); | ||
$this->isEnabled = $config->isEnabled(); | ||
} catch (\Exception $exception) { | ||
; // Nothing to do, it's already kinda hacky to allow this to happen. | ||
} | ||
} | ||
|
||
return $this->isEnabled; | ||
} | ||
} |
109 changes: 109 additions & 0 deletions
109
...lasticsuite-catalog/Model/Product/Search/Request/Container/Filter/CategoryPermissions.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
<?php | ||
/** | ||
* DISCLAIMER | ||
* | ||
* Do not edit or add to this file if you wish to upgrade Smile ElasticSuite to newer | ||
* versions in the future. | ||
* | ||
* @category Smile | ||
* @package Smile\ElasticSuiteCatalog | ||
* @author Romain Ruaud <[email protected]> | ||
* @copyright 2025 Smile | ||
* @license Open Software License ("OSL") v. 3.0 | ||
*/ | ||
namespace Smile\ElasticsuiteCatalog\Model\Product\Search\Request\Container\Filter; | ||
|
||
use Smile\ElasticsuiteCore\Api\Search\Request\Container\FilterInterface; | ||
|
||
/** | ||
* Category Permissions filter. | ||
* | ||
* @category Smile | ||
* @package Smile\ElasticsuiteCatalog | ||
* @author Romain Ruaud <[email protected]> | ||
*/ | ||
class CategoryPermissions implements FilterInterface | ||
{ | ||
/** | ||
* @var \Smile\ElasticsuiteCore\Search\Request\Query\QueryFactory | ||
*/ | ||
private $queryFactory; | ||
|
||
/** | ||
* @var \Magento\Customer\Model\Session | ||
*/ | ||
private $customerSession; | ||
|
||
/** | ||
* @var \Smile\ElasticsuiteCatalog\Model\CategoryPermissions\Filter\Provider | ||
*/ | ||
private $categoryPermissionsFilter; | ||
|
||
/** | ||
* @var \Magento\Framework\ObjectManagerInterface | ||
*/ | ||
private $objectManager; | ||
|
||
/** | ||
* @var null|boolean | ||
*/ | ||
private $isEnabled = null; | ||
|
||
/** | ||
* Search Blacklist filter constructor. | ||
* | ||
* @param \Smile\ElasticsuiteCore\Search\Request\Query\QueryFactory $queryFactory Query Factory | ||
* @param \Smile\ElasticsuiteCatalog\Model\CategoryPermissions\Filter\Provider $categoryPermissionsFilter Query Filter for Permissions | ||
* @param \Magento\Customer\Model\Session $customerSession Customer Session | ||
* @param \Magento\Framework\ObjectManagerInterface $objectManager Object Manager | ||
*/ | ||
public function __construct( | ||
\Smile\ElasticsuiteCore\Search\Request\Query\QueryFactory $queryFactory, | ||
\Smile\ElasticsuiteCatalog\Model\CategoryPermissions\Filter\Provider $categoryPermissionsFilter, | ||
\Magento\Customer\Model\Session $customerSession, | ||
\Magento\Framework\ObjectManagerInterface $objectManager | ||
) { | ||
$this->queryFactory = $queryFactory; | ||
$this->categoryPermissionsFilter = $categoryPermissionsFilter; | ||
$this->customerSession = $customerSession; | ||
$this->objectManager = $objectManager; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function getFilterQuery() | ||
{ | ||
$query = null; | ||
|
||
if ($this->isEnabled()) { | ||
$query = $this->categoryPermissionsFilter->getQueryFilter( | ||
$this->customerSession->getCustomerGroupId(), | ||
-2 // Cannot use \Magento\CatalogPermissions\Model::PERMISSION_DENY because the class can be missing. | ||
); | ||
} | ||
|
||
return $query; | ||
} | ||
|
||
/** | ||
* Check if category permissions feature is enabled. | ||
* | ||
* @return bool | ||
*/ | ||
private function isEnabled() | ||
{ | ||
if (null === $this->isEnabled) { | ||
$this->isEnabled = false; | ||
try { | ||
// Class will be missing if not using Adobe Commerce. | ||
$config = $this->objectManager->get(\Magento\CatalogPermissions\App\ConfigInterface::class); | ||
$this->isEnabled = $config->isEnabled(); | ||
} catch (\Exception $exception) { | ||
; // Nothing to do, it's already kinda hacky to allow this to happen. | ||
} | ||
} | ||
|
||
return $this->isEnabled; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters