Skip to content

Commit

Permalink
Merge pull request PrestaShop#37745 from jolelievre/product-api-multi…
Browse files Browse the repository at this point in the history
…shop

Improve ShopCollection list of shop IDs in API, and test API validation
  • Loading branch information
jolelievre authored Jan 6, 2025
2 parents 10695c7 + 0391fd6 commit a7c53a3
Show file tree
Hide file tree
Showing 19 changed files with 384 additions and 152 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
"prestashop/hummingbird": "^1.0.0",
"prestashop/pagesnotfound": "^2",
"prestashop/productcomments": "^7.0",
"prestashop/ps_apiresources": "dev-dev",
"prestashop/ps_apiresources": "dev-product-multishop-validation",
"prestashop/ps_banner": "^2",
"prestashop/ps_bestsellers": "^1.0",
"prestashop/ps_brandlist": "^1.0",
Expand Down Expand Up @@ -212,7 +212,7 @@
},
"ps_apiresources": {
"type": "vcs",
"url": "https://github.com/PrestaShop/ps_apiresources.git"
"url": "https://github.com/jolelievre/ps_apiresources.git"
}
},
"minimum-stability": "dev",
Expand Down
17 changes: 8 additions & 9 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/OSL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to https://devdocs.prestashop.com/ for more information.
*
* @author PrestaShop SA and Contributors <[email protected]>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
*/

namespace PrestaShop\PrestaShop\Adapter\ApiClient\CommandHandler;

use Doctrine\ORM\Exception\ORMException;
use Doctrine\ORM\NoResultException;
use PrestaShop\PrestaShop\Core\CommandBus\Attributes\AsCommandHandler;
use PrestaShop\PrestaShop\Core\Domain\ApiClient\Command\ForceApiClientSecretCommand;
use PrestaShop\PrestaShop\Core\Domain\ApiClient\CommandHandler\ForceApiClientSecretHandlerInterface;
use PrestaShop\PrestaShop\Core\Domain\ApiClient\Exception\ApiClientNotFoundException;
use PrestaShop\PrestaShop\Core\Domain\ApiClient\Exception\CannotGenerateApiClientSecretException;
use PrestaShopBundle\Entity\Repository\ApiClientRepository;
use Symfony\Component\PasswordHasher\PasswordHasherInterface;

#[AsCommandHandler]
class ForceApiClientSecretHandler implements ForceApiClientSecretHandlerInterface
{
public function __construct(
private readonly ApiClientRepository $repository,
private readonly PasswordHasherInterface $passwordHasher
) {
}

public function handle(ForceApiClientSecretCommand $command): void
{
try {
$apiClient = $this->repository->getById($command->getApiClientId()->getValue());
} catch (NoResultException $e) {
throw new ApiClientNotFoundException(sprintf('Could not find Api client with ID %s', $command->getApiClientId()->getValue()), 0, $e);
}

try {
$apiClient->setClientSecret($this->passwordHasher->hash($command->getSecret()->getValue()));
$this->repository->save($apiClient);
} catch (ORMException $e) {
throw new CannotGenerateApiClientSecretException('Could not generate new token Api client', 0, $e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,8 @@ public function handle(GetProductForEditing $query): ProductForEditing
$this->getAttachments($query->getProductId()),
$this->getProductStockInformation($product),
$this->getVirtualProductFile($product),
$this->getCover($query->getProductId(), $product->getShopId())
$this->getCover($query->getProductId(), $product->getShopId()),
array_map(fn (ShopId $shopId) => $shopId->getValue(), $this->productRepository->getAssociatedShopIds($query->getProductId()))
);
}

Expand Down
55 changes: 55 additions & 0 deletions src/Core/Domain/ApiClient/Command/ForceApiClientSecretCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/OSL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to https://devdocs.prestashop.com/ for more information.
*
* @author PrestaShop SA and Contributors <[email protected]>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
*/

namespace PrestaShop\PrestaShop\Core\Domain\ApiClient\Command;

use PrestaShop\PrestaShop\Core\Domain\ApiClient\ValueObject\ApiClientId;
use PrestaShop\PrestaShop\Core\Domain\ApiClient\ValueObject\ApiClientSecret;

class ForceApiClientSecretCommand
{
private ApiClientId $apiClientId;

private ApiClientSecret $secret;

public function __construct(
int $apiClientId,
string $secret
) {
$this->apiClientId = new ApiClientId($apiClientId);
$this->secret = new ApiClientSecret($secret);
}

public function getApiClientId(): ApiClientId
{
return $this->apiClientId;
}

public function getSecret(): ApiClientSecret
{
return $this->secret;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/OSL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to https://devdocs.prestashop.com/ for more information.
*
* @author PrestaShop SA and Contributors <[email protected]>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
*/

namespace PrestaShop\PrestaShop\Core\Domain\ApiClient\CommandHandler;

use PrestaShop\PrestaShop\Core\Domain\ApiClient\Command\ForceApiClientSecretCommand;

interface ForceApiClientSecretHandlerInterface
{
public function handle(ForceApiClientSecretCommand $command): void;
}
53 changes: 53 additions & 0 deletions src/Core/Domain/ApiClient/ValueObject/ApiClientSecret.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/OSL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to https://devdocs.prestashop.com/ for more information.
*
* @author PrestaShop SA and Contributors <[email protected]>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
*/

namespace PrestaShop\PrestaShop\Core\Domain\ApiClient\ValueObject;

use PrestaShop\PrestaShop\Core\Domain\ApiClient\Exception\ApiClientConstraintException;

class ApiClientSecret
{
public const MIN_SIZE = 32;
public const MAX_SIZE = 72;

public function __construct(
private string $value,
) {
$this->assertSecretValue($this->value);
}

public function getValue(): string
{
return $this->value;
}

private function assertSecretValue(string $value)
{
if (strlen($value) < self::MIN_SIZE || strlen($value) > self::MAX_SIZE) {
throw new ApiClientConstraintException(sprintf('Invalid api client secret "%s".', var_export($value, true)), ApiClientConstraintException::INVALID_SECRET);
}
}
}
Loading

0 comments on commit a7c53a3

Please sign in to comment.