<?php
namespace App\Controller;
use App\Entity\Upartistik;
use App\Repository\UpartistikRepository;
use App\Entity\User;
use App\Form\UserType;
use App\Form\UserInscriptionType;
use App\Repository\UserRepository;
use App\Entity\Stats;
use App\Form\StatsType;
use App\Repository\StatsRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
class LoginController extends AbstractController
{
/**
* @var UserRepository
*/
private $repository;
public function __construct(UpartistikRepository $upartRepo, UserRepository $repository, UserPasswordEncoderInterface $passwordEncoder, EntityManagerInterface $em)
{
$this->repository = $repository;
$this->em = $em;
$this->passwordEncoder = $passwordEncoder;
$this->upartRepo = $upartRepo;
}
/**
* @Route("/login", name="login_index")
*/
public function index(AuthenticationUtils $authenticationUtils): Response
{
$this->moucheUser("visite", "login_index");
// get the login error if there is one
$error = $authenticationUtils->getLastAuthenticationError();
// last username entered by the user
$lastUsername = $authenticationUtils->getLastUsername();
return $this->render('login/index.html.twig', [
'controller_name' => 'LoginController',
'upartRepo' => $this->upartRepo->find(1),
'last_username' => $lastUsername,
'error' => $error,
'users' => $this->repository->findAll(),
]);
}
/**
* @Route("/logout", name="login_logout")
*/
public function logout()
{
$this->moucheUser("visite", "login_logout");
throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
}
/**
* @Route("/inscription", name="login_inscription", methods={"GET", "POST"})
*/
public function inscription(Request $request, EntityManagerInterface $entityManager): Response
{
$this->moucheUser("visite", "login_inscription");
$user = new User();
$form = $this->createForm(UserInscriptionType::class, $user);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$this->moucheUser("visite", "login_inscription_valide ".$user->getUsername());
// crypte le mot de passe
$user->setPassword($this->passwordEncoder->encodePassword(
$user,
$form['password']->getData()
));
$user->setRoles(['ROLE_ADMIN']);
//$user->setRoles(['ROLE_USER']);
$entityManager->persist($user);
$entityManager->flush();
$this->addFlash('success', 'Inscription Valide !');
return $this->redirectToRoute('login_index', [], Response::HTTP_SEE_OTHER);
}
/*
*/
return $this->renderForm('login/inscription.html.twig', [
'controller_name' => 'user_inscription',
'upartRepo' => $this->upartRepo->find(1),
'previous' => $request->headers->get("referer"),
'user' => $user,
'form' => $form,
]);
}
/**
* @Route("/inscriptionEnCours", name="login_inscriptionEnCours", methods={"GET", "POST"})
*/
public function inscriptionEC(Request $request, EntityManagerInterface $entityManager): Response
{
}
public function moucheUser($action, $page){
// $stat = new Stats();
// // Get IP address
// $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');
// $stat->setIp($ip_address);
// // Get action
// $stat->setAction($action);
// $stat->setPage($page);
// $stat->setDateCrea(new \DateTime());
// // Get JSON object
// $jsondata = file_get_contents("http://timezoneapi.io/api/ip/?" . $ip_address . '&token=aYJMQZkEKzSLtQwhCmzD');
// // Decode
// $data = json_decode($jsondata, true);
// // Request OK?
// if($data['meta']['code'] == '200'){
// $stat->setPays($data['data']['country']);
// $stat->setRegion($data['data']['state']);
// $stat->setVille($data['data']['city']);
// }
// $this->em->persist($stat);
// $this->em->flush();
}
}