forked from PrestaShop/PrestaShop
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request PrestaShop#37745 from jolelievre/product-api-multi…
…shop Improve ShopCollection list of shop IDs in API, and test API validation
- Loading branch information
Showing
19 changed files
with
384 additions
and
152 deletions.
There are no files selected for viewing
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
63 changes: 63 additions & 0 deletions
63
src/Adapter/ApiClient/CommandHandler/ForceApiClientSecretHandler.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,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); | ||
} | ||
} | ||
} |
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
55 changes: 55 additions & 0 deletions
55
src/Core/Domain/ApiClient/Command/ForceApiClientSecretCommand.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,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; | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
src/Core/Domain/ApiClient/CommandHandler/ForceApiClientSecretHandlerInterface.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,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; | ||
} |
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,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); | ||
} | ||
} | ||
} |
Oops, something went wrong.