src/Controller/SecurityController.php line 71

  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Employee;
  4. use App\Entity\User;
  5. use App\Form\UserPasswordType;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use Doctrine\Persistence\ManagerRegistry;
  8. use Knp\Snappy\Pdf;
  9. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  10. use Symfony\Component\HttpFoundation\JsonResponse;
  11. use Symfony\Component\HttpFoundation\RedirectResponse;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  15. use Symfony\Component\Routing\Annotation\Route;
  16. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  17. class SecurityController extends AbstractController
  18. {
  19.     /**
  20.      * @var ManagerRegistry $doctrine
  21.      */
  22.     private ManagerRegistry $doctrine;
  23.     /**
  24.      * @param ManagerRegistry $doctrine
  25.      */
  26.     public function __construct(ManagerRegistry $doctrine)
  27.     {
  28.         $this->doctrine $doctrine;
  29.     }
  30.     /**
  31.      * @Route("/inscription", name="security_registration")
  32.      * @param Request $request
  33.      * @param EntityManagerInterface $manager
  34.      * @param UserPasswordHasherInterface $encoder
  35.      * @return Response
  36.      */
  37.     public function registration(
  38.         Request $request,
  39.         EntityManagerInterface $manager,
  40.         UserPasswordHasherInterface $encoder
  41.     ): Response {
  42.         $user = new User();
  43. //        $form = $this->createForm(RegistrationType::class, $user);
  44. //        $form->handleRequest($request);
  45. //        if ($form->isSubmitted() && $form->isValid()) {
  46. //            $hash = $encoder->encodePassword($user, $user->getPassword());
  47. //            $user->setPassword($hash);
  48. //            $manager->persist($user);
  49. //            $manager->flush();
  50. //
  51. //            return $this->redirectToRoute('security_login');
  52. //        }
  53.         return $this->render('security/registration.html.twig', [
  54. //            'form' => $form->createView()
  55.         ]);
  56.     }
  57.     /**
  58.      * @Route("/login", name="security_login")
  59.      * @param AuthenticationUtils $authenticationUtils
  60.      * @return Response
  61.      */
  62.     public function login(AuthenticationUtils $authenticationUtils): Response
  63.     {
  64.         // diable local login only for prod
  65.         if (strtoupper($this->getParameter('app.env')) === "PROD")
  66.             return $this->redirectToRoute('security_ldap_login');
  67.         // get the login error if there is one
  68.         $error $authenticationUtils->getLastAuthenticationError();
  69.         // last username entered by the user
  70.         $lastUsername $authenticationUtils->getLastUsername();
  71.         return $this->render('security/login.html.twig', [
  72.             'last_username' => $lastUsername,
  73.             'error'         => $error,
  74.         ]);
  75.         // return $this->redirectToRoute('home');
  76.     }
  77.     /**
  78.      * @Route("/profile/{id}", name="profile")
  79.      * @param Request $request
  80.      * @param UserPasswordHasherInterface $encoder
  81.      * @param EntityManagerInterface $manager
  82.      * @param int $id
  83.      * @return RedirectResponse|Response
  84.      */
  85.     public function profile(
  86.         Request $request,
  87.         UserPasswordHasherInterface $encoder,
  88.         EntityManagerInterface $manager,
  89.         int $id = -1
  90.     ): RedirectResponse|Response {
  91.         $selfId false;
  92.         if ($id !== -&&
  93.             !$this->isGranted("ROLE_IT") &&
  94.             !$this->isGranted("ROLE_API") &&
  95.             !$this->isGranted("ROLE_MANAGER") &&
  96.             !$this->isGranted("ROLE_RH") &&
  97.             !$this->isGranted("ROLE_MG") &&
  98.             !$this->isGranted("ROLE_ADMIN")
  99.         ) {
  100.             return $this->redirectToRoute('home');
  101.         }
  102.         if ($id !== -1) {
  103.             /**
  104.              * @var Employee $employee
  105.              */
  106.             $employee $this->doctrine->getRepository(Employee::class)->findOneById($id);
  107.             /**
  108.              * @var User $user
  109.              */
  110.             $user $employee->getUser();
  111.         } else {
  112.             /**
  113.              * @var User $user
  114.              */
  115.             $user $this->getUser();
  116.             $employee $user->getEmployee();
  117.         }
  118.         /**
  119.          * @var User $userConnected
  120.          */
  121.         $userConnected $this->getUser();
  122.         if ($user != null && $user->getId() === $userConnected->getId()) {
  123.             $selfId true;
  124.         }
  125.         $form null;
  126.         if ($selfId === true) {
  127.             $form $this->createForm(UserPasswordType::class, $user);
  128.             $form->handleRequest($request);
  129.             if ($form->isSubmitted() && $form->isValid()) {
  130.                 $hash $encoder->hashPassword($user$user->getPassword());
  131.                 $user->setPassword($hash);
  132.                 $manager->persist($user);
  133.                 $manager->flush();
  134.                 return $this->redirectToRoute('security_ldap_login');
  135.             }
  136.         }
  137.         $renderArg = [
  138.             'user' => $user,
  139.             'employeeByUser' => $employee,
  140.             'percentTasks' => ($employee !== NULL $employee->getPercentTasks() : 0),
  141.             'form' => NULL
  142.         ];
  143.         if ($form) {
  144.             $renderArg['form'] = $form->createView();
  145.         }
  146. //        return $this->render('security/profile.html.twig', $renderArg);
  147.         return $this->redirectToRoute('employee', [
  148.             'id' => $employee->getId(),
  149.         ]);
  150.     }
  151.     /**
  152.      * @Route ("/profilepdf/{id}", name="profilepdf")
  153.      * @param EntityManagerInterface $manager
  154.      * @param Pdf $snappy
  155.      * @param int $id
  156.      * @return Response
  157.      */
  158.     public function profilepdf(EntityManagerInterface $managerPdf $snappyint $id = -1): Response
  159.     {
  160.         /**
  161.          * @var User $user
  162.          */
  163.         $user $this->getUser();
  164.         if ($id !== -1) {
  165.             /**
  166.              * @var Employee $employee
  167.              */
  168.             $employee $manager->getRepository(Employee::class)->findOneById($id);
  169.         } else {
  170.             /**
  171.              * @var Employee $employee
  172.              */
  173.             $employee $user->getEmployee();
  174.         }
  175.         $html $this->renderView('security/profilepdf.html.twig', [
  176.             'employee' => $employee,
  177.         ]);
  178.         $footer $this->renderView('security/footer-pdf.html.twig');
  179.         $snappy->setOption("enable-local-file-access"true);
  180.         $snappy->setOption('disable-javascript'true);
  181.         return new Response(
  182.             $snappy->getOutputFromHtml($html, array(
  183.                 'footer-html' => $footer
  184.             )),
  185.             200,
  186.                 [
  187.                     "Content-Type" => 'application/pdf',
  188.                     "Content-Disposition" => 'attachement; filename="' $employee->getName() . '_' $employee->getFirstname() . '.pdf'
  189.                 ]
  190.             );
  191.     }
  192.     /**
  193.      * @Route("/employeeByUser/image/edition", name="edit_employeeByUser_image")
  194.      * @param EntityManagerInterface $manager
  195.      * @return JsonResponse
  196.      */
  197.     public function ajaxImageEmployeeByUser(EntityManagerInterface $manager): JsonResponse
  198.     {
  199.         if (isset($_FILES['fileAjax'])) {
  200.             $finalName $_FILES['fileAjax']['name'];
  201.             $tmpname explode('.'$finalName);
  202.             $newName $tmpname[0] . '-' uniqid(''false) . '.' $tmpname[1];
  203.             $destination $this->getParameter('kernel.project_dir').'/public/uploads/employee/images/'.$newName;
  204.             move_uploaded_file($_FILES['fileAjax']['tmp_name'], $destination);
  205.             /**
  206.              * @var User $user
  207.              */
  208.             $user $this->getUser();
  209.             $employeeByUser $user->getEmployee();
  210.             if ($employeeByUser) {
  211.                 $employeeByUser->setImageFilename($finalName);
  212.                 $manager->persist($employeeByUser);
  213.                 $manager->flush();
  214.             }
  215.             return new JsonResponse($finalName);
  216.         }
  217.         return new JsonResponse();
  218.     }
  219.     /**
  220.      * @Route("/employeeByUser/image/editiontest/{id}", name="edit_employeeByUser_image_test")
  221.      * @param EntityManagerInterface $manager
  222.      * @param int $id
  223.      * @return JsonResponse
  224.      */
  225.     public function ajaxImageEmployeeTest(
  226.         EntityManagerInterface $manager,
  227.         int $id = -1
  228.     ): JsonResponse {
  229.         if (isset($_POST["image"])) {
  230.             $data $_POST["image"];
  231.             $image_array_1 explode(";"$data);
  232.             $image_array_2 explode(","$image_array_1[1]);
  233.             $extension explode('/'$image_array_1[0])[1];
  234.             $data base64_decode($image_array_2[1]);
  235.             $newName =  uniqid(''false) . '.' $extension;
  236.             $destination $this->getParameter('kernel.project_dir').'/public/uploads/employee/images/'.$newName;
  237.             file_put_contents($destination$data);
  238.             $employeeByUser $this->doctrine->getRepository(Employee::class)->findOneBy(
  239.                 ['id' => $id]
  240.             );
  241.             if ($employeeByUser) {
  242.                 $employeeByUser->setImageFilename($newName);
  243.                 $manager->flush();
  244.                 return new JsonResponse($newName);
  245.             }
  246.             return new JsonResponse("Error : Employee by user no exist.");
  247.         }
  248.         return new JsonResponse("ko");
  249.     }
  250.     /**
  251.      * @Route("/logout", name="security_logout")
  252.      * @Route("/ldap_auth/logout", name="security_ldap_logout")
  253.      */
  254.     public function logout(): void {
  255.         // throw new \Exception('Will be intercepted before getting here');
  256.     }
  257.     /**
  258.      * @Route("/ldap_auth/login", name="security_ldap_login")
  259.      * @param AuthenticationUtils $authenticationUtils
  260.      * @return Response
  261.      */
  262.     public function ldap_login(AuthenticationUtils $authenticationUtils): Response
  263.     {
  264.         // diable lap login only for preprod
  265.         if (strtoupper($this->getParameter('app.env')) === "PREPROD")
  266.             return $this->redirectToRoute('security_login');
  267.         // get the login error if there is one
  268.         $error $authenticationUtils->getLastAuthenticationError();
  269.         // last username entered by the user
  270.         $lastUsername $authenticationUtils->getLastUsername();
  271.         return $this->render('security/login_ldap.html.twig', [
  272.             'last_username' => $lastUsername,
  273.             'error'         => $error,
  274.         ]);
  275.         // return $this->redirectToRoute('home');
  276.     }
  277. }