src/Controller/Frontend/SecurityController.php line 28

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Frontend;
  3. use App\Entity\Activity;
  4. use App\Entity\Contact;
  5. use App\Entity\EntityStatus;
  6. use App\Entity\Entreprise;
  7. use App\Entity\EntrepriseActivity;
  8. use App\Entity\FirmType;
  9. use App\Entity\User;
  10. use App\Form\Common\ChangePasswordType;
  11. use App\Form\Frontend\SignupType;
  12. use App\Repository\UserRepository;
  13. use App\Service\SignupService;
  14. use Doctrine\ORM\EntityManagerInterface;
  15. use Exception;
  16. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  17. use Symfony\Component\HttpFoundation\Request;
  18. use Symfony\Component\HttpFoundation\Response;
  19. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  20. use Symfony\Component\Routing\Annotation\Route;
  21. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  22. class SecurityController extends AbstractController
  23. {
  24.     #[Route(path'/login'name'customer_login')]
  25.     public function login(AuthenticationUtils $authenticationUtils): Response
  26.     {
  27.         $error $authenticationUtils->getLastAuthenticationError();
  28.         $lastUserName $authenticationUtils->getLastUsername();
  29.         return $this->render('frontend/security/login.html.twig', [
  30.             'controller_name' => 'SecurityController',
  31.             'last_username' => $lastUserName,
  32.             'error' => $error
  33.         ]);
  34.     }
  35.     #[Route(path'/security/change-password'name'security_change_password')]
  36.     public function changePassword(
  37.         Request $request,
  38.         UserPasswordHasherInterface $passwordHasher,
  39.         UserRepository $userRepository
  40.     ): Response
  41.     {
  42.         $user $this->getUser();
  43.         $form $this->createForm(ChangePasswordType::class);
  44.         $form->handleRequest($request);
  45.         if ($form->isSubmitted() && $form->isValid()) {
  46.             $encodedPassword $passwordHasher->hashPassword(
  47.                 $user,
  48.                 $form->get('plainPassword')->getData()
  49.             );
  50.             $user
  51.                 ->setPassword($encodedPassword)
  52.                 ->setChangePassword(false)
  53.             ;
  54.             $userRepository->save($usertrue);
  55.             $this->addFlash(
  56.                 'success',
  57.                 'Mot de passe changé avec succès'
  58.             );
  59.             return $this->redirectToRoute('customer_home');
  60.         }
  61.         return $this->renderForm('frontend/security/change_password.html.twig', [
  62.             'controller_name' => 'SecurityController',
  63.             'user' => $user,
  64.             'form' => $form
  65.         ]);
  66.     }
  67.     #[Route(path'/logout'name'customer_logout')]
  68.     public function logout()
  69.     {
  70.         throw new Exception('FFCB logout');
  71.     }
  72.     #[Route('/signup'name'customer_signup'methods: ['GET''POST'])]
  73.     public function signup(Request $requestSignupService $signupServiceEntityManagerInterface $em): Response
  74.     {
  75.         $form $this->createForm(SignupType::class);
  76.         $form->handleRequest($request);
  77.         if ($form->isSubmitted() && $form->isValid()) {
  78.             $contactEmail $form->get('contact_email')->getData();
  79.             $contactUser $em->getRepository(User::class)->findOneBy(['email' => $contactEmail]);
  80.             if(in_array('ROLE_MEMBER'$contactUser->getRoles())) {
  81.                 $this->addFlash(
  82.                     'error',
  83.                     'Email déjà existant, veuillez vous connecter <a href="' $this->generateUrl('customer_login') .'">ici.</a>'
  84.                 );
  85.                 return $this->redirectToRoute('customer_signup');
  86.             }
  87.             $entreprise = new Entreprise();
  88.             $entreprise
  89.                 ->setRaisonSocial($form->get('raisonSocial')->getData())
  90.                 ->setStatus(EntityStatus::STATUS_IN_CREATION)
  91.                 ->setEmail($form->get('firm_email')->getData())
  92.                 ->setTel1($form->get('firm_phone')->getData())
  93.                 ->setCommentaires($form->get('reason')->getData())
  94.                 ->setAdresse($form->get('firm_adresse')->getData())
  95.                 ->setCodePostal($form->get('firm_codePostal')->getData())
  96.                 ->setVille($form->get('firm_ville')->getData())
  97.                 ->setSouhaiteAdherer($form->get('isMember')->getData())
  98.                 ->setFirmType($em->getRepository(FirmType::class)->find(1))
  99.             ;
  100.             $activities $em->getRepository(Activity::class)->findBy([
  101.                 'isEnabled' => true
  102.             ]);
  103.             foreach ($activities as $activity) {
  104.                 $firmActivity = new EntrepriseActivity();
  105.                 $firmActivity
  106.                     ->setNumber(0)
  107.                     ->setChecked(false)
  108.                     ->setActivity($activity)
  109.                     ->setEntreprise($entreprise)
  110.                 ;
  111.                 $entreprise->addEntrepriseActivity($firmActivity);
  112.             }
  113.             $contact = new Contact();
  114.             $contact
  115.                 ->setNom($form->get('contact_lastname')->getData())
  116.                 ->setPrenom($form->get('contact_firstname')->getData())
  117.                 ->setTelPortable($form->get('contact_cellphone')->getData())
  118.                 ->setEmailContact($form->get('contact_email')->getData())
  119.                 ->setIsResponsible(true)
  120.             ;
  121.             $entreprise->addContact($contact);
  122.             $em->persist($entreprise);
  123.             $em->flush();
  124.             $signupService->notifySignup($form->getData(), $entreprise);
  125.             $this->addFlash(
  126.                 'success',
  127.                 'Votre demande d\'inscription à bien été prise en compte. L\'équipe FFCB vous répondra dans les plus brefs délais.'
  128.             );
  129.             return $this->redirectToRoute('customer_signup');
  130.         }
  131.         return $this->renderForm('frontend/security/signup.html.twig', [
  132.             'controller_name' => 'SecurityController',
  133.             'form' => $form
  134.         ]);
  135.     }
  136. }