-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[API] Extract user context and revamp change password handler
- Loading branch information
1 parent
2f1d8e8
commit 554192f
Showing
15 changed files
with
376 additions
and
50 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
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
15 changes: 15 additions & 0 deletions
15
src/Sylius/Bundle/ApiBundle/Command/ShopUserIdAwareInterface.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,15 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sylius\Bundle\ApiBundle\Command; | ||
|
||
/** | ||
* @experimental | ||
*/ | ||
interface ShopUserIdAwareInterface extends CommandAwareDataTransformerInterface | ||
{ | ||
public function getShopUserId(); | ||
|
||
public function setShopUserId($shopUserId): 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
52 changes: 52 additions & 0 deletions
52
...Sylius/Bundle/ApiBundle/DataTransformer/LoggedInShopUserIdAwareCommandDataTransformer.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,52 @@ | ||
<?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\DataTransformer; | ||
|
||
use Sylius\Bundle\ApiBundle\Command\ShopUserIdAwareInterface; | ||
use Sylius\Bundle\ApiBundle\Context\UserContextInterface; | ||
use Sylius\Component\Core\Model\ShopUserInterface; | ||
|
||
/** @experimental */ | ||
final class LoggedInShopUserIdAwareCommandDataTransformer implements CommandDataTransformerInterface | ||
{ | ||
/** @var UserContextInterface */ | ||
private $userContext; | ||
|
||
public function __construct(UserContextInterface $userContext) | ||
{ | ||
$this->userContext = $userContext; | ||
} | ||
|
||
/** | ||
* @param ShopUserIdAwareInterface $object | ||
*/ | ||
public function transform($object, string $to, array $context = []): ShopUserIdAwareInterface | ||
{ | ||
/** @var ShopUserInterface $user */ | ||
$user = $this->userContext->getUser(); | ||
|
||
if (!$user instanceof ShopUserInterface) { | ||
return $object; | ||
} | ||
|
||
$object->setShopUserId($user->getId()); | ||
|
||
return $object; | ||
} | ||
|
||
public function supportsTransformation($object): bool | ||
{ | ||
return $object instanceof ShopUserIdAwareInterface; | ||
} | ||
} |
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
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
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
33 changes: 33 additions & 0 deletions
33
src/Sylius/Bundle/ApiBundle/Validator/Constraints/CorrectChangeShopUserConfirmPassword.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,33 @@ | ||
<?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 CorrectChangeShopUserConfirmPassword extends Constraint | ||
{ | ||
/** @var string */ | ||
public $message = 'sylius.user.plainPassword.mismatch'; | ||
|
||
public function validatedBy(): string | ||
{ | ||
return 'sylius_api_correct_change_shop_user_confirm_password'; | ||
} | ||
|
||
public function getTargets(): string | ||
{ | ||
return self::CLASS_CONSTRAINT; | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
.../Bundle/ApiBundle/Validator/Constraints/CorrectChangeShopUserConfirmPasswordValidator.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,38 @@ | ||
<?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\Bundle\ApiBundle\Command\ChangeShopUserPassword; | ||
use Symfony\Component\Validator\Constraint; | ||
use Symfony\Component\Validator\ConstraintValidator; | ||
use Webmozart\Assert\Assert; | ||
|
||
/** @experimental */ | ||
final class CorrectChangeShopUserConfirmPasswordValidator extends ConstraintValidator | ||
{ | ||
/** | ||
* @param ChangeShopUserPassword|mixed $command | ||
*/ | ||
public function validate($command, Constraint $constraint): void | ||
{ | ||
Assert::isInstanceOf($command, ChangeShopUserPassword::class); | ||
|
||
if ($command->confirmPassword !== $command->newPassword) { | ||
$this->context->buildViolation($constraint->message) | ||
->atPath('newPassword') | ||
->addViolation() | ||
; | ||
} | ||
} | ||
} |
Oops, something went wrong.