<?php
namespace App\Controller\Users\User;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use App\Security\TokenAuthenticator;
use Symfony\Component\Security\Guard\GuardAuthenticatorHandler;
use App\Entity\Users\User\User;
use Doctrine\ORM\EntityManagerInterface;
use App\Entity\Users\User\Slideaccueil;
use App\Service\Servicetext\GeneralServicetext;
use App\Entity\Users\Adminuser\Parametre;
use App\Entity\Projet\Partenaire\Partenaire;
use App\Entity\Produit\Service\Testimonial;
use App\Entity\Produit\Service\Team;
use App\Entity\Produit\Service\Faq;
use App\Entity\Produit\Service\Article;
use App\Entity\Projet\Portfolio\Intervention;
use Symfony\Component\HttpFoundation\Request;
use App\Entity\Produit\Produit\Produit;
use App\Entity\Produit\Service\Service;
class SecurityController extends AbstractController
{
private $params;
private $authenticator;
private $guardHandler;
private $_entityManager;
public function __construct(ParameterBagInterface $params, TokenAuthenticator $authenticator, GuardAuthenticatorHandler $guardHandler, EntityManagerInterface $entityManager)
{
$this->params = $params;
$this->authenticator = $authenticator;
$this->guardHandler = $guardHandler;
$this->_entityManager = $entityManager;
}
public function accueilsite(GeneralServicetext $service)
{
/*$var = '["val une", "val 2"]';
$var = json_decode($var, TRUE);
print_r($var);
exit;*/
$liste_slide = $this->_entityManager->getRepository(Slideaccueil::class)
->findAll();
$slide = $service->selectEntity($liste_slide);
$aboutParam = $this->_entityManager->getRepository(Parametre::class)
->findOneBy(array('typeParam'=>'aboutWithArticle'), array('rang'=>'asc'), 1);
if($aboutParam != null)
{
$aboutParam->setEm($this->_entityManager);
}
$liste_partenaire = $this->_entityManager->getRepository(Partenaire::class)
->findAll();
$aboutService = $this->_entityManager->getRepository(Parametre::class)
->findOneBy(array('typeParam'=>'detailservice'), array('rang'=>'asc'), 1);
$aboutTestimonial = $this->_entityManager->getRepository(Parametre::class)
->findOneBy(array('typeParam'=>'detailtestimonial'), array('rang'=>'asc'), 1);
$liste_testimonial = $this->_entityManager->getRepository(Testimonial::class)
->myfindAll();
$aboutCalltoaction = $this->_entityManager->getRepository(Parametre::class)
->findOneBy(array('typeParam'=>'calltoaction'), array('rang'=>'asc'), 1);
$aboutDetailteam = $this->_entityManager->getRepository(Parametre::class)
->findOneBy(array('typeParam'=>'detailteam'), array('rang'=>'asc'), 1);
$liste_team = $this->_entityManager->getRepository(Team::class)
->myfindAll();
$aboutPricing = $this->_entityManager->getRepository(Parametre::class)
->findOneBy(array('typeParam'=>'detailpricing'), array('rang'=>'asc'), 1);
$aboutFaq = $this->_entityManager->getRepository(Parametre::class)
->findOneBy(array('typeParam'=>'detailfaq'), array('rang'=>'asc'), 1);
$aboutBlog = $this->_entityManager->getRepository(Parametre::class)
->findOneBy(array('typeParam'=>'detailblog'), array('rang'=>'asc'), 1);
$liste_faq = $this->_entityManager->getRepository(Faq::class)
->myfindAll();
$liste_blog = $this->_entityManager->getRepository(Article::class)
->myfindAll(1,6);
$aboutIntervention = $this->_entityManager->getRepository(Parametre::class)
->findOneBy(array('typeParam'=>'detailintervention'), array('rang'=>'asc'), 1);
$liste_intervention = $this->_entityManager->getRepository(Intervention::class)
->findTopProjet();
foreach($liste_intervention as $intervention)
{
$intervention->setEm($this->_entityManager);
}
$liste_produit = $this->_entityManager->getRepository(Produit::class)
->myFindBy();
foreach($liste_produit as $produitCurrent)
{
$produitCurrent->setEm($this->_entityManager);
}
$repository = $this->_entityManager->getRepository(Service::class);
$liste_service = $repository->findServiceTypePagine("service", 1, 20);
$liste_service = $service->selectEntities($liste_service, 6);
$repository = $this->_entityManager->getRepository(Service::class);
$liste_departement = $repository->findServiceTypePagine("departement", 1, 20);
$liste_departement = $service->selectEntities($liste_departement, 6);
return $this->render($service->getThemeDirectory().'/Users/User/Security/accueilsite.html.twig',
array('slide'=>$slide, 'aboutParam'=>$aboutParam, 'liste_partenaire'=>$liste_partenaire, 'aboutService'=>$aboutService,
'aboutTestimonial'=>$aboutTestimonial, 'liste_testimonial'=>$liste_testimonial, 'aboutCalltoaction'=>$aboutCalltoaction,
'liste_team'=>$liste_team,'aboutDetailteam'=>$aboutDetailteam, 'aboutPricing'=>$aboutPricing, 'aboutFaq'=>$aboutFaq,
'aboutBlog'=>$aboutBlog, 'liste_faq'=>$liste_faq, 'liste_blog'=>$liste_blog, 'aboutIntervention'=>$aboutIntervention,
'liste_intervention'=>$liste_intervention, 'liste_produit'=>$liste_produit, 'liste_service'=>$liste_service, 'liste_departement'=>$liste_departement));
}
public function login(GeneralServicetext $service, Request $request)
{
$em = $this->getDoctrine()->getManager();
// Si le visiteur est déjà identifié, on le redirige vers l'accueil
if($this->getUser() != null){ //IS_AUTHENTICATED_REMEMBERED
return $this->redirect($this->generateUrl('users_user_accueil'));
}
//connexion sécurisé user.
$error_login = '';
$last_username = null;
if($request->getMethod() == 'POST' and $this->getUser() == null){
if (isset($_POST['_username']) and isset($_POST['_password'])){
$repository = $em->getRepository(User::class);
$user = $repository->findOneBy(array('username'=>$_POST['_username']));
if($user != null)
{
if($_POST['_password'] == $service->decrypt($user->getPassword(),$user->getSalt()))
{
$response = $this->guardHandler->authenticateUserAndHandleSuccess(
$user, // the User object you just created
$request,
$this->authenticator, // authenticator whose onAuthenticationSuccess you want to use
'main' // the name of your firewall in security.yaml
);
//$token = new UsernamePasswordToken($user, null, 'main', $user->getRoles());
//$this->get('security.token_storage')->setToken($token);
//$this->get('session')->set('_security_main', serialize($token));
// Verifie si le cookie n existe pas
if((!isset($_COOKIE["PIDSESSREM"]) or $_COOKIE["PIDSESSREM"] == 'delete') and isset($_POST['_remember_me']) and $_POST['_remember_me'] == true)
{
// Stock les infos du cookie
$cookie_info = array(
'name' => 'PIDSESSREM',
'value' => $service->encrypt($user->getUsername(),$this->params->get('saltcookies')),
'time' => time() + (3600 * 24 * 360)
);
// Cree le cookie
setCookie($cookie_info['name'], $cookie_info['value'], $cookie_info['time'],'/');
setCookie('PIDSESSDUR',$cookie_info['time'], $cookie_info['time'],'/');
}
$session = $this->get('session');
$target_link = $session->get('_security.welcome.target_path');
if($target_link != null and strlen($target_link) > 5)
{
return $this->redirect($target_link);
}else{
return $this->redirect($this->generateUrl('users_user_accueil'));
}
}else{
$error_login = '<span style="color: red;">Echec: Mot de passe ou Email invalide.</span>';
$last_username = $_POST['_username'];
}
}else{
$last_username = $_POST['_username'];
}
}
}
return $this->render($service->getThemeDirectory().'/Users/User/Security/login.html.twig',
array('last_username' => $last_username,'error'=> $error_login));
}
}