src/Security/ParentLoginFormAuthenticator.php line 170

Open in your IDE?
  1. <?php
  2. namespace App\Security;
  3. use App\Entity\User;
  4. use App\Entity\Panier;
  5. use App\Entity\PanierProduit;
  6. use App\Entity\Ref;
  7. use App\Entity\Typeref;
  8. use Doctrine\ORM\EntityManagerInterface;
  9. use Symfony\Component\HttpFoundation\RedirectResponse;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\HttpFoundation\Response;
  12. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  13. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  14. use Symfony\Component\Security\Core\Exception\CustomUserMessageAuthenticationException;
  15. use Symfony\Component\Security\Core\Exception\InvalidCsrfTokenException;
  16. use Symfony\Component\Security\Core\Security;
  17. use Symfony\Component\Security\Csrf\CsrfToken;
  18. use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;
  19. use Symfony\Component\Security\Http\Util\TargetPathTrait;
  20. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  21. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  22. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  23. use Symfony\Component\Security\Http\Authenticator\AbstractAuthenticator;
  24. use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;
  25. use Symfony\Component\Security\Http\Authenticator\Passport\Passport;
  26. use Symfony\Component\Security\Http\Authenticator\Passport\SelfValidatingPassport;
  27. use Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface;
  28. use Symfony\Component\Security\Http\LoginLink\Exception\InvalidLoginLinkExceptionInterface;
  29. use Symfony\Component\Security\Http\LoginLink\LoginLinkHandlerInterface;
  30. class ParentLoginFormAuthenticator extends AbstractAuthenticator implements AuthenticationEntryPointInterface
  31. {
  32.     use TargetPathTrait;
  33.     private $entityManager;
  34.     private $urlGenerator;
  35.     private $csrfTokenManager;
  36.     private $passwordEncoder;
  37.     private $session;
  38.     private $signatureHasher;
  39.     public function __construct(EntityManagerInterface $entityManagerUrlGeneratorInterface $urlGeneratorCsrfTokenManagerInterface $csrfTokenManagerUserPasswordHasherInterface $passwordEncoderSessionInterface $sessionLoginLinkHandlerInterface $signatureHasher)
  40.     {
  41.         $this->entityManager $entityManager;
  42.         $this->urlGenerator $urlGenerator;
  43.         $this->csrfTokenManager $csrfTokenManager;
  44.         $this->passwordEncoder $passwordEncoder;
  45.         $this->session $session;
  46.         $this->signatureHasher $signatureHasher;
  47.     }
  48.     public function supports(Request $request): ?bool
  49.     {
  50.         return 'app_back_Parent' === $request->attributes->get('_route')
  51.             && $request->isMethod('POST')
  52.             || 'login_check' === $request->attributes->get('_route');
  53.     }
  54.     public function authenticate(Request $request): Passport
  55.     {
  56.         $passwordlessLogin false;
  57.         $credentials = [
  58.             'email' => $request->request->get('email'),
  59.             'password' => $request->request->get('password'),
  60.             // 'codesejour' => $request->request->get('codesejour'),
  61.             'csrf_token' => $request->request->get('_csrf_token'),
  62.         ];
  63.         if ($request->attributes->get('_route') === 'login_check') {
  64.             $email $request->query->get('user');
  65.             try {
  66.                 $user $this->signatureHasher->consumeLoginLink($request);
  67.             } catch (InvalidLoginLinkExceptionInterface $e) {
  68.                 throw new CustomUserMessageAuthenticationException($e->getMessage());
  69.             }
  70.             
  71.             if (!$user) {
  72.                 throw new CustomUserMessageAuthenticationException("Ce lien a expiré.");
  73.             }
  74.             $passwordlessLogin true;
  75.             $credentials = [
  76.                 'email' => $email,
  77.                 'password' => $request->request->get('password'),
  78.                 'csrf_token' => $request->request->get('_csrf_token')
  79.             ];
  80.         }
  81.         $request->getSession()->set(
  82.             Security::LAST_USERNAME,
  83.             $credentials['email']
  84.         );
  85.         $token = new CsrfToken('authenticate'$credentials['csrf_token']);
  86.         if (!$this->csrfTokenManager->isTokenValid($token) && $passwordlessLogin == false) {
  87.             throw new InvalidCsrfTokenException();
  88.         }
  89.         $parent $this->entityManager->getRepository(User::class)->findOneBy(['email' => $credentials['email']]);
  90.         $user $parent;
  91.         if ($user) {
  92.             if ($user->getActivatemail() == null) {
  93.                 throw new CustomUserMessageAuthenticationException("vous n’avez pas encore cliquer sur votre lien d’activation reçu par mail au moment de la création de votre compte.");
  94.             }
  95.             $Products $this->session->get("Panier");
  96.             //Touhemi22-04:Initialisation de panier.
  97.             $typeref $this->entityManager->getRepository(Typeref::class)->find(8);
  98.             $statutPanier $this->entityManager->getRepository(Ref::class)->findOneBy(array("libiller" => "creer""typeref" => $typeref));
  99.             $panierTrace $this->entityManager->getRepository(Panier::class)->findOneBy(array("creerPar" => $user"statut" => $statutPanier));
  100.             if ($Products == null) {
  101.                 $Products = [];
  102.             }
  103.             if ($panierTrace == null) {
  104.                 $panierTrace = new Panier();
  105.                 $dateNow = new \Datetime();
  106.                 $panierTrace->setDateCreation($dateNow);
  107.                 $panierTrace->setCreerPar($user);
  108.                 $panierTrace->setStatut($statutPanier);
  109.                 $this->entityManager->persist($panierTrace);
  110.                 $this->entityManager->flush();
  111.             }
  112.             $panierProduit $this->entityManager->getRepository(PanierProduit::class)->findBy(array("idPanier" => $panierTrace));
  113.             foreach ($panierProduit as $produit) {
  114.                 $p = [];
  115.                 $p["id"] = $produit->getIdProduit()->getId();
  116.                 $p["nom"] = $produit->getIdProduit()->getType()->getLabeletype();
  117.                 $p["ident"] = $produit->getIdProduit()->getLabele();
  118.                 $p["codesejour"] = $produit->getIdProduit()->getIdsjour()->getcodeSejour();
  119.                 $montantTTC '';
  120.                 if ($produit->getIdProduit()->getIdConditionnement()) {
  121.                    $montantTTC $produit->getIdProduit()->getIdConditionnement()->getMontantTTC();
  122.                 }
  123.                 $p["mnt"] = $montantTTC;
  124.                 $p["path"] ="";
  125.                 if( $produit->getIdProduit()->getType())
  126.                {
  127.                 if($produit->getIdProduit()->getType()->getAttachements()[0] !=null)
  128.                 {
  129.                 $p["path"] = $produit->getIdProduit()->getType()->getAttachements()[0]->getIdAttachement()->getPath();}}
  130.                 $condition '';
  131.                 if ($produit->getIdProduit()->getIdConditionnement()) {
  132.                     
  133.                     $condition $produit->getIdProduit()->getIdConditionnement()->getId();
  134.                 }
  135.                 $p["condition"] = $condition;
  136.                 $p["qte"] = $produit->getQuantite();
  137.                 array_push($Products$p);
  138.             }
  139.             $this->session->set('Panier'$Products);
  140.         }
  141.         if (!$user) {
  142.             // fail authentication with a custom error
  143.             throw new CustomUserMessageAuthenticationException("Votre identifiant est votre mail renseigné lors de la création de compte parent.");
  144.         }
  145.         if (!$user->hasRole('ROLE_PARENT')) {
  146.             throw new CustomUserMessageAuthenticationException('Votre identifiant est votre mail renseigné lors de la création de compte parent.');
  147.         }
  148.         if ($passwordlessLogin == false) {
  149.             if (!$this->passwordEncoder->isPasswordValid($user$credentials['password'])) {
  150.                 throw new CustomUserMessageAuthenticationException("Votre mot de passe est celui que vous avez renseigné lors de la création de votre compte parent.");
  151.             }
  152.         }
  153.         return new SelfValidatingPassport(new UserBadge($credentials['email']));
  154.     }
  155.     public function onAuthenticationSuccess(Request $requestTokenInterface $tokenstring $firewallName): ?Response
  156.     {
  157.         return new RedirectResponse($this->urlGenerator->generate('CodeSejour'));
  158.     }
  159.     public function onAuthenticationFailure(Request $requestAuthenticationException $exception): ?Response
  160.     {
  161.         return new RedirectResponse(
  162.             $this->urlGenerator->generate('app_back_Parent')
  163.         );
  164.     }
  165.     public function start(Request $request, ?AuthenticationException $authException null)
  166.     {
  167.         return new RedirectResponse(
  168.             $this->urlGenerator->generate('app_back_Parent')
  169.         );
  170.     }
  171. }