Skip to content

Commit

Permalink
Merge branch '1.11'
Browse files Browse the repository at this point in the history
* 1.11:
  [Docs] small review fixes
  [Docs] how to force already registered user to log in during checkout
  • Loading branch information
Zales0123 committed Feb 24, 2022
2 parents 911400d + 0867433 commit 1239fdf
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
How to force already registered user to login during checkout in Sylius API?
============================================================================

You can force the user to log in during checkout if he is already registered in your app.

Create a new constraint validator
---------------------------------

Firstly you need to add a new constraint class:

.. code-block:: php
// src/App/Validator/Constraints
<?php
declare(strict_types=1);
namespace App\Validator\Constraints;
use Symfony\Component\Validator\Constraint;
final class UserAlreadyRegistered extends Constraint
{
public string $message = 'This email is already registered. Please log in.';
public function validatedBy(): string
{
return 'sylius_api_registered_user_validator';
}
public function getTargets(): string
{
return self::CLASS_CONSTRAINT;
}
}
Then you need to add a validator class:

.. code-block:: php
// src/App/Validator/Constraints
<?php
declare(strict_types=1);
namespace App\Validator\Constraints;
use Sylius\Component\Core\Model\CustomerInterface;
use Sylius\Component\Core\Model\ShopUserInterface;
use Sylius\Component\Core\Repository\CustomerRepositoryInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Webmozart\Assert\Assert;
final class UserAlreadyRegisteredValidator extends ConstraintValidator
{
public function __construct(
private CustomerRepositoryInterface $customerRepository,
private TokenStorageInterface $tokenStorage
) {
}
public function validate($value, Constraint $constraint): void
{
/** @var UserAlreadyRegistered $constraint */
Assert::isInstanceOf($constraint, UserAlreadyRegistered::class);
$token = $this->tokenStorage->getToken();
/** @var CustomerInterface|null $existingCustomer */
$existingCustomer = $this->customerRepository->findOneBy(['email' => $value->getEmail()]);
if (null !== $existingCustomer && !$token->getUser() instanceof ShopUserInterface) {
$this->context->addViolation($constraint->message);
}
}
}
Enabling your validator
-----------------------

As the last thing you need to do is enable this constraint validator in your app and register it as a service in ``services.yaml``. You can do it this way:

.. code-block:: xml
// # config/validator/validation.xml
<?xml version="1.0" encoding="UTF-8"?>
<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping http://symfony.com/schema/dic/services/constraint-mapping-1.0.xsd">
<class name="Sylius\Bundle\ApiBundle\Command\Checkout\UpdateCart">
<constraint name="App\Validator\Constraints\UserAlreadyRegistered">
<option name="groups">
<value>sylius</value>
</option>
</constraint>
</class>
</constraint-mapping>
.. code-block:: yaml
// # config/services.yaml
services:
# other definitions
App\Validator\Constraints\UserAlreadyRegisteredValidator:
class: App\Validator\Constraints\UserAlreadyRegisteredValidator
tags: [ { name: validator.constraint_validator, alias: sylius_api_registered_user_validator } ]
1 change: 1 addition & 0 deletions docs/cookbook/api/map.rst.inc
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
* :doc:`/cookbook/api/add_to_cart_product_chosen_by_product_options`
* :doc:`/cookbook/api/how_force_login_already_registered_user_during_checkout`
1 change: 1 addition & 0 deletions docs/cookbook/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -167,5 +167,6 @@ API
:hidden:

api/add_to_cart_product_chosen_by_product_options
api/how_force_login_already_registered_user_during_checkout

.. include:: /cookbook/api/map.rst.inc

0 comments on commit 1239fdf

Please sign in to comment.