<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
use Doctrine\ORM\EntityManagerInterface;
use App\Services\RoleService;
use App\Services\Utils;
use App\Entity\User;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
/**
* @Route("/")
*/
class AppController extends AbstractController
{
protected $em;
protected $utils;
public function __construct(RoleService $roleService, EntityManagerInterface $entityManager, \Swift_Mailer $mailer, Utils $utils) {
$this->roleService = $roleService;
$this->mailer = $mailer;
$this->em = $entityManager;
$this->utils = $utils;
}
/**
* @Route("/", name="home")
*/
public function home(): Response
{
//if ($this->getUser()) {
return $this->redirectToRoute('app_login');
//}
}
/**
* @Route("/import-seller", name="import-seller")
*/
public function importSeller()
{
$userCurrent = $this->get('security.token_storage')->getToken()->getUser();
if(!$this->roleService->isGranted('ROLE_ADMIN', $userCurrent)){
return $this->redirectToRoute('app_login');
}
//Import seller
$row = 0;
$separateur = ";";
$donnee = array();
$nom_fichier = "import-seller.csv";
$f = fopen ($nom_fichier,"r");
$taille = filesize($nom_fichier)+1;
while ($donnee = fgetcsv($f, $taille, $separateur)) {
if($row > 0){
$result[$row] = $donnee;
//var_dump($donnee);
$lastName = $result[$row][0];
$firstName = $result[$row][1];
$code = $result[$row][2];
$email = $result[$row][3];
$phone = $result[$row][4];
$siren = $result[$row][5];
//Remove " ";
$lastName = str_replace(" ", "", $lastName);
$firstName = str_replace(" ", "", $firstName);
$email = str_replace(" ", "", $email);
//Remove " ";
//$phone = str_replace(" ", "", $phone);
if(strlen($phone) == 13){
$phone = "0".$phone;
}
$formValid = true;
$userMailExist = $this->em
->getRepository(User::class)
->findOneBy(['email' => $email]);
if($userMailExist) {
$formValid = false;
}
if($formValid){
//Create user
$user = new User;
//Create salt for end signin
$tokenGenerator = random_bytes(10);
$user->setTokenReset(md5($tokenGenerator));
$user->setPassword('null');
$user->addRole('ROLE_SELLER');
$user->setStatus('SIGNINEND');
$user->setLastName($lastName);
$user->setFirstName($firstName);
$user->setCodeConseiller($code);
$user->setEmail($email);
$user->setUsername($email);
$user->setPhone($phone);
$user->setNumeroSiren($siren);
$this->em->persist($user);
$this->em->flush($user);
//Send email
$mailNoReply = $this->getParameter('mail_no_reply');
$senderName = $this->getParameter('sender_name');
$url = $this->get('router')->generate('signin_end', array('token' => $user->getTokenReset()), UrlGeneratorInterface::ABSOLUTE_URL);
$bodyEmail = $this->renderView(
'email/addUserWithToken.html.twig',
array('user' => $user, 'confirmationToken' => $url)
);
$message = (new \Swift_Message("Fin d'inscription"))
->setSubject("Fin d'inscription")
->setFrom(array($mailNoReply => $senderName))
->setTo(array($email => $senderName))
->setBody($bodyEmail,'text/html');
//$this->mailer->send($message);
var_dump("<br/>Success - ".$lastName." - ".$firstName." - ".$email);
}
else{
var_dump("<br/>Error - existe déjà - ".$lastName." - ".$firstName." - ".$email);
}
}
$row++;
}
fclose ($f);
var_dump("<br/><br/>END - ".$row);
die;
}
public function reSendMailAll(){
$userCurrent = $this->get('security.token_storage')->getToken()->getUser();
$users = $this->em
->getRepository(User::class)
->findBy(array("password" => "null"));
foreach ($users as $user) {
if($this->roleService->isGranted('ROLE_SELLER', $user) && $user->getStatus() == "SIGNINEND"){
if($user->getDateCreated()->format("Y") == "2020" && $user->getId() > 128){
//$this->utils->sendEmailEndSigninCustomer($user);
//var_dump($user->getRoles());
$this->addFlash('success', 'Le mail de fin d\'inscription a bien été envoyé - '.$user->getEmail());
}
}
}
return $this->redirectToRoute('customer_list');
}
/**
* @Route("/politique-de-confidentialite", name="confidentialite")
*/
public function confidentialite()
{
return $this->render('app/confidentialite.html.twig', [
]);
}
/**
* @Route("/conditions-generales-d-utilisation", name="condition_generales_utilisation")
*/
public function conditionGeneralesUtilisation()
{
return $this->render('app/conditionGeneralesUtilisation.html.twig', [
]);
}
/**
* @Route("/login-as/{userID}", name="login_as")
*/
public function LoginAsAction($userID)
{
$em = $this->em;
$userCurrent = $this->get('security.token_storage')->getToken()->getUser();
if ($this->roleService->isGranted('ROLE_ADMIN', $userCurrent) || $this->roleService->isGranted('ROLE_ADMIN_MANAGER', $userCurrent)) {
$user = $this->em
->getRepository(User::class)
->find($userID);
if(!$user){
$this->addFlash('danger', 'Cet utilisateur n\'est pas disponible');
return $this->redirectToRoute('app_login');
}
if ($this->roleService->isGranted('ROLE_ADMIN_MANAGER', $userCurrent) && !$this->roleService->isGranted('ROLE_MANAGER', $user)) {
$this->addFlash('danger', 'Cet utilisateur n\'est pas disponible');
return $this->redirectToRoute('app_login');
}
// Authenticating user
$token = new UsernamePasswordToken($user, null, 'main', $user->getRoles());
$this->get('security.token_storage')->setToken($token);
$this->get('session')->set('_security_main', serialize($token));
$this->addFlash('success', 'Vous êtes maintenant connecté en tant que '.$user->getLastname()." ".$user->getFirstname());
return $this->redirectToRoute('app_login');
} else {
$this->addFlash('danger', 'Cette page n\'est pas disponible');
return $this->redirectToRoute('app_login');
}
}
}