src/Controller/LoginController.php line 80

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Upartistik;
  4. use App\Repository\UpartistikRepository;
  5. use App\Entity\User;
  6. use App\Form\UserType;
  7. use App\Form\UserInscriptionType;
  8. use App\Repository\UserRepository;
  9. use App\Entity\Stats;
  10. use App\Form\StatsType;
  11. use App\Repository\StatsRepository;
  12. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\HttpFoundation\Response;
  15. use Symfony\Component\Routing\Annotation\Route;
  16. use Doctrine\ORM\EntityManagerInterface;
  17. use Symfony\Component\Security\Core\User\UserInterface;
  18. use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
  19. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  20. class LoginController extends AbstractController
  21. {
  22.   /**
  23.   * @var UserRepository
  24.   */
  25.   private $repository;
  26.   public function __construct(UpartistikRepository $upartRepoUserRepository $repositoryUserPasswordEncoderInterface $passwordEncoderEntityManagerInterface $em)
  27.   {
  28.     $this->repository $repository;
  29.     $this->em $em;
  30.     $this->passwordEncoder $passwordEncoder;
  31.     $this->upartRepo $upartRepo;
  32.   }
  33.     /**
  34.      * @Route("/login", name="login_index")
  35.      */
  36.     public function index(AuthenticationUtils $authenticationUtils): Response
  37.     {
  38.       $this->moucheUser("visite""login_index");
  39.       // get the login error if there is one
  40.       $error $authenticationUtils->getLastAuthenticationError();
  41.       // last username entered by the user
  42.       $lastUsername $authenticationUtils->getLastUsername();
  43.         return $this->render('login/index.html.twig', [
  44.             'controller_name' => 'LoginController',
  45.             'upartRepo' => $this->upartRepo->find(1),
  46.             'last_username' => $lastUsername,
  47.             'error'         => $error,
  48.             'users' => $this->repository->findAll(),
  49.         ]);
  50.     }
  51.     /**
  52.     * @Route("/logout", name="login_logout")
  53.     */
  54.     public function logout()
  55.     {
  56.       $this->moucheUser("visite""login_logout");
  57.       throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
  58.     }
  59.     /**
  60.      * @Route("/inscription", name="login_inscription", methods={"GET", "POST"})
  61.      */
  62.     public function inscription(Request $requestEntityManagerInterface $entityManager): Response
  63.     {
  64.         $this->moucheUser("visite""login_inscription");
  65.         $user = new User();
  66.         $form $this->createForm(UserInscriptionType::class, $user);
  67.         $form->handleRequest($request);
  68.         if ($form->isSubmitted() && $form->isValid()) {
  69.           $this->moucheUser("visite""login_inscription_valide ".$user->getUsername());
  70.           // crypte le mot de passe
  71.           $user->setPassword($this->passwordEncoder->encodePassword(
  72.             $user,
  73.             $form['password']->getData()
  74.           ));
  75.           $user->setRoles(['ROLE_ADMIN']);
  76.           //$user->setRoles(['ROLE_USER']);
  77.           $entityManager->persist($user);
  78.           $entityManager->flush();
  79.           $this->addFlash('success''Inscription Valide !');
  80.           return $this->redirectToRoute('login_index', [], Response::HTTP_SEE_OTHER);
  81.         }
  82.         /*
  83.         */
  84.         return $this->renderForm('login/inscription.html.twig', [
  85.           'controller_name' => 'user_inscription',
  86.           'upartRepo' => $this->upartRepo->find(1),
  87.           'previous' => $request->headers->get("referer"),
  88.             'user' => $user,
  89.             'form' => $form,
  90.         ]);
  91.     }
  92.     /**
  93.      * @Route("/inscriptionEnCours", name="login_inscriptionEnCours", methods={"GET", "POST"})
  94.      */
  95.     public function inscriptionEC(Request $requestEntityManagerInterface $entityManager): Response
  96.     {
  97.     }
  98.     public function moucheUser($action$page){
  99.       // $stat = new Stats();
  100.       // // Get IP address
  101.       // $ip_address = getenv('HTTP_CLIENT_IP') ?: getenv('HTTP_X_FORWARDED_FOR') ?: getenv('HTTP_X_FORWARDED') ?: getenv('HTTP_FORWARDED_FOR') ?: getenv('HTTP_FORWARDED') ?: getenv('REMOTE_ADDR');
  102.       // $stat->setIp($ip_address);
  103.       // // Get action
  104.       // $stat->setAction($action);
  105.       // $stat->setPage($page);
  106.       // $stat->setDateCrea(new \DateTime());
  107.       // // Get JSON object
  108.       // $jsondata = file_get_contents("http://timezoneapi.io/api/ip/?" . $ip_address . '&token=aYJMQZkEKzSLtQwhCmzD');
  109.       // // Decode
  110.       // $data = json_decode($jsondata, true);
  111.       // // Request OK?
  112.       // if($data['meta']['code'] == '200'){
  113.       //   $stat->setPays($data['data']['country']);
  114.       //   $stat->setRegion($data['data']['state']);
  115.       //   $stat->setVille($data['data']['city']);
  116.       // }
  117.       // $this->em->persist($stat);
  118.       // $this->em->flush();
  119.     }
  120. }