src/Controller/SecurityController.php line 65

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Users;
  4. use App\Entity\Roles;
  5. use App\Repository\UsersRepository
  6. use App\Repository\RolesRepository;
  7. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use App\Form\ResetPasswordRequestFormType;
  10. use App\Form\RegistrationFormType;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\Routing\Annotation\Route;
  13. use Symfony\Component\Security\Core\Security;
  14. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  15. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  16. use Symfony\Contracts\Translation\TranslatorInterface;
  17. use Symfony\Component\Mailer\MailerInterface;
  18. use Symfony\Component\Mime\Email;
  19. use Doctrine\ORM\EntityManagerInterface;
  20. use PHPMailer\PHPMailer\PHPMailer;
  21. use PHPMailer\PHPMailer\Exception;
  22. class SecurityController extends AbstractController
  23. {   
  24.     private $rolesRepository;
  25.     private $em;
  26.     private $UsersRepository;
  27.     public function __construct(EntityManagerInterface $emUserPasswordHasherInterface $userPasswordHasherUsersRepository $UsersRepositoryRolesRepository $rolesRepositorySecurity $security)
  28.     {
  29.         $this->security $security;
  30.         $this->em $em;
  31.         $this->rolesRepository $rolesRepository;
  32.         $this->UsersRepository $UsersRepository;
  33.     }
  34.     #[Route(path'/checklogin'name'app_check_login')]
  35.     public function checklogin(Security $securityAuthenticationUtils $authenticationUtils): Response
  36.     {
  37.         $user $security->getUser();
  38.         if (!$user) {
  39.             return $this->redirectToRoute('app_login');
  40.         }
  41.         $userRole $user->getUserRole();
  42.         if (!$userRole) {
  43.             return $this->redirectToRoute('app_index');
  44.         }
  45.         $roles $userRole->getRoles();
  46.         if ($roles == 'CLIENT') {
  47.             return $this->redirectToRoute('app_client');
  48.         }
  49.         return $this->redirectToRoute('app_index');
  50.     }
  51.     #[Route(path'/createclient'name'app_create_client')]
  52.     public function createclient(Request $request,UserPasswordHasherInterface $userPasswordHasher,EntityManagerInterface $em): Response 
  53.     {
  54.             $flag 0;
  55.             $user = new Users();
  56.             $form $this->createForm(RegistrationFormType::class, $user);
  57.             $form->handleRequest($request);
  58.             $now = new \DateTimeImmutable();
  59.             if ($form->isSubmitted() && $form->isValid()) {
  60.                 $token substr(md5(openssl_random_pseudo_bytes(20)), -10);
  61.                 $user->setTokens($token);
  62.                 $email $user->getUsername();
  63.                 try {
  64.                     $mail = new PHPMailer(true);
  65.                     $mail->CharSet 'UTF-8';
  66.                     $mail->isSMTP();
  67.                     $mail->Host 'sosconsultoria.pt';
  68.                     $mail->SMTPAuth true;
  69.                     $mail->Username 'suporte@sosconsultoria.pt';
  70.                     $mail->Password $_SERVER['MAILER_PASSWORD'];
  71.                     $mail->SMTPSecure 'ssl';
  72.                     $mail->Port 465;
  73.                     $mail->setFrom('suporte@sosconsultoria.pt''SOS Consultoria');
  74.                     $mail->addAddress($email);
  75.                     $mail->addReplyTo('suporte@sosconsultoria.pt''SOS Consultoria');
  76.                     $mail->isHTML(true);
  77.                     $mail->Subject 'Tickets Central';
  78.                     $mail->Body '
  79.                         <table style="font-family: Helvetica, Arial, sans-serif; font-size: 14px;">
  80.                             <tr><td><strong>Tickets Central</strong></td></tr>
  81.                             <tr><td>Por favor confirme o seu e-mail clicando no link abaixo:</td></tr>
  82.                             <tr><td><a href="https://helpdesk.sosadvanced.pt/logint/' $token '" style="color: #608E34; font-weight: bold;">Confirmar e-mail</a></td></tr>
  83.                             <tr><td>— <b>Tickets</b> - Admin Dashboard</td></tr>
  84.                         </table>';
  85.                     $mail->send();
  86.                 } catch (MailException $e) {
  87.                     return new Response('Erro ao enviar e-mail: ' $mail->ErrorInfo);
  88.                 }
  89.                 $user->setPassword(
  90.                     $userPasswordHasher->hashPassword(
  91.                         $user,
  92.                         $form->get('plainPassword')->getData()
  93.                     )
  94.                 );
  95.                 $user->setPhoto('default.png');
  96.                 $user->setActive(1);
  97.                 $user->setUpdatedAt($now);
  98.                 $user->setCreatedAt($now);
  99.                 $userRole $this->rolesRepository->find(4);
  100.                 $user->setUserRole($userRole);
  101.                 $em->persist($user);
  102.                 $em->flush();
  103.                 $flag 1;
  104.                 return $this->redirectToRoute('app_confirm');
  105.             }
  106.             return $this->render('client_area/security/register.html.twig', [
  107.                 'registrationForm' => $form->createView(),
  108.                 'flag' => $flag,
  109.             ]);
  110.         }
  111.     #[Route(path'/login'name'app_login')]
  112.     public function login(AuthenticationUtils $authenticationUtils): Response
  113.     {
  114.         $error $authenticationUtils->getLastAuthenticationError();
  115.         $lastUsername $authenticationUtils->getLastUsername();
  116.         return $this->render('security/login.html.twig', ['username' => $lastUsername'error' => $error]);
  117.     }
  118.     #[Route(path'/logout'name'app_logout')]
  119.     public function logout(): void
  120.     {
  121.         throw new \LogicException(); 
  122.     }
  123.     #[Route(path'/logint/{token}'name'app_login_with_token')]
  124.     public function loginwithtoken($tokenAuthenticationUtils $authenticationUtils): Response
  125.     {
  126.         if($UsersRepository $this->UsersRepository->findbyToken($token)){
  127.             foreach($UsersRepository as $Users){
  128.                 $userusername $Users['username'];
  129.                 $userid $Users['id'];
  130.             }
  131.             $UserL $this->UsersRepository->removetoken($userid);
  132.             return $this->redirectToRoute('app_login');
  133.         }else{
  134.             return $this->redirectToRoute('app_home');
  135.         }
  136.       
  137.        /*  $UsersRepository = $this->UsersRepository->findbyToken($token);
  138.         foreach($UsersRepository as $Users){
  139.             $userusername = $Users['username'];
  140.             $userid = $Users['id'];
  141.         }
  142.         if(!isset($userusername)){return $this->redirectToRoute('app_login');}
  143.         if($_POST){
  144.             $UserL = $this->UsersRepository->removetoken($userid);
  145.             return $this->redirectToRoute('app_check_login');
  146.         }
  147.         $error = $authenticationUtils->getLastAuthenticationError();
  148.         $lastUsername = $authenticationUtils->getLastUsername(); */
  149.         return $this->render('security/login.html.twig', ['last_username' => $lastUsername'error' => $error'username' => $userusername]);
  150.     }
  151.     #[Route('/resetpassword'methods:['GET','POST'], name'app_reset_password')]
  152.     public function register(Request $requestUserPasswordHasherInterface $userPasswordHasherEntityManagerInterface $entityManager): Response
  153.     {
  154.         $flag 0;
  155.         if (isset($_POST["email"])) {
  156.             $email $_POST["email"];
  157.             $users $this->UsersRepository->findbyEmail($email);
  158.             if (!$users) {
  159.                 return new Response("Utilizador não encontrado."404);
  160.             }
  161.             foreach ($users as $user1) {
  162.                 $userid $user1["id"];
  163.             }
  164.             $token substr(md5(openssl_random_pseudo_bytes(20)), -10);
  165.             $user $this->UsersRepository->find($userid);
  166.             $user->setTokens($token);
  167.             try {
  168.                 $mail = new PHPMailer(true);
  169.                 $mail->CharSet 'UTF-8';
  170.                 $mail->isSMTP();
  171.                 $mail->Host 'sosconsultoria.pt';
  172.                 $mail->SMTPAuth true;
  173.                 $mail->Username 'suporte@sosconsultoria.pt';
  174.                 $mail->Password $_SERVER['MAILER_PASSWORD'];
  175.                 $mail->SMTPSecure 'ssl';
  176.                 $mail->Port 465;
  177.                 $mail->setFrom('suporte@sosconsultoria.pt''SOS Consultoria');
  178.                 $mail->addAddress($email);
  179.                 $mail->addReplyTo('suporte@sosconsultoria.pt''SOS Consultoria');
  180.                 $mail->isHTML(true);
  181.                 $mail->Subject 'Tickets Central - Recuperação de Password';
  182.                 $mail->Body '
  183.                     <table style="font-family: Helvetica, Arial, sans-serif; font-size: 14px;">
  184.                         <tr><td><strong>Tickets Central</strong></td></tr>
  185.                         <tr><td>Por favor clique no link abaixo para alterar a sua password:</td></tr>
  186.                         <tr><td><a href="https://helpdesk.sosadvanced.pt/newpassword/' $token '" style="color: #608E34; font-weight: bold;">Alterar Password</a></td></tr>
  187.                         <tr><td>— <b>Tickets</b> - Admin Dashboard</td></tr>
  188.                     </table>
  189.                 ';
  190.                 $mail->send();
  191.             } catch (MailException $e) {
  192.                 return new Response('Erro ao enviar e-mail: ' $mail->ErrorInfo);
  193.             }
  194.             $entityManager->persist($user);
  195.             $entityManager->flush();
  196.             $flag 1;
  197.             return $this->render('reset_password/check_email.html.twig', [
  198.                 'email' => $email,
  199.             ]);
  200.         }
  201.         return $this->render('reset_password/request.html.twig');
  202.     }
  203.     #[Route(path'/newpassword/{token}',methods:['GET','POST'], name'app_newpassword')]
  204.     public function newpassword($tokenRequest $requestUserPasswordHasherInterface $userPasswordHasherEntityManagerInterface $entityManager): Response
  205.     {
  206.         if(isset($_POST["newpassword"]))
  207.         {
  208.             $user = new Users;
  209.             $UsersRepository $this->UsersRepository->findbyToken($token);
  210.             foreach($UsersRepository as $Users){
  211.                 $username=$Users['username'];
  212.                 $userid $Users['id'];
  213.             }
  214.             $user=$this->UsersRepository->find($userid);
  215.             $user->setPassword(
  216.                 $userPasswordHasher->hashPassword(
  217.                         $user,
  218.                         $_POST['newpassword']
  219.                     )
  220.                 );
  221.             
  222.             $entityManager->persist($user);
  223.             $entityManager->flush();
  224.                 
  225.                 $user $this->UsersRepository->removetoken($userid);
  226.              
  227.                 return $this->redirectToRoute('app_login',[
  228.                     
  229.                 ]);
  230.               
  231.         }
  232.         return $this->render('reset_password/reset.html.twig', [
  233.             'token'=>$token,
  234.         ]);
  235.     }
  236.     #[Route(path'/password/{token}',methods:['GET','POST'], name'app_createpassword')]
  237.     public function createpassword($tokenRequest $requestUserPasswordHasherInterface $userPasswordHasherEntityManagerInterface $entityManager): Response
  238.     {
  239.         if(isset($_POST["choosepassword"]))
  240.         {
  241.             $user = new Users;
  242.             $UsersRepository $this->UsersRepository->findbyToken($token);
  243.             foreach($UsersRepository as $Users){
  244.                 $username=$Users['username'];
  245.                 $userid $Users['id'];
  246.             }
  247.             $user=$this->UsersRepository->find($userid);
  248.             
  249.             $user->setPassword(
  250.                 $userPasswordHasher->hashPassword(
  251.                         $user,
  252.                         $_POST['choosepassword']
  253.                     )
  254.                 );
  255.             
  256.             $entityManager->persist($user);
  257.             $entityManager->flush();
  258.                 
  259.                 $user $this->UsersRepository->removetoken($userid);
  260.              
  261.                 return $this->redirectToRoute('app_login',[
  262.                     
  263.                 ]);
  264.               
  265.         }
  266.         return $this->render('reset_password/enterpassword.html.twig', [
  267.             'token'=>$token,
  268.         ]);
  269.     }
  270. }