Skip to content

Commit

Permalink
Merge pull request #188 from bold-commerce/Q1-661
Browse files Browse the repository at this point in the history
Expose Bold modules installed and their versions through an api endpoint
  • Loading branch information
nmalevanec authored Nov 27, 2023
2 parents c4870ca + 6c4619c commit bfcba77
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 0 deletions.
1 change: 1 addition & 0 deletions Model/BoldIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
class BoldIntegration
{
private const API_RESOURCES = [
'Bold_Checkout::integration',
'Bold_Checkout::secret_create',
'Magento_Backend::store',
'Magento_Catalog::products',
Expand Down
123 changes: 123 additions & 0 deletions Setup/Patch/Data/AddIntegrationPermissionsPatch.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
<?php

declare(strict_types=1);

namespace Bold\Checkout\Setup\Patch\Data;

use Magento\Authorization\Model\UserContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\Patch\DataPatchInterface;

/**
* Perform integration permissions upgrade.
*/
class AddIntegrationPermissionsPatch implements DataPatchInterface
{
private const RESOURCE = 'Bold_Checkout::integration';

/**
* @var ModuleDataSetupInterface
*/
private $moduleDataSetup;

/**
* @param ModuleDataSetupInterface $moduleDataSetup
*/
public function __construct(
ModuleDataSetupInterface $moduleDataSetup
)
{
$this->moduleDataSetup = $moduleDataSetup;
}

/**
* Perform integration permissions upgrade.
*
* @return void
*/
public function apply(): void
{
$this->moduleDataSetup->startSetup();

$this->moduleDataSetup->getConnection()->delete(
$this->moduleDataSetup->getTable('authorization_rule'),
[
'resource_id = ?' => self::RESOURCE,
'role_id IN (?)' => $this->getRoleIdsToDelete(),
]
);
$this->moduleDataSetup->getConnection()->insertMultiple(
$this->moduleDataSetup->getTable('authorization_rule'),
$this->getNewAuthorizationRules()
);

$this->moduleDataSetup->endSetup();
}

/**
* Get role IDs to delete.
*
* @return array
*/
private function getRoleIdsToDelete(): array
{
$select = $this->moduleDataSetup->getConnection()
->select()
->from(['ar' => $this->moduleDataSetup->getTable('authorization_role')], 'role_id')
->join(
['i' => $this->moduleDataSetup->getTable('integration')],
'i.integration_id = ar.user_id',
[]
)
->where('ar.user_type = ?', UserContextInterface::USER_TYPE_INTEGRATION)
->where('i.name LIKE ?', 'BoldPlatformIntegration%');

return $this->moduleDataSetup->getConnection()->fetchCol($select);
}

/**
* Get new authorization rules data.
*
* @return array
*/
private function getNewAuthorizationRules(): array
{
$select = $this->moduleDataSetup->getConnection()
->select()
->from(['ar' => $this->moduleDataSetup->getTable('authorization_role')], ['role_id'])
->join(
['i' => $this->moduleDataSetup->getTable('integration')],
'i.integration_id = ar.user_id',
[]
)
->where('ar.user_type = ?', UserContextInterface::USER_TYPE_INTEGRATION)
->where('i.name LIKE ?', 'BoldPlatformIntegration%');
$roleIds = $this->moduleDataSetup->getConnection()->fetchCol($select);
$data = [];
foreach ($roleIds as $roleId) {
$data[] = [
'role_id' => $roleId,
'resource_id' => self::RESOURCE,
'permission' => 'allow',
];
}

return $data;
}

/**
* @inheritDoc
*/
public function getAliases()
{
return [];
}

/**
* @inheritDoc
*/
public static function getDependencies()
{
return [];
}
}

0 comments on commit bfcba77

Please sign in to comment.