Skip to content

Commit

Permalink
try to register shop with client api secret
Browse files Browse the repository at this point in the history
  • Loading branch information
dylanDenizonPresta committed Feb 10, 2025
1 parent aafd9cd commit cd1e268
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 20 deletions.
15 changes: 15 additions & 0 deletions ps_mbo.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,21 @@ class ps_mbo extends Module
'AdminModulesManage',
];

/**
* @var string
*/
public const API_CLIENT_NAME = "ps_mbo";

/**
* @var string
*/
public const API_CLIENT_ID = "mbo_client";

/**
* @var integer
*/
public const API_CLIENT_LIFETIME = 600;

public $configurationList = [
'PS_MBO_SHOP_ADMIN_UUID' => '', // 'ADMIN' because there will be only one for all shops in a multishop context
'PS_MBO_SHOP_ADMIN_MAIL' => '',
Expand Down
54 changes: 34 additions & 20 deletions src/Traits/HaveShopOnExternalService.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
use PrestaShop\Module\Mbo\Helpers\ErrorHelper;
use PrestaShop\Module\Mbo\Helpers\Uuid;
use PrestaShop\PrestaShop\Core\Domain\Employee\Exception\EmployeeException;
use PrestaShop\PrestaShop\Core\Domain\ApiClient\Command\AddApiClientCommand;
use PrestaShop\PrestaShop\Core\Domain\ApiClient\Command\DeleteApiClientCommand;

trait HaveShopOnExternalService
{
Expand Down Expand Up @@ -83,16 +85,8 @@ private function unregisterShop(): void
*/
private function callServiceWithLockFile(string $method, array $params = []): void
{
$lockFile = $this->moduleCacheDir . $method . '.lock';

try {
$this->getAdminAuthenticationProvider()->clearCache();

// If the module is installed via command line or somehow the ADMIN_DIR is not defined,
// we ignore the shop registration, so it will be done at any action on the backoffice
if (php_sapi_name() === 'cli' || !defined('_PS_ADMIN_DIR_')) {
throw new \Exception();
}
/** @var Client $distributionApi */
$distributionApi = $this->get(Client::class);
if (!method_exists($distributionApi, $method)) {
Expand All @@ -103,25 +97,14 @@ private function callServiceWithLockFile(string $method, array $params = []): vo

// Add the default params
$params = array_merge($params, [
'mbo_api_user_token' => $this->getAdminAuthenticationProvider()->getAdminToken(),
'mbo_client_secret' => $this->createAPIClientCommand()->getSecret(),
'accounts_token' => $accountsDataProvider ? $accountsDataProvider->getAccountsToken() : null,
'accounts_shop_id' => $accountsDataProvider ? $accountsDataProvider->getAccountsShopId() : null,
]);
$distributionApi->setBearer($this->getAdminAuthenticationProvider()->getMboJWT());
$distributionApi->{$method}($params);

if (file_exists($lockFile)) {
unlink($lockFile);
}
} catch (\Exception $exception) {
// Create the lock file
if (!file_exists($lockFile)) {
if (!is_dir($this->moduleCacheDir)) {
mkdir($this->moduleCacheDir, 0777, true);
}
$f = fopen($lockFile, 'w+');
fclose($f);
}
ErrorHelper::reportError($exception, [
'method' => $method,
'params' => $params,
Expand Down Expand Up @@ -194,4 +177,35 @@ private function syncApiConfig()
}
$commandHandler->handle($command);
}

private function createAPIClientCommand()
{
try {
return $this->apiClientRepository->getByClientId(self::API_CLIENT_ID);
} catch (NoResultException) {
}

$command = new AddApiClientCommand(
self::API_CLIENT_NAME,
self::API_CLIENT_ID,
true,
'',
self::API_CLIENT_LIFETIME,
["module_read", "module_write"]
);

$commandBus = $this->getContainer()->get('prestashop.core.command_bus');
return $commandBus->handle($command);
}

private function deleteAPIClientCommand()
{
try {
$apiClient = $this->apiClientRepository->getByClientId(self::API_CLIENT_ID);
$command = new DeleteApiClientCommand($apiClient->getId());
$this->commandBus->handle($command);
} catch (NoResultException) {
return;
}
}
}

0 comments on commit cd1e268

Please sign in to comment.