-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ForAuthorizedUserOnlyValidator validation constraint
- Loading branch information
Showing
8 changed files
with
198 additions
and
11 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
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,23 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Symfony\Component\DependencyInjection\Loader\Configurator; | ||
|
||
use CommerceWeavers\SyliusTpayPlugin\Validator\Constraint\EncodedGooglePayTokenValidator; | ||
use CommerceWeavers\SyliusTpayPlugin\Validator\Constraint\ForAuthorizedUserOnlyValidator; | ||
|
||
return static function(ContainerConfigurator $container): void { | ||
$services = $container->services(); | ||
|
||
$services->set('commerce_weavers_sylius_tpay.validator.constraint.encoded_google_pay_token', EncodedGooglePayTokenValidator::class) | ||
->tag('validator.constraint_validator') | ||
; | ||
|
||
$services->set('commerce_weavers_sylius_tpay.validator.constraint.for_authorized_user_only', ForAuthorizedUserOnlyValidator::class) | ||
->args([ | ||
service('security.helper'), | ||
]) | ||
->tag('validator.constraint_validator') | ||
; | ||
}; |
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
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,23 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace CommerceWeavers\SyliusTpayPlugin\Validator\Constraint; | ||
|
||
use Symfony\Component\Validator\Constraint; | ||
|
||
final class ForAuthorizedUserOnly extends Constraint | ||
{ | ||
public const USER_NOT_AUTHORIZED_ERROR = 'c146928c-f22b-4802-ba90-5fb9952e7ee8'; | ||
|
||
public string $userNotAuthorizedErrorMessage = 'commerce_weavers_sylius_tpay.shop.pay.field.user_not_authorized'; | ||
|
||
protected static $errorNames = [ | ||
self::USER_NOT_AUTHORIZED_ERROR => 'USER_NOT_AUTHORIZED_ERROR', | ||
]; | ||
|
||
public function getTargets(): string | ||
{ | ||
return self::PROPERTY_CONSTRAINT; | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
src/Validator/Constraint/ForAuthorizedUserOnlyValidator.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 | ||
|
||
declare(strict_types=1); | ||
|
||
namespace CommerceWeavers\SyliusTpayPlugin\Validator\Constraint; | ||
|
||
use Symfony\Component\Security\Core\Security; | ||
use Symfony\Component\Validator\Constraint; | ||
use Symfony\Component\Validator\ConstraintValidator; | ||
use Symfony\Component\Validator\Exception\UnexpectedTypeException; | ||
|
||
final class ForAuthorizedUserOnlyValidator extends ConstraintValidator | ||
{ | ||
public function __construct(private readonly Security $security) | ||
{ | ||
} | ||
|
||
public function validate(mixed $value, Constraint $constraint): void | ||
{ | ||
if (!$constraint instanceof ForAuthorizedUserOnly) { | ||
throw new UnexpectedTypeException($constraint, ForAuthorizedUserOnly::class); | ||
} | ||
|
||
if (null === $value || null !== $this->security->getUser()) { | ||
return; | ||
} | ||
|
||
$this->context | ||
->buildViolation($constraint->userNotAuthorizedErrorMessage) | ||
->setCode($constraint::USER_NOT_AUTHORIZED_ERROR) | ||
->addViolation() | ||
; | ||
} | ||
} |
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,9 @@ | ||
Sylius\Component\Core\Model\ShopUser: | ||
shop_user_john_doe: | ||
plainPassword: "sylius" | ||
roles: [ROLE_USER] | ||
enabled: true | ||
verifiedAt: "<(new \\DateTime())>" | ||
customer: "@customer_john_doe" | ||
username: "[email protected]" | ||
usernameCanonical: "[email protected]" |
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
74 changes: 74 additions & 0 deletions
74
tests/Unit/Validator/Constraint/ForAuthorizedUserOnlyValidatorTest.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,74 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\CommerceWeavers\SyliusTpayPlugin\Unit\Validator\Constraint; | ||
|
||
use CommerceWeavers\SyliusTpayPlugin\Validator\Constraint\ForAuthorizedUserOnly; | ||
use CommerceWeavers\SyliusTpayPlugin\Validator\Constraint\ForAuthorizedUserOnlyValidator; | ||
use Prophecy\PhpUnit\ProphecyTrait; | ||
use Prophecy\Prophecy\ObjectProphecy; | ||
use Symfony\Component\Security\Core\Security; | ||
use Symfony\Component\Security\Core\User\UserInterface; | ||
use Symfony\Component\Validator\Constraint; | ||
use Symfony\Component\Validator\Exception\UnexpectedTypeException; | ||
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase; | ||
|
||
final class ForAuthorizedUserOnlyValidatorTest extends ConstraintValidatorTestCase | ||
{ | ||
use ProphecyTrait; | ||
|
||
private Security|ObjectProphecy $security; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->security = $this->prophesize(Security::class); | ||
|
||
parent::setUp(); | ||
} | ||
|
||
public function test_it_throws_an_exception_if_a_constraint_has_an_invalid_type(): void | ||
{ | ||
$this->expectException(UnexpectedTypeException::class); | ||
|
||
$this->validator->validate( | ||
'hello', | ||
$this->prophesize(Constraint::class)->reveal(), | ||
); | ||
} | ||
|
||
public function test_it_does_not_build_violation_if_value_is_null(): void | ||
{ | ||
$this->validator->validate(null, new ForAuthorizedUserOnly()); | ||
|
||
$this->assertNoViolation(); | ||
} | ||
|
||
public function test_it_does_not_build_violation_if_user_is_authorized(): void | ||
{ | ||
$user = $this->prophesize(UserInterface::class); | ||
$this->security->getUser()->willReturn($user); | ||
|
||
$this->validator->validate('hello', new ForAuthorizedUserOnly()); | ||
|
||
$this->assertNoViolation(); | ||
} | ||
|
||
public function test_it_builds_violation_if_user_is_not_authorized(): void | ||
{ | ||
$this->security->getUser()->willReturn(null); | ||
|
||
$this->validator->validate('hello', new ForAuthorizedUserOnly()); | ||
|
||
$this | ||
->buildViolation('commerce_weavers_sylius_tpay.shop.pay.field.user_not_authorized') | ||
->setCode(ForAuthorizedUserOnly::USER_NOT_AUTHORIZED_ERROR) | ||
->assertRaised() | ||
; | ||
} | ||
|
||
protected function createValidator(): ForAuthorizedUserOnlyValidator | ||
{ | ||
return new ForAuthorizedUserOnlyValidator($this->security->reveal()); | ||
} | ||
} |