src/Controller/SecurityController.php line 37

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\AdminMaintenance;
  4. use App\Entity\Logactivity;
  5. use App\Entity\Pages;
  6. use App\Entity\User;
  7. use App\Form\UserType;
  8. use Doctrine\Persistence\ManagerRegistry;
  9. use Symfony\Bridge\Twig\Mime\TemplatedEmail;
  10. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  11. use Symfony\Component\HttpFoundation\RedirectResponse;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\Mailer\MailerInterface;
  15. use Symfony\Component\PasswordHasher\Hasher\PasswordHasherFactoryInterface;
  16. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasher;
  17. use Symfony\Component\Routing\Annotation\Route;
  18. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  19. use Scheb\TwoFactorBundle\Security\TwoFactor\Provider\Google\GoogleAuthenticatorInterface;
  20. use App\Controller\DGSMailsController;
  21. /**
  22.  * @Route("/dgs/cnx")
  23.  */
  24. class SecurityController extends AbstractController
  25. {
  26.     public function __construct(ManagerRegistry $doctrineDGSMailsController $DGSMailsController) {
  27.         $this->doctrine $doctrine;
  28.         $this->DGSMailsController $DGSMailsController;
  29.     }
  30.     /**
  31.      * @Route("/dgs-enregistrement", name="registration")
  32.      */
  33.     public function registration(Request $request,PasswordHasherFactoryInterface $passwordHasherFactoryGoogleAuthenticatorInterface $googleAuthenticator): Response // OK Gauthier
  34.     {
  35.         //On désactive l'inscription
  36.         return $this->redirectToRoute('app_login');
  37.         
  38.         //On va créer le formulaire pour User
  39.         $user = new User();
  40.         $form $this->createForm(UserType::class, $user);
  41.         $form->handleRequest($request);
  42.         if ($form->isSubmitted()) {
  43.             //Token
  44.             $submittedToken $request->get('user');
  45.             if ( $form->isValid() && $this->isCsrfTokenValid('LRMYDu5alu'$submittedToken['_token']) )
  46.             {
  47.                 //On récupere les données
  48.                 $user $form->getData();
  49.                 //On encrypt le mot de pass
  50.                 // Encode the plain password, and set it.
  51.                 $passwordHasher = new UserPasswordHasher($passwordHasherFactory);
  52.                 $encodedPassword $passwordHasher->hashPassword(
  53.                     $user,
  54.                     $form->get('password')->getData()
  55.                 );
  56.                 $user->setPassword($encodedPassword);
  57.                 //On lui defini un role
  58.                 $user->setRoles( array('ROLE_USER') );
  59.                 $user->setUsername($user->getEmail());
  60.                 //2Fa
  61.                 //$user->setGoogleAuthenticatorSecret($googleAuthenticator->generateSecret());
  62.                 $user->setEmailAuthenticationCode(random_int(1000009999999));
  63.                 $user->setGoogleAuthenticatorSecret('');
  64.                 //On écrit dans la BDD
  65.                 $entityManager $this->doctrine->getManager();
  66.                 $entityManager->persist($user);
  67.                 $entityManager->flush();
  68.                 //Mail
  69.                 $this->DGSMailsController->submitMailToGeneral($user"Confirmation inscription",'');
  70.                 $this->DGSMailsController->submitMailToGeneral(''"Admin - Nouvelle inscription",'');
  71.                 $this->addFlash('success'"Votre inscription a été enregistrée !");
  72.                 return $this->redirectToRoute('app_login');
  73.             } else {
  74.                 $mess "Une erreur s'est produite. Essayez de recharger la page.";
  75.                 if($request->query->get('registration')){
  76.                     $this->DGSMailsController->submitMailToAdmin($user);
  77.                     $mess $mess " - 1";
  78.                 }
  79.                 $this->addFlash('error'$mess);
  80.             }
  81.         }
  82.         return $this->render('security/registration.html.twig', [
  83.             'csrf_token' => 'LRMYDu5alu',
  84.             'form'      => $form->createView()
  85.         ]);
  86.     }
  87.     /**
  88.      * @Route("/dgs-login", name="app_login")
  89.      */
  90.     public function login(AuthenticationUtils $authenticationUtils): Response // OK Gauthier
  91.     {
  92.         //Si déjà identifié
  93.         if ($this->getUser()) {
  94.             return $this->redirectToRoute('admin');
  95.         }
  96.         // get the login error if there is one
  97.         $error $authenticationUtils->getLastAuthenticationError();
  98.         // last username entered by the user
  99.         $lastUsername $authenticationUtils->getLastUsername();
  100.         return $this->render('security/login.html.twig', [
  101.             'last_username' => $lastUsername,
  102.             'error' => $error
  103.         ]);
  104.     }
  105.     /**
  106.      * @Route("/dgs-logout", name="app_logout")
  107.      */
  108.     public function logout() // OK Gauthier
  109.     {
  110.         throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
  111.     }
  112.     /**
  113.      * @Route("/ip/activate", name="dgsipactivate")
  114.      */
  115.     public function dgsipactivate(Request $requestManagerRegistry $doctrineMailerInterface $mailer): Response // OK Gauthier
  116.     {
  117.         //Variables
  118.         $return false;
  119.         $action $request->get('action');
  120.         $ip "";
  121.         $em $doctrine->getManager();
  122.         //On récupére les info dans twig.yaml
  123.         $twig $this->container->get('twig');
  124.         $globals $twig->getGlobals();
  125.         if($action == "go") {
  126.             //On va ajouter l'IP à la base de donnée
  127.             if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
  128.                 $ip $_SERVER['HTTP_CLIENT_IP'];
  129.             } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
  130.                 $ip $_SERVER['HTTP_X_FORWARDED_FOR'];
  131.             } else {
  132.                 $ip $_SERVER['REMOTE_ADDR'];
  133.             }
  134.             //On recherche la BDD
  135.             $EntityDgstools $em->getRepository(AdminMaintenance::class)->findOneBy(array('id'=>1));
  136.             if($EntityDgstools && $ip != "") {
  137.                 //On va rechercher la liste des IPs
  138.                 $listIps $EntityDgstools->getIpmaintenance();
  139.                 $listIpsArray explode(","$listIps);
  140.                 //On boucle
  141.                 $listIpsString $ip;
  142.                 foreach($listIpsArray as $listIpDetail) {
  143.                     if($listIpDetail != $ip) {
  144.                         $listIpsString $listIpsString .",".$listIpDetail;
  145.                     }
  146.                 }
  147.                 $EntityDgstools->setIpmaintenance($listIpsString);
  148.                 $em->persist($EntityDgstools);
  149.                 $em->flush();
  150.                 //On envoi un mail
  151.                 $email = (new TemplatedEmail())
  152.                     ->from($globals['email_reset_dgs'])
  153.                     ->to($globals['email_reset_dgs'])
  154.                     ->subject('['.$globals['nom_normalise'].'] - IP débloqué')
  155.                     ->htmlTemplate('emails/ip.html.twig')
  156.                     ->context([
  157.                         'message' => "L'IP suivante vient d'être débloquée : " $ip
  158.                     ])
  159.                 ;
  160.                 $mailer->send($email);
  161.             }
  162.             $return true;
  163.         }
  164.         return $this->render('admin/dgstools/ipactivation.html.twig', [
  165.             "return" => $return,
  166.             "ip" => $ip
  167.         ]);
  168.     }
  169. }