mirror of
https://github.com/Sylius/Sylius.git
synced 2026-07-17 02:18:01 +00:00
Add new UserNotFound validator to the RequestResetPasswordToken object
This commit is contained in:
parent
623e184fe7
commit
e4eabd5f30
6 changed files with 188 additions and 0 deletions
|
|
@ -115,6 +115,12 @@
|
|||
<tag name="validator.constraint_validator" alias="sylius_api_shipment_already_shipped" />
|
||||
</service>
|
||||
|
||||
<service id="Sylius\Bundle\ApiBundle\Validator\Constraints\UserNotFoundValidator">
|
||||
<argument type="service" id="sylius.canonicalizer" />
|
||||
<argument type="service" id="sylius.repository.shop_user" />
|
||||
<tag name="validator.constraint_validator" alias="sylius_api_user_not_found" />
|
||||
</service>
|
||||
|
||||
<service id="Sylius\Bundle\ApiBundle\Validator\Constraints\ResetPasswordTokenExistsValidator">
|
||||
<argument type="service" id="sylius.repository.shop_user" />
|
||||
<tag name="validator.constraint_validator" alias="sylius_api_reset_password_token_exists" />
|
||||
|
|
|
|||
|
|
@ -24,5 +24,12 @@
|
|||
</option>
|
||||
</constraint>
|
||||
</property>
|
||||
<property name="email">
|
||||
<constraint name="Sylius\Bundle\ApiBundle\Validator\Constraints\UserNotFound">
|
||||
<option name="groups">
|
||||
<value>sylius</value>
|
||||
</option>
|
||||
</constraint>
|
||||
</property>
|
||||
</class>
|
||||
</constraint-mapping>
|
||||
|
|
|
|||
|
|
@ -33,3 +33,5 @@ sylius:
|
|||
not_found: 'The shipping method with %code% code does not exist.'
|
||||
not_available: 'The shipping method %name% is not available for this order. Please reselect your shipping method.'
|
||||
shipping_address_not_found: 'Order should be addressed first.'
|
||||
user:
|
||||
not_found: 'The user with %email% email does not exist.'
|
||||
|
|
|
|||
|
|
@ -0,0 +1,27 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Sylius package.
|
||||
*
|
||||
* (c) Paweł Jędrzejewski
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Sylius\Bundle\ApiBundle\Validator\Constraints;
|
||||
|
||||
use Symfony\Component\Validator\Constraint;
|
||||
|
||||
/** @experimental */
|
||||
final class UserNotFound extends Constraint
|
||||
{
|
||||
public string $message = 'sylius.user.not_found';
|
||||
|
||||
public function validatedBy(): string
|
||||
{
|
||||
return 'sylius_api_user_not_found';
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,49 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Sylius package.
|
||||
*
|
||||
* (c) Paweł Jędrzejewski
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Sylius\Bundle\ApiBundle\Validator\Constraints;
|
||||
|
||||
use Sylius\Component\Core\Model\ShopUserInterface;
|
||||
use Sylius\Component\User\Canonicalizer\CanonicalizerInterface;
|
||||
use Sylius\Component\User\Repository\UserRepositoryInterface;
|
||||
use Symfony\Component\Validator\Constraint;
|
||||
use Symfony\Component\Validator\ConstraintValidator;
|
||||
use Webmozart\Assert\Assert;
|
||||
|
||||
/** @experimental */
|
||||
final class UserNotFoundValidator extends ConstraintValidator
|
||||
{
|
||||
public function __construct(
|
||||
private CanonicalizerInterface $canonicalizer,
|
||||
private UserRepositoryInterface $shopUserRepository,
|
||||
) {
|
||||
}
|
||||
|
||||
public function validate($value, Constraint $constraint): void
|
||||
{
|
||||
Assert::string($value);
|
||||
|
||||
/** @var UserNotFound $constraint */
|
||||
Assert::isInstanceOf($constraint, UserNotFound::class);
|
||||
|
||||
$emailCanonical = $this->canonicalizer->canonicalize($value);
|
||||
/** @var ShopUserInterface $shopUser */
|
||||
$shopUser = $this->shopUserRepository->findOneByEmail($emailCanonical);
|
||||
|
||||
if (null !== $shopUser) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->context->addViolation($constraint->message, ['%email%' => $emailCanonical]);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,97 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Sylius package.
|
||||
*
|
||||
* (c) Paweł Jędrzejewski
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace spec\Sylius\Bundle\ApiBundle\Validator\Constraints;
|
||||
|
||||
use PhpSpec\ObjectBehavior;
|
||||
use Sylius\Bundle\ApiBundle\Validator\Constraints\UserNotFound;
|
||||
use Sylius\Component\Core\Model\ShopUserInterface;
|
||||
use Sylius\Component\User\Canonicalizer\CanonicalizerInterface;
|
||||
use Sylius\Component\User\Repository\UserRepositoryInterface;
|
||||
use Symfony\Component\Validator\Constraint;
|
||||
use Symfony\Component\Validator\ConstraintValidatorInterface;
|
||||
use Symfony\Component\Validator\Context\ExecutionContextInterface;
|
||||
|
||||
final class UserNotFoundValidatorSpec extends ObjectBehavior
|
||||
{
|
||||
function let(
|
||||
CanonicalizerInterface $canonicalizer,
|
||||
UserRepositoryInterface $userRepository
|
||||
): void {
|
||||
$this->beConstructedWith($canonicalizer, $userRepository);
|
||||
}
|
||||
|
||||
function it_is_a_constraint_validator(): void
|
||||
{
|
||||
$this->shouldImplement(ConstraintValidatorInterface::class);
|
||||
}
|
||||
|
||||
function it_throws_an_exception_if_value_is_not_a_string(): void
|
||||
{
|
||||
$this
|
||||
->shouldThrow(\InvalidArgumentException::class)
|
||||
->during('validate', [null, new class() extends Constraint {
|
||||
}])
|
||||
;
|
||||
}
|
||||
|
||||
function it_throws_an_exception_if_constraint_is_not_a_userNotFound_constraint(): void
|
||||
{
|
||||
$this
|
||||
->shouldThrow(\InvalidArgumentException::class)
|
||||
->during('validate', ['', new class() extends Constraint {
|
||||
}])
|
||||
;
|
||||
}
|
||||
|
||||
function it_does_not_add_violation_if_shop_user_exist(
|
||||
CanonicalizerInterface $canonicalizer,
|
||||
UserRepositoryInterface $userRepository,
|
||||
ExecutionContextInterface $executionContext,
|
||||
ShopUserInterface $shopUser
|
||||
): void {
|
||||
$this->initialize($executionContext);
|
||||
|
||||
$value = 'sylius@example.com';
|
||||
|
||||
$canonicalizer->canonicalize('sylius@example.com')->willReturn('sylius@example.com');
|
||||
|
||||
$userRepository->findOneByEmail('sylius@example.com')->willReturn($shopUser);
|
||||
|
||||
$executionContext
|
||||
->addViolation('sylius.user.not_found', ['%email%' => 'sylius@example.com'])
|
||||
->shouldNotBeCalled();
|
||||
|
||||
$this->validate($value, new UserNotFound());
|
||||
}
|
||||
|
||||
function it_adds_violation_if_reset_password_token_exists(
|
||||
CanonicalizerInterface $canonicalizer,
|
||||
UserRepositoryInterface $userRepository,
|
||||
ExecutionContextInterface $executionContext
|
||||
): void {
|
||||
$this->initialize($executionContext);
|
||||
|
||||
$value = 'sylius@example.com';
|
||||
|
||||
$canonicalizer->canonicalize('sylius@example.com')->willReturn('sylius@example.com');
|
||||
|
||||
$userRepository->findOneByEmail('sylius@example.com')->willReturn(null);
|
||||
|
||||
$executionContext
|
||||
->addViolation('sylius.user.not_found', ['%email%' => 'sylius@example.com'])
|
||||
->shouldBeCalled();
|
||||
|
||||
$this->validate($value, new UserNotFound());
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue