-
-
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 111cc43
Showing
15 changed files
with
385 additions
and
54 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
13 changes: 13 additions & 0 deletions
13
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,13 @@ | ||
<?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
53 changes: 53 additions & 0 deletions
53
...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,53 @@ | ||
<?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; | ||
use Sylius\Component\User\Model\UserInterface; | ||
|
||
/** @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|UserInterface $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
30 changes: 30 additions & 0 deletions
30
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,30 @@ | ||
<?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 | ||
{ | ||
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('sylius.user.plainPassword.mismatch') | ||
->atPath('newPassword') | ||
->addViolation() | ||
; | ||
} | ||
} | ||
} |
Oops, something went wrong.