Skip to content

Commit

Permalink
refactor Sylius#13778 [Maintenance][API] Decouple resource metadata f…
Browse files Browse the repository at this point in the history
…rom ResetPasswordHandler (coldic3)

This PR was merged into the 1.10-dev branch.

Discussion
----------

| Q               | A
| --------------- | -----
| Branch?         | 1.12
| Bug fix?        | no
| New feature?    | no
| BC breaks?      | no
| Deprecations?   | no
| Related tickets |
| License         | MIT


Commits
-------

f83840f [Maintenance][API] Decouple resource metadata from ResetPasswordHandler
c343e28 [Maintenance][API] Update UPGRADE-API-1.12.md
  • Loading branch information
lchrusciel authored Mar 17, 2022
2 parents 6650e57 + c343e28 commit cfd4f84
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 20 deletions.
2 changes: 2 additions & 0 deletions UPGRADE-API-1.12.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,5 @@ Here is how the response looks like:
1. The `GET` `api/v2/shop/orders/{tokenValue}/payments/{payments}/methods` and `api/v2/shop/payments/{id}/methods` endpoints have been removed and changed into collection request with 2 parameters `api/v2/shop/payment-methods?paymentId={id}&tokenValue={token}`.
Now when we do not provide parameters in response it returns all available `paymentMethods` in channel.
Wrong parameters otherwise cause empty array `[]` in response and correct parameters return `paymentMethods` available for your `payment`.
1. The 2nd parameter `MetadataInterface` has been removed from `src/Sylius/Bundle/ApiBundle/CommandHandler/Account/ResetPasswordHandler` and a token TTL value must be used instead as the 3rd parameter.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

use Sylius\Bundle\ApiBundle\Command\Account\ResetPassword;
use Sylius\Component\Core\Model\ShopUserInterface;
use Sylius\Component\Resource\Metadata\MetadataInterface;
use Sylius\Component\User\Repository\UserRepositoryInterface;
use Sylius\Component\User\Security\PasswordUpdaterInterface;
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;
Expand All @@ -26,8 +25,8 @@ final class ResetPasswordHandler implements MessageHandlerInterface
{
public function __construct(
private UserRepositoryInterface $userRepository,
private MetadataInterface $metadata,
private PasswordUpdaterInterface $passwordUpdater
private PasswordUpdaterInterface $passwordUpdater,
private string $tokenTtl
) {
}

Expand All @@ -38,8 +37,7 @@ public function __invoke(ResetPassword $command): void

Assert::notNull($user, 'No user found with reset token: ' . $command->resetPasswordToken);

$resetting = $this->metadata->getParameter('resetting');
$lifetime = new \DateInterval($resetting['token']['ttl']);
$lifetime = new \DateInterval($this->tokenTtl);

if (!$user->isPasswordRequestNonExpired($lifetime)) {
throw new \InvalidArgumentException('Password reset token has expired');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,13 +160,8 @@

<service id="Sylius\Bundle\ApiBundle\CommandHandler\Account\ResetPasswordHandler">
<argument type="service" id="sylius.repository.shop_user" />
<argument type="service">
<service class="Sylius\Component\Resource\Metadata\MetadataInterface">
<factory service="sylius.resource_registry" method="get" />
<argument type="string">sylius.shop_user</argument>
</service>
</argument>
<argument type="service" id="sylius.security.password_updater" />
<argument type="string">%sylius.shop_user.token.password_reset.ttl%</argument>
<tag name="messenger.message_handler" bus="sylius.command_bus" />
<tag name="messenger.message_handler" bus="sylius_default.bus" />
</service>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
use Prophecy\Argument;
use Sylius\Bundle\ApiBundle\Command\Account\ResetPassword;
use Sylius\Component\Core\Model\ShopUserInterface;
use Sylius\Component\Resource\Metadata\MetadataInterface;
use Sylius\Component\User\Repository\UserRepositoryInterface;
use Sylius\Component\User\Security\PasswordUpdaterInterface;
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;
Expand All @@ -26,10 +25,9 @@ final class ResetPasswordHandlerSpec extends ObjectBehavior
{
function let(
UserRepositoryInterface $userRepository,
MetadataInterface $metadata,
PasswordUpdaterInterface $passwordUpdater
): void {
$this->beConstructedWith($userRepository, $metadata, $passwordUpdater);
$this->beConstructedWith($userRepository, $passwordUpdater, 'P5D');
}

function it_is_a_message_handler(): void
Expand All @@ -40,11 +38,9 @@ function it_is_a_message_handler(): void
function it_resets_password(
UserRepositoryInterface $userRepository,
ShopUserInterface $shopUser,
MetadataInterface $metadata,
PasswordUpdaterInterface $passwordUpdater
): void {
$userRepository->findOneBy(['passwordResetToken' => 'TOKEN'])->willReturn($shopUser);
$metadata->getParameter('resetting')->willReturn(['token' => ['ttl' => 'P5D']]);

$shopUser->isPasswordRequestNonExpired(Argument::that(function (\DateInterval $dateInterval) {
return $dateInterval->format('%d') === '5';
Expand All @@ -66,11 +62,9 @@ function it_resets_password(
function it_throws_exception_if_token_is_expired(
UserRepositoryInterface $userRepository,
ShopUserInterface $shopUser,
MetadataInterface $metadata,
PasswordUpdaterInterface $passwordUpdater
): void {
$userRepository->findOneBy(['passwordResetToken' => 'TOKEN'])->willReturn($shopUser);
$metadata->getParameter('resetting')->willReturn(['token' => ['ttl' => 'P5D']]);

$shopUser->isPasswordRequestNonExpired(Argument::that(function (\DateInterval $dateInterval) {
return $dateInterval->format('%d') === '5';
Expand All @@ -93,11 +87,9 @@ function it_throws_exception_if_token_is_expired(
function it_throws_exception_if_tokens_are_not_exact(
UserRepositoryInterface $userRepository,
ShopUserInterface $shopUser,
MetadataInterface $metadata,
PasswordUpdaterInterface $passwordUpdater
): void {
$userRepository->findOneBy(['passwordResetToken' => 'TOKEN'])->willReturn($shopUser);
$metadata->getParameter('resetting')->willReturn(['token' => ['ttl' => 'P5D']]);

$shopUser->isPasswordRequestNonExpired(Argument::that(function (\DateInterval $dateInterval) {
return $dateInterval->format('%d') === '5';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public function load(array $configs, ContainerBuilder $container): void

$loader->load('services.xml');

$this->createParameters($config['resources'], $container);
$this->createServices($config['resources'], $container);
$this->loadEncodersAwareServices($config['encoder'], $config['resources'], $container);
}
Expand Down Expand Up @@ -81,6 +82,13 @@ private function createServices(array $resources, ContainerBuilder $container):
}
}

private function createParameters(array $resources, ContainerBuilder $container): void
{
foreach ($resources as $userType => $config) {
$this->createResettingTokenParameters($userType, $config['user'], $container);
}
}

private function loadEncodersAwareServices(?string $globalEncoder, array $resources, ContainerBuilder $container): void
{
foreach ($resources as $userType => $config) {
Expand Down Expand Up @@ -274,4 +282,9 @@ private function registerUpdateUserEncoderListener(ContainerBuilder $container,
$updateUserEncoderListenerDefinition
);
}

private function createResettingTokenParameters(string $userType, array $config, ContainerBuilder $container)
{
$container->setParameter(sprintf('sylius.%s_user.token.password_reset.ttl', $userType), $config['resetting']['token']['ttl']);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,54 @@ public function it_creates_a_update_user_encoder_listener_for_each_user_type():
Assert::assertSame('_password', $shopUserListenerDefinition->getArgument(4));
}

/** @test */
public function it_creates_default_resetting_token_parameters_for_each_user_type(): void
{
$this->load([
'resources' => [
'admin' => [
'user' => [],
],
'shop' => [
'user' => [],
],
],
]);

Assert::assertSame('P1D', $this->container->getParameter('sylius.admin_user.token.password_reset.ttl'));
Assert::assertSame('P1D', $this->container->getParameter('sylius.shop_user.token.password_reset.ttl'));
}

/** @test */
public function it_creates_custom_resetting_token_parameters_for_each_user_type(): void
{
$this->load([
'resources' => [
'admin' => [
'user' => [
'resetting' => [
'token' => [
'ttl' => 'P5D',
],
],
],
],
'shop' => [
'user' => [
'resetting' => [
'token' => [
'ttl' => 'P2D',
],
],
],
],
],
]);

Assert::assertSame('P5D', $this->container->getParameter('sylius.admin_user.token.password_reset.ttl'));
Assert::assertSame('P2D', $this->container->getParameter('sylius.shop_user.token.password_reset.ttl'));
}

protected function getContainerExtensions(): array
{
return [
Expand Down

0 comments on commit cfd4f84

Please sign in to comment.