src/Controller/SecurityController.php line 274

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Company;
  4. use App\Entity\Product;
  5. use App\Entity\User;
  6. use App\Form\CompanyIndividualType;
  7. use App\Form\CompanyType;
  8. use App\Form\ForgotPasswordType;
  9. use App\Form\ProductType;
  10. use App\Form\UserType;
  11. use App\Repository\CompanyRepository;
  12. use App\Repository\PageRepository;
  13. use App\Repository\SubCategoryRepository;
  14. use App\Repository\UserRepository;
  15. use App\Services\File;
  16. use App\Services\Mail;
  17. use App\Services\MangoPayService;
  18. use App\Services\Password;
  19. use Doctrine\ORM\EntityManagerInterface;
  20. use MangoPay\UserLegal;
  21. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  22. use Symfony\Component\Finder\Exception\AccessDeniedException;
  23. use Symfony\Component\HttpFoundation\Request;
  24. use Symfony\Component\HttpFoundation\Response;
  25. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  26. use Symfony\Component\Routing\Annotation\Route;
  27. use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
  28. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  29. use Symfony\Component\String\Slugger\SluggerInterface;
  30. class SecurityController extends AbstractController
  31. {
  32.     /**
  33.      * @var EntityManagerInterface
  34.      */
  35.     private $em;
  36.     public function __construct(EntityManagerInterface $em)
  37.     {
  38.         $this->em $em;
  39.     }
  40.     /**
  41.      * @Route("/client/connexion", name="app_login")
  42.      */
  43.     public function loginClient(Mail $mailMangoPayService $mangoPayServiceAuthenticationUtils $authenticationUtilsRequest $requestUserPasswordEncoderInterface $encoder): Response
  44.     {
  45.         if ($this->getUser()) {
  46.             $target $request->query->get('target');
  47.             if ($target) { // button in home page that redirct to reseaux sociaux
  48.                 return $this->redirectToRoute('reseau-sociaux_home');
  49.             }
  50.             return $this->redirectToRoute('front_home');
  51.         }
  52.         $redirectUrl $request->query->get('redirect'null);
  53.         $user = new User();
  54.         $form $this->createForm(UserType::class, $user)->handleRequest($request);
  55.         if ($form->isSubmitted() && $form->isValid()) {
  56.             $passwordEncoded $encoder->encodePassword($user$user->getPassword());
  57.             $user->setPassword($passwordEncoded);
  58.             $user->setRoles(['ROLE_USER']);
  59.             $this->em->persist($user);
  60.             $this->em->flush();
  61.             $mail->ConfirmationClient($user);
  62.             
  63.             $this->addFlash('success''Vous êtes maintenant inscrit, Votre compte est en attente de validation');
  64.             return $this->redirectToRoute('app_login');
  65.         }
  66.         // get the login error if there is one
  67.         $error $authenticationUtils->getLastAuthenticationError();
  68.         // last username entered by the user
  69.         $lastUsername $authenticationUtils->getLastUsername();
  70.         return $this->render('security/login.html.twig', [
  71.             'last_username' => $lastUsername,
  72.             'error' => $error,
  73.             'form' => $form->createView(),
  74.             'redirect' => $redirectUrl
  75.         ]);
  76.     }
  77.     /**
  78.      * @Route("/acces-prestataire", name="app_login_enterprise")
  79.      */
  80.     public function loginEnterprise(AuthenticationUtils $authenticationUtils)
  81.     {
  82.         // get the login error if there is one
  83.         $error $authenticationUtils->getLastAuthenticationError();
  84.         // last username entered by the user
  85.         $lastUsername $authenticationUtils->getLastUsername();
  86.         return $this->render('security/login_company.html.twig', [
  87.             'last_username' => $lastUsername,
  88.             'error' => $error
  89.         ]);
  90.     }
  91.     /**
  92.      * @Route("/inscription-prestataire/{type}/etape-1", name="front_subscription_company_step1")
  93.      */
  94.     public function subscriptionCompanyStep1(
  95.         Request $request,
  96.         string $type,
  97.         UserPasswordEncoderInterface $encoder,
  98.         SluggerInterface $slugger,
  99.         SessionInterface $session,
  100.         PageRepository $pageRepository,
  101.         MangoPayService $mangoPayService)
  102.     {
  103.         if (($type != Company::COMPANY_TYPE_ENTREPRISE) && ($type != Company::COMPANY_TYPE_INDIVIDUEL)){
  104.             throw new AccessDeniedException("Acces interdit : type de prestataire $type inconnu");
  105.         }
  106.         $company = new Company();
  107.         if ($type == Company::COMPANY_TYPE_ENTREPRISE){
  108.             $form $this->createForm(CompanyType::class, $company)->handleRequest($request);
  109.         }else if ($type == Company::COMPANY_TYPE_INDIVIDUEL){
  110.             $form $this->createForm(CompanyIndividualType::class, $company)->handleRequest($request);
  111.         }
  112.         if ($form->isSubmitted() && $form->isValid()) {
  113.             $passwordEncoded $encoder->encodePassword($company$company->getPassword());
  114.             $company->setPassword($passwordEncoded);
  115.             $companyName $company->getCompanyName();
  116.             if (empty($companyName)) {
  117.                 $companyName $form->get("contactName")->getNormData()." ".$form->get("firstName")->getNormData();
  118.             }
  119.             $sluggedName $slugger->slug($companyName)->lower();
  120.             $company->setCompanySlug($sluggedName);
  121.             $company->setRoles(["ROLE_ENTERPRISE"]);
  122.             $company->setIsDirectReservationMode(0);
  123.             $company->setCreatedAt( new \DateTime());
  124.             $this->em->persist($company);
  125.             $this->em->flush();
  126.             $session->set('idCompany'$company->getId());
  127.             $session->set('isSubscription'true);
  128.             return $this->redirectToRoute('front_subscription_company_confirmation');
  129.             //return $this->redirectToRoute('front_subscription_company_step2');
  130.         }
  131.         return $this->render('security/subscription_company_step1.html.twig', [
  132.             'form' => $form->createView(),
  133.             'type' => $type,
  134.             'cgv' => $pageRepository->findOneBy(['name' => 'cgv'])
  135.         ]);
  136.     }
  137.     /**
  138.      * @Route("/inscription-prestataire/etape-2", name="front_subscription_company_step2")
  139.      */
  140.     public function SubscriptionCompanyStep2(SessionInterface $sessionRequest $requestCompanyRepository $companyRepositorySluggerInterface $sluggerFile $file)
  141.     {
  142.         if (!$session->get('idCompany')) {
  143.             return $this->redirectToRoute('app_login_enterprise');
  144.         }
  145.         $company $companyRepository->find($session->get('idCompany'));
  146.         $ifYoutube false;
  147.         $product = new Product();
  148.         $form $this->createForm(ProductType::class, $product, ['ifYoutube' => $ifYoutube'isSubscription' => true,'isNew' => true'user' => $company])->handleRequest($request);
  149.         if ($form->isSubmitted() && $form->isValid()) {
  150.             $contentProduct str_replace('script>'''$product->getText());
  151.             $product->setText($contentProduct);
  152.             $product->setCompany($company);
  153.             $slugTitle $slugger->slug($product->getName())->lower();
  154.             $product->setProductSlug($slugTitle);
  155.             foreach ($product->getPhotos() as $photo) {
  156.                 $filename $file->uploadPhotoProduct($photo->getUrl());
  157.                 $photo->setUrl($filename);
  158.                 $photo->setProduct($product);
  159.             }
  160.             if (!empty($request->get('clothing'))) {
  161.                 $product->setSizes($request->get('clothing'));
  162.             }
  163.             if (!empty($request->get('color'))) {
  164.                 $product->setColors($request->get('color'));
  165.             }
  166.             $arrayTransporteurs = [];
  167.             if ($request->get('transporteurs')) {
  168.                 foreach ($request->get('transporteurs') as $id => $value) {
  169.                     $arrayTransporteurs[] = $id;
  170.                 }
  171.             }
  172.             $product->setTransporteurs($arrayTransporteurs);
  173.             $this->em->persist($product);
  174.             $this->em->flush();
  175.             $session->set('isSubscription'true);
  176.             return $this->redirectToRoute('front_subscription_company_confirmation');
  177.         }
  178.         return $this->render('security/subscription_company_step2.html.twig', [
  179.             'form' => $form->createView()
  180.         ]);
  181.     }
  182.     /**
  183.      * @Route("/inscription-prestataire/confirmation", name="front_subscription_company_confirmation")
  184.      */
  185.     public function confirmationSubscription(SessionInterface $sessionMail $mailCompanyRepository $companyRepository)
  186.     {
  187.         if (!$session->get('isSubscription')) {
  188.             return $this->redirectToRoute('front_subscription_company_step1');
  189.         }
  190.         if (!$session->get('idCompany')) {
  191.             return $this->redirectToRoute('app_login_enterprise');
  192.         }
  193.         $company $companyRepository->find($session->get('idCompany'));
  194.         //$session->remove('isSubscription');
  195.         $mail->subscriptionConfirm($company);
  196.         $mail->subscriptionConfirmRoAdmin();
  197.         return $this->render('security/subscription_company_confirmation.html.twig');
  198.     }
  199.     /**
  200.      * @Route("/mot-de-passe-oublie", name="front_forgot_password")
  201.      */
  202.     public function forgotPassword(Mail $mailRequest $requestUserRepository $userRepositoryCompanyRepository $companyRepositoryUserPasswordEncoderInterface $userPasswordEncoder)
  203.     {
  204.         $form $this->createForm(ForgotPasswordType::class)->handleRequest($request);
  205.         if ($form->isSubmitted() && $form->isValid()) {
  206.             $user $userRepository->findOneBy(['email' => $form->get('email')->getData()]);
  207.             if (!$user) {
  208.                 $user $companyRepository->findOneBy(['email' => $form->get('email')->getData()]);
  209.             }
  210.             if (!$user) {
  211.                 $this->addFlash('danger''Aucun compte ne correspond à cette adresse mail');
  212.                 return $this->redirectToRoute('front_forgot_password');
  213.             }
  214.             $newPassword Password::generatePassword(8);
  215.             $encodedPassword $userPasswordEncoder->encodePassword($user$newPassword);
  216.             $user->setPassword($encodedPassword);
  217.             $this->em->flush();
  218.             $mail->forgotPassword($newPassword$user);
  219.             $this->addFlash('success''Un mail vient de vous être envoyé avec un nouveau mot de passe.');
  220.             return $this->redirectToRoute('front_forgot_password');
  221.         }
  222.         return $this->render('security/forgot_password.html.twig', [
  223.             'form' => $form->createView()
  224.         ]);
  225.     }
  226.     /**
  227.      * @Route("/inscription/get-subcategory", name="front_company__subscription_get_subcategory")
  228.      */
  229.     public function getSubCategory(Request $requestSubCategoryRepository $subCategoryRepository)
  230.     {
  231.         if ($request->get('data')) {
  232.             $idCategory json_decode($request->get('data'));
  233.             $subCategory $subCategoryRepository->find($idCategory->idCategory);
  234.             return $this->json([
  235.                 'isClothing' => $subCategory->getIsClothing(),
  236.                 'isClothingHTML' => $this->render('front/html/clothing.html.twig')
  237.             ]);
  238.         }
  239.     }
  240.      /**
  241.      * @Route("/deconnexion", name="deconnexion")
  242.      */
  243.     public function deconnecter(UserRepository $repo,CompanyRepository $comp)
  244.     {
  245.         $userconn $this->getUser();
  246.         $user $userconn ?($repo->find($userconn)?$repo->find($userconn):$comp->find($userconn)):null;
  247.         if($user == null)
  248.         {
  249.             return $this->redirectToRoute('front_home');
  250.         }
  251.         $user->setStatuCon(false);
  252.         $this->em->persist($user);
  253.         $this->em->flush();
  254.         return $this->redirectToRoute('app_logout');
  255.     }
  256.     
  257.     /**
  258.      * @Route("/logout", name="app_logout")
  259.      */
  260.     public function logout()
  261.     {
  262.         throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
  263.     }
  264. }