<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\HttpFoundation\Response;
use App\Entity\AffectationFonctionnalite;
use App\Entity\User;
use App\Repository\AffectationFonctionnaliteRepository;
use App\Repository\LogRepository;
use App\Repository\UserRepository;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Doctrine\ORM\EntityManagerInterface;
use App\Fonctions\Fonctions;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Email;
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
use Symfony\Component\Mime\Address;
use Symfony\Component\Routing\Annotation\Route;
class HomeController extends AbstractController
{
public function __construct(AffectationFonctionnaliteRepository $AffectationFonctionnaliteRepository,?UserInterface $user,SessionInterface $session,Fonctions $fonct,EntityManagerInterface $em)
{
$this->getUser = $user;
$this->em = $em;
$this->fonct = $fonct;
$this->session = $session;
$this->AffectationFonctionnaliteRepository = $AffectationFonctionnaliteRepository;
}
#[Route('/app_code_security', name: 'app_code_security')]
public function app_code_security(Request $request,UserPasswordHasherInterface $passwordHasher,MailerInterface $mailer): Response
{
// dd(1);
$this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
if ($this->getUser()->getStatut()!=1) {
$user=$this->getUser();
$action="Authentification";
$statut="Echouée";
$msg="Votre compte a été verrouiller, veuillez contacter votre superviseur";
$entity="User";
$identity="";
$old_v="";
$new_v="";
$log=$this->fonct->log($user,$action,$statut,$msg,$entity,$identity,$old_v,$new_v);
$this->em->persist($log);
$this->em->flush();
$this->addFlash('logout','Votre compte a été verrouiller, veuillez contacter votre superviseur');
return $this->redirectToRoute('first_logout');
}
if ($this->getUser()->isFirstConnexion()==1) {
$code_verfier=$this->generercodeInit();
$hashedPassword = $passwordHasher->hashPassword(
$this->getUser(),
$code_verfier
);
$date = new \DateTime('now');
$this->session->set('code_verif', $hashedPassword);
$this->session->set('date', $date);
$otp=$this->session->get('OTP');
//dd()
if ($otp==" ") {
// code...
return $this->redirectToRoute('app_logout');
}else{
if ($otp=="1") {
// code...
$contenu="Votre code de verification: ".$code_verfier;
$nbrecaractere=strlen($contenu);
$sms= $this->fonct->envoi_sms($this->getUser()->getTelephone(),$contenu);
$this->session->set('OTP'," ");
}else{
$email = (new TemplatedEmail())
->from(new Address('crm-garbal@noreply.airtel.africa','CRM GARBAL'))
->to(new Address($this->getUser()->getEmail()))
->subject('Code de verification pour acceder a votre espace Client!')
->htmlTemplate('security/text.html.twig')
->context([
'username' => $this->getUser(),
'code' => $code_verfier,
]);
$mailer->send($email);
$this->session->set('OTP'," ");
}
}
// $contenu="Votre code de verification: ".$code_verfier;
// $nbrecaractere=strlen($contenu);
// dd($nbrecaractere, $contenu);
//$sms= $this->fonct->envoi_sms(91056163,$contenu);
$user=$this->getUser();
$action="Authentification";
$statut="Success";
$msg="Email envoyé: recuperation de OTP";
$entity="User";
$identity="";
$old_v="";
$new_v="";
$log=$this->fonct->log($user,$action,$statut,$msg,$entity,$identity,$old_v,$new_v);
$this->em->persist($log);
$this->em->flush();
return $this->redirectToRoute('verification_compte');
}else{
$user=$this->getUser();
$action="Authentification";
$statut="Success";
$msg="Premiere connexion";
$entity="User";
$identity="";
$old_v="";
$new_v="";
$log=$this->fonct->log($user,$action,$statut,$msg,$entity,$identity,$old_v,$new_v);
$this->em->persist($log);
$this->em->flush();
return $this->render('security/first_connexion.html.twig', [
'controller_name' => 'HomeController',
'error' => '',
]);
}
}
#[Route('/home', name: 'app_home')]
public function index(Request $request,AffectationFonctionnaliteRepository $AffectationFonctionnaliteRepository): Response
{
$this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
//a enlever
$this->session->set('active_fonction',1);
if ($this->getUser()->isFirstConnexion()==1) {
// code...
if (in_array('ROLE_ADMINISTRATEUR', $this->getUser()->getRoles())==true || in_array('ROLE_COLLECTEUR', $this->getUser()->getRoles())==true) {
if (in_array('ROLE_ADMINISTRATEUR', $this->getUser()->getRoles())==true) {
$this->session->set('select', 1);
$this->session->set('Sousselect', 1);
// code...
}else{
$this->session->set('Sousselect', 1);
$this->session->set('select', 22);
}
// dd('1');
return $this->render('home/index.html.twig', [
'controller_name' => 'HomeController',
]);
// code...
}elseif (in_array('ROLE_TELECONSEILLER', $this->getUser()->getRoles())==true) {
$this->session->set('select', 7);
return $this->redirectToRoute('app_dashboard');
// return $this->render('home/index.html.twig', [
// 'controller_name' => 'HomeController',
// ]);
// code...
}elseif (in_array('ROLE_SUPERVISEUR', $this->getUser()->getRoles())==true) {
$this->session->set('select', 34);
return $this->redirectToRoute('app_dashboard_superviseur');
// return $this->render('home/index.html.twig', [
// 'controller_name' => 'HomeController',
// ]);
// code...
}elseif (in_array('ROLE_SNV', $this->getUser()->getRoles())==true) {
$this->session->set('select', 17);
return $this->redirectToRoute('app_rapport_historique_appel');
// return $this->render('home/index.html.twig', [
// 'controller_name' => 'HomeController',
// ]);
// code...
}elseif (in_array('ROLE_PARTENAIRE_MARKETPLACE', $this->getUser()->getRoles())==true) {
$this->session->set('select', 29);
$this->session->set('Sousselect', 57);
// dd($this->getUser()->getRoles());
return $this->render('plaintes/home.html.twig', [
'controller_name' => 'HomeController',
]);
}else{
return $this->render('home/index.html.twig', [
'controller_name' => 'HomeController',
]);
}
}else{
return $this->render('security/first_connexion.html.twig', [
'controller_name' => 'HomeController',
'error' => '',
]);
}
}
#[Route('/update_password/profil', name: 'update_password_profil')]
public function update_password_profil(Request $request,UserPasswordHasherInterface $passwordHasher,EntityManagerInterface $manger): Response
{
$this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
if ($request->request->count()>0){
$password=$request->request->get('password');
$confirm_password=$request->request->get('confirm_password');
$ancien_password=$request->request->get('ancien_password');
$user=$this->getUser();
$pattern = "/([a-z])/i";
$pattern1 = "/([0-9])/";
$pattern2 = "/[@\.\-\+\_\*\#\&]/";
//dd(preg_match($pattern, $password),preg_match($pattern1, $password),preg_match($pattern2, $password),$password);
if( password_verify($ancien_password, $user->getPassword())) {
if (preg_match($pattern, $password)==1 && preg_match($pattern1, $password) && preg_match($pattern2, $password)) {
if ($password==$confirm_password) {
// hash the password (based on the security.yaml config for the $user class)
$hashedPassword = $passwordHasher->hashPassword(
$user,
$password
);
$user->setPassword($hashedPassword);
$user->setFirstConnexion(1);
$manger->persist($user);
$manger->flush();
$user=$this->getUser();
$action="Changement mot de passe profil";
$statut="Success";
$msg="Votre mot de passe a été changer avec succés";
$entity="User";
$identity="";
$old_v="";
$new_v="";
$log=$this->fonct->log($user,$action,$statut,$msg,$entity,$identity,$old_v,$new_v);
$this->em->persist($log);
$this->em->flush();
return $this->redirectToRoute('app_home');
// code...
}else{
$user=$this->getUser();
$action="Changement mot de passe profil";
$statut="Echouée";
$msg="Les deux mots de passe ne sont pas identiques";
$entity="User";
$identity="";
$old_v="";
$new_v="";
$log=$this->fonct->log($user,$action,$statut,$msg,$entity,$identity,$old_v,$new_v);
$this->em->persist($log);
$this->em->flush();
return $this->render('security/update_password.html.twig', [
'controller_name' => 'AccueilController',
'error' => 'Les deux mots de passe ne sont pas identiques',
]);
}
// code...
}else{
$user=$this->getUser();
$action="Changement mot de passe profil";
$statut="Echouée";
$msg="le mot de passe doit contenir au moins 6 characteres dont au moins une lettre, un chiffre et un symbole (&#@.-+_*)";
$entity="User";
$identity="";
$old_v="";
$new_v="";
$log=$this->fonct->log($user,$action,$statut,$msg,$entity,$identity,$old_v,$new_v);
$this->em->persist($log);
$this->em->flush();
return $this->render('security/update_password.html.twig', [
'controller_name' => 'AccueilController',
'error' => 'le mot de passe doit contenir au moins 6 characteres dont au moins une lettre, un chiffre et un symbole (&#@.-+_*) ',
]);
}
}else{
$user=$this->getUser();
$action="Changement mot de passe profil";
$statut="Echouée";
$msg="Votre ancien mot de passe est incorrect";
$entity="User";
$identity="";
$old_v="";
$new_v="";
$log=$this->fonct->log($user,$action,$statut,$msg,$entity,$identity,$old_v,$new_v);
$this->em->persist($log);
$this->em->flush();
return $this->render('security/update_password.html.twig', [
'controller_name' => 'AccueilController',
'error' => 'Votre ancien mot de passe est incorrect',
]);
}
// code...
}
return $this->render('security/update_password.html.twig', [
'controller_name' => 'AccueilController',
'error' => "",
]);
}
#[Route('/update_password', name: 'update_password')]
public function update_password(Request $request,UserPasswordHasherInterface $passwordHasher,EntityManagerInterface $manger): Response
{
$this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
if ($request->request->count()>0){
$password=$request->request->get('password');
$confirm_password=$request->request->get('confirm_password');
$user=$this->getUser();
$pattern = "/([a-z])/i";
$pattern1 = "/([0-9])/";
$pattern2 = "/[@\.\-\+\_\*\#\&]/";
//dd(preg_match($pattern, $password),preg_match($pattern1, $password),preg_match($pattern2, $password),$password);
if (preg_match($pattern, $password)==1 && preg_match($pattern1, $password) && preg_match($pattern2, $password)) {
if ($password==$confirm_password) {
// hash the password (based on the security.yaml config for the $user class)
$hashedPassword = $passwordHasher->hashPassword(
$user,
$password
);
$user->setPassword($hashedPassword);
$user->setFirstConnexion(1);
$manger->persist($user);
$manger->flush();
$user=$this->getUser();
$action="Changement mot de passe";
$statut="Success";
$msg="Votre mot de passe a été changer avec succés";
$entity="User";
$identity="";
$old_v="";
$new_v="";
$log=$this->fonct->log($user,$action,$statut,$msg,$entity,$identity,$old_v,$new_v);
$this->em->persist($log);
$this->em->flush();
return $this->redirectToRoute('app_logout');
// code...
}else{
$user=$this->getUser();
$action="Changement mot de passe";
$statut="Echouée";
$msg="Les deux mots de passe ne sont pas identiques";
$entity="User";
$identity="";
$old_v="";
$new_v="";
$log=$this->fonct->log($user,$action,$statut,$msg,$entity,$identity,$old_v,$new_v);
$this->em->persist($log);
$this->em->flush();
return $this->render('security/first_connexion.html.twig', [
'controller_name' => 'AccueilController',
'error' => 'Les deux mots de passe ne sont pas identiques',
]);
}
// code...
}else{
$user=$this->getUser();
$action="Changement mot de passe";
$statut="Echouée";
$msg="le mot de passe doit contenir au moins 6 characteres dont au moins une lettre, un chiffre et un symbole (&#@.-+_*)";
$entity="User";
$identity="";
$old_v="";
$new_v="";
$log=$this->fonct->log($user,$action,$statut,$msg,$entity,$identity,$old_v,$new_v);
$this->em->persist($log);
$this->em->flush();
return $this->render('security/first_connexion.html.twig', [
'controller_name' => 'AccueilController',
'error' => 'le mot de passe doit contenir au moins 6 characteres dont au moins une lettre, un chiffre et un symbole (&#@.-+_*) ',
]);
}
// code...
}
return $this->render('security/verification_compte.html.twig', [
'controller_name' => 'AccueilController',
'error' => "",
]);
}
#[Route('/MonProfil/{id}', name: 'app_user_monprofil', methods: ['GET','POST'])]
public function show(EntityManagerInterface $em,Request $request,User $user,AffectationFonctionnaliteRepository $AffectationFonctionnaliteRepository,UserRepository $userRepository,LogRepository $LogRepository): Response
{
// dd(1);
$this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
$affectationFonctionProfil=$AffectationFonctionnaliteRepository->findBy(['id_user'=>$user->getId(),'statut'=>[0,1],'id_sous_fonctionnalite'=>null]);
$log=$LogRepository->findBy(['id_user'=>$user->getId()],['id'=>'DESC'],25);
$sousFonction=array();
foreach ($affectationFonctionProfil as $key) {
if ($key->getIdFonctionnalite()->getIdFonctionnalite()->getLien()=="#") {
$sousFonction[$key->getIdFonctionnalite()->getId()]=$AffectationFonctionnaliteRepository->listeSous($user->getId(),$key->getIdFonctionnalite());
// code...
}
// code...
}
return $this->render('admin/user/profil.html.twig', [
'user' => $user,
'log' => $log,
'menu' => $affectationFonctionProfil,
'sousmenu' => $sousFonction,
]);
}
public function MenuSection()
{
$nenu=array();
$menu['onglet']=$this->AffectationFonctionnaliteRepository->findListeOngletBy($this->getUser()->getId());
foreach ($menu['onglet'] as $key) {
$menu['menu'][$key->getId()]=$this->AffectationFonctionnaliteRepository->findBy(['id_user'=>$this->getUser()->getId(),'statut'=>1,'id_onglet'=>$key->getId()]);
foreach ($menu['menu'][$key->getId()] as $value) {
if ($value->getIdFonctionnalite()->getIdFonctionnalite()->getLien()=="#") {
$menu['sousmenu'][$value->getIdFonctionnalite()->getId()]=$this->AffectationFonctionnaliteRepository->listeSousMenu($this->getUser()->getId(),$value->getIdFonctionnalite()->getId());
// code...
}
// code...
}
// code...
}
return $menu;
}
public function generercodeInit()
{
$haz=array(1,
rand(2,3),
rand(4,5),
rand(6,7),
rand(8,9),0);
shuffle($haz);
$co="";
$i=0;
while($i<6){
$co.= $haz[$i];
if(!in_array($co,$haz)){
}else{}
$i++;
}
return $co;
}
}