src/Controller/AppController.php line 37

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  4. use Symfony\Component\HttpFoundation\Response;
  5. use Symfony\Component\Routing\Annotation\Route;
  6. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  7. use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
  8. use Doctrine\ORM\EntityManagerInterface;
  9. use App\Services\RoleService;
  10. use App\Services\Utils;
  11. use App\Entity\User;
  12. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  13. /**
  14.  * @Route("/")
  15.  */
  16. class AppController extends AbstractController
  17. {
  18.     protected $em;
  19.     protected $utils;
  20.     public function __construct(RoleService $roleServiceEntityManagerInterface $entityManager\Swift_Mailer $mailerUtils $utils) {
  21.         $this->roleService $roleService;
  22.         $this->mailer $mailer;
  23.         $this->em $entityManager;
  24.         $this->utils $utils;
  25.     }
  26.     /**
  27.      * @Route("/", name="home")
  28.      */
  29.     public function home(): Response
  30.     {
  31.         //if ($this->getUser()) {
  32.             return $this->redirectToRoute('app_login');
  33.         //}
  34.     }
  35.     /**
  36.      * @Route("/import-seller", name="import-seller")
  37.      */
  38.     public function importSeller()
  39.     {
  40.         
  41.         $userCurrent $this->get('security.token_storage')->getToken()->getUser();
  42.         if(!$this->roleService->isGranted('ROLE_ADMIN'$userCurrent)){
  43.             return $this->redirectToRoute('app_login');
  44.         }
  45.         //Import seller
  46.         $row 0;
  47.         $separateur ";";
  48.         $donnee = array();    
  49.         $nom_fichier "import-seller.csv";
  50.         $f fopen ($nom_fichier,"r");
  51.         $taille filesize($nom_fichier)+1;
  52.         while ($donnee fgetcsv($f$taille$separateur)) {
  53.             if($row 0){
  54.                 $result[$row] = $donnee;
  55.                 //var_dump($donnee);
  56.                 $lastName $result[$row][0];
  57.                 $firstName $result[$row][1];
  58.                 $code $result[$row][2];
  59.                 $email $result[$row][3];
  60.                 $phone $result[$row][4];
  61.                 $siren $result[$row][5];
  62.                 //Remove " ";
  63.                 $lastName str_replace(" """$lastName);
  64.                 $firstName str_replace(" """$firstName);
  65.                 $email str_replace(" """$email);
  66.                 //Remove " ";
  67.                 //$phone = str_replace(" ", "", $phone);
  68.                 if(strlen($phone) == 13){
  69.                     $phone "0".$phone;
  70.                 }
  71.                 $formValid true;
  72.                 $userMailExist $this->em
  73.                     ->getRepository(User::class)
  74.                     ->findOneBy(['email' => $email]);
  75.                 if($userMailExist) {
  76.                     $formValid false;
  77.                 }
  78.                 if($formValid){
  79.                     //Create user
  80.                     $user = new User;
  81.                     //Create salt for end signin
  82.                     $tokenGenerator random_bytes(10);
  83.                     $user->setTokenReset(md5($tokenGenerator));
  84.                     $user->setPassword('null');
  85.                     $user->addRole('ROLE_SELLER');
  86.                     $user->setStatus('SIGNINEND');
  87.                     $user->setLastName($lastName);
  88.                     $user->setFirstName($firstName);
  89.                     $user->setCodeConseiller($code);
  90.                     $user->setEmail($email);
  91.                     $user->setUsername($email);
  92.                     $user->setPhone($phone);
  93.                     $user->setNumeroSiren($siren);
  94.                     $this->em->persist($user);
  95.                     $this->em->flush($user);
  96.                     //Send email
  97.                     $mailNoReply $this->getParameter('mail_no_reply');
  98.                     $senderName $this->getParameter('sender_name');
  99.                     $url $this->get('router')->generate('signin_end', array('token' => $user->getTokenReset()), UrlGeneratorInterface::ABSOLUTE_URL);
  100.                     $bodyEmail $this->renderView(
  101.                         'email/addUserWithToken.html.twig',
  102.                         array('user' => $user'confirmationToken' => $url)
  103.                     );
  104.                     $message = (new \Swift_Message("Fin d'inscription"))
  105.                         ->setSubject("Fin d'inscription")
  106.                         ->setFrom(array($mailNoReply => $senderName))
  107.                         ->setTo(array($email => $senderName))
  108.                         ->setBody($bodyEmail,'text/html');
  109.                     //$this->mailer->send($message);
  110.                     var_dump("<br/>Success - ".$lastName." - ".$firstName." - ".$email);
  111.                 }
  112.                 else{
  113.                     var_dump("<br/>Error - existe déjà - ".$lastName." - ".$firstName." - ".$email);
  114.                 }
  115.             }
  116.             $row++;
  117.         }
  118.         fclose ($f);
  119.         var_dump("<br/><br/>END - ".$row);
  120.         die;
  121.         
  122.     }
  123.     public function reSendMailAll(){
  124.         $userCurrent $this->get('security.token_storage')->getToken()->getUser();
  125.         $users $this->em
  126.             ->getRepository(User::class)
  127.             ->findBy(array("password" => "null"));
  128.         foreach ($users as $user) {
  129.             if($this->roleService->isGranted('ROLE_SELLER'$user) && $user->getStatus() == "SIGNINEND"){
  130.                 if($user->getDateCreated()->format("Y") == "2020" && $user->getId() > 128){
  131.                     //$this->utils->sendEmailEndSigninCustomer($user);
  132.                     //var_dump($user->getRoles());
  133.                     $this->addFlash('success''Le mail de fin d\'inscription a bien été envoyé - '.$user->getEmail());
  134.                 }
  135.             }
  136.         }
  137.         return $this->redirectToRoute('customer_list');
  138.     }
  139.     /**
  140.      * @Route("/politique-de-confidentialite", name="confidentialite")
  141.      */
  142.     public function confidentialite()
  143.     {
  144.         return $this->render('app/confidentialite.html.twig', [
  145.         ]);
  146.     }
  147.     /**
  148.      * @Route("/conditions-generales-d-utilisation", name="condition_generales_utilisation")
  149.      */
  150.     public function conditionGeneralesUtilisation()
  151.     {
  152.         return $this->render('app/conditionGeneralesUtilisation.html.twig', [
  153.         ]);
  154.     }
  155.     /**
  156.      * @Route("/login-as/{userID}", name="login_as")
  157.      */
  158.     public function LoginAsAction($userID)
  159.     {
  160.         $em $this->em;
  161.         $userCurrent $this->get('security.token_storage')->getToken()->getUser();
  162.         if ($this->roleService->isGranted('ROLE_ADMIN'$userCurrent) || $this->roleService->isGranted('ROLE_ADMIN_MANAGER'$userCurrent)) {
  163.             $user $this->em
  164.                 ->getRepository(User::class)
  165.                 ->find($userID);
  166.             if(!$user){
  167.                 $this->addFlash('danger''Cet utilisateur n\'est pas disponible');
  168.                 return $this->redirectToRoute('app_login');
  169.             }
  170.             if ($this->roleService->isGranted('ROLE_ADMIN_MANAGER'$userCurrent) && !$this->roleService->isGranted('ROLE_MANAGER'$user)) {
  171.                 $this->addFlash('danger''Cet utilisateur n\'est pas disponible');
  172.                 return $this->redirectToRoute('app_login');
  173.             }
  174.             // Authenticating user
  175.             $token = new UsernamePasswordToken($usernull'main'$user->getRoles());
  176.             $this->get('security.token_storage')->setToken($token);
  177.             $this->get('session')->set('_security_main'serialize($token));
  178.             $this->addFlash('success''Vous êtes maintenant connecté en tant que '.$user->getLastname()." ".$user->getFirstname());
  179.             return $this->redirectToRoute('app_login');
  180.         } else {
  181.             $this->addFlash('danger''Cette page n\'est pas disponible');
  182.             return $this->redirectToRoute('app_login');
  183.         }
  184.     }
  185. }