src/Security/CustomerFormAuthenticator.php line 29

Open in your IDE?
  1. <?php
  2. namespace App\Security;
  3. use App\Entity\Customer\Customer;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use Doctrine\Persistence\ManagerRegistry;
  6. use Symfony\Component\HttpFoundation\RedirectResponse;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  10. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  11. use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
  12. use Symfony\Component\Security\Core\Exception\CustomUserMessageAuthenticationException;
  13. use Symfony\Component\Security\Core\Exception\InvalidCsrfTokenException;
  14. use Symfony\Component\Security\Core\Security;
  15. use Symfony\Component\Security\Core\User\UserInterface;
  16. use Symfony\Component\Security\Core\User\UserProviderInterface;
  17. use Symfony\Component\Security\Csrf\CsrfToken;
  18. use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;
  19. use Symfony\Component\Security\Guard\Authenticator\AbstractFormLoginAuthenticator;
  20. use Symfony\Component\Security\Guard\PasswordAuthenticatedInterface;
  21. use Symfony\Component\Security\Http\Util\TargetPathTrait;
  22. use App\Service\Tools;
  23. use App\Service\ObpAPI;
  24. class CustomerFormAuthenticator extends AbstractFormLoginAuthenticator implements PasswordAuthenticatedInterface
  25. {
  26.     use TargetPathTrait;
  27.     public const LOGIN_ROUTE 'customer_login';
  28.     private $entityManager;
  29.     private $doctrine;
  30.     private $urlGenerator;
  31.     private $csrfTokenManager;
  32.     private $passwordEncoder;
  33.     private $obpAPIservice;
  34.     private $pass_valid;
  35.     private $centre_prefere 'lons';
  36.     public function __construct(EntityManagerInterface $entityManagerUrlGeneratorInterface $urlGeneratorCsrfTokenManagerInterface $csrfTokenManagerUserPasswordEncoderInterface $passwordEncoderManagerRegistry $doctrineObpAPI $obpAPIservice)
  37.     {
  38.         $this->entityManager $doctrine->getManager('customer');
  39.         $this->urlGenerator $urlGenerator;
  40.         $this->csrfTokenManager $csrfTokenManager;
  41.         $this->passwordEncoder $passwordEncoder;
  42.         $this->obpAPIservice $obpAPIservice;
  43.     }
  44.     public function supports(Request $request): bool
  45.     {
  46.         return self::LOGIN_ROUTE === $request->attributes->get('_route')
  47.             && $request->isMethod('POST');
  48.     }
  49.     public function getCredentials(Request $request)
  50.     {
  51.         $credentials = [
  52.             'email' => $request->request->get('email'),
  53.             'password' => $request->request->get('password'),
  54.             'csrf_token' => $request->request->get('_csrf_token'),
  55.             'g-recaptcha-response' => $request->request->get('g-recaptcha-response'),
  56.         ];
  57.         $request->getSession()->set(
  58.             Security::LAST_USERNAME,
  59.             $credentials['email']
  60.         );
  61.         return $credentials;
  62.     }
  63.     public function getUser($credentialsUserProviderInterface $userProvider): ?Customer
  64.     {
  65.         $token = new CsrfToken('authenticate'$credentials['csrf_token']);
  66.         if (!$this->csrfTokenManager->isTokenValid($token)) {
  67.             throw new InvalidCsrfTokenException();
  68.         }
  69.         $dataObpAPI = array(
  70.             'email' => $credentials['email'],
  71.             'password' => $credentials['password'],
  72.             'g-recaptcha-response' => $credentials['g-recaptcha-response'],
  73.             'instance' => $this->obpAPIservice->getBaseInstance(),
  74.         );
  75.         $urlApi $this->obpAPIservice->getContactApiUrl();
  76.         $customer_obp_infos json_decode
  77.             $this->obpAPIservice->CallAPI('POST'$urlApi.'/auth/users/login'json_encode($dataObpAPI)) 
  78.         );
  79.         // dd($credentials,$customer_obp_infos);
  80.         if (is_object($customer_obp_infos) && !$customer_obp_infos->meta->success) {
  81.             // fail authentication with a custom error
  82.             $this->pass_valid false;
  83.             throw new CustomUserMessageAuthenticationException($customer_obp_infos->meta->message);
  84.         }
  85.         $this->pass_valid = (is_object($customer_obp_infos) && $customer_obp_infos->meta->success) ? true false;
  86.         $user $this->entityManager->getRepository(Customer::class)->findOneBy(['email' => $credentials['email']]);
  87.         if($this->pass_valid && !$user) {
  88.             $customer = new Customer();
  89.             $entityManager $this->getDoctrine()->getManager('customer');
  90.             $entityManager->persist$customer );
  91.             $entityManager->flush();
  92.             $number str_pad((int)$customer->getId(), 6'0'STR_PAD_LEFT);
  93.             $customer->setCustomerNumber$number );
  94.             $customer->setEmail$credentials['email'] );
  95.             $customer->setFirstname$customer_obp_infos->data->firsname );
  96.             $customer->setLastname$customer_obp_infos->data->lastname );
  97.             $customer->setPhoneMobile$customer_obp_infos->data->phone );
  98.             $birthdate =  new \DateTime$customer_obp_infos->data->birthdate );
  99.             $customer->setBirthday$birthdate );
  100.             if($customer_obp_infos->data->dialCountry) {
  101.                 $customer->setCountry$customer_obp_infos->data->dialCountry );
  102.             }
  103.             $customer->setStatut('Actif');
  104.             $customer->setActive(1);
  105.             $entityManager->persist$customer );
  106.             $entityManager->flush();   
  107.             return $customer;
  108.         }
  109.         // dd($userProvider,$user);
  110.         if (!$user) {
  111.             // fail authentication with a custom error
  112.             throw new CustomUserMessageAuthenticationException('Utilisateur introuvable');
  113.         }
  114.         return $user;
  115.     }
  116.     public function checkCredentials($credentialsUserInterface $user): bool
  117.     {
  118.         return $this->pass_valid;
  119.     }
  120.     /**
  121.      * Used to upgrade (rehash) the user's password automatically over time.
  122.      */
  123.     public function getPassword($credentials): ?string
  124.     {
  125.         return $credentials['password'];
  126.     }
  127.     public function onAuthenticationSuccess(Request $requestTokenInterface $token$providerKey): ?Response
  128.     {
  129.         // dd($request->getSession());
  130.         if ($request->request->get('_target_path')){
  131.             $this->saveTargetPath($request->getSession(), $providerKey$request->request->get('_target_path'));
  132.         }
  133.         if ($targetPath $this->getTargetPath($request->getSession(), $providerKey)) {
  134.             return new RedirectResponse($targetPath);
  135.         }
  136.         // For example : return new RedirectResponse($this->urlGenerator->generate('some_route'));
  137.         return new RedirectResponse($this->urlGenerator->generate('home'));
  138.     }
  139.     protected function getLoginUrl(): string
  140.     {
  141.         return $this->urlGenerator->generate(self::LOGIN_ROUTE);
  142.     }
  143. }