src/Controller/SecurityController.php line 295
<?phpnamespace App\Controller;use App\Entity\Employee;use App\Entity\User;use App\Form\UserPasswordType;use Doctrine\ORM\EntityManagerInterface;use Doctrine\Persistence\ManagerRegistry;use Knp\Snappy\Pdf;use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;use Symfony\Component\HttpFoundation\JsonResponse;use Symfony\Component\HttpFoundation\RedirectResponse;use Symfony\Component\HttpFoundation\Request;use Symfony\Component\HttpFoundation\Response;use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;use Symfony\Component\Routing\Annotation\Route;use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;class SecurityController extends AbstractController{/*** @var ManagerRegistry $doctrine*/private ManagerRegistry $doctrine;/*** @param ManagerRegistry $doctrine*/public function __construct(ManagerRegistry $doctrine){$this->doctrine = $doctrine;}/*** @Route("/inscription", name="security_registration")* @param Request $request* @param EntityManagerInterface $manager* @param UserPasswordHasherInterface $encoder* @return Response*/public function registration(Request $request,EntityManagerInterface $manager,UserPasswordHasherInterface $encoder): Response {$user = new User();// $form = $this->createForm(RegistrationType::class, $user);// $form->handleRequest($request);// if ($form->isSubmitted() && $form->isValid()) {// $hash = $encoder->encodePassword($user, $user->getPassword());// $user->setPassword($hash);// $manager->persist($user);// $manager->flush();//// return $this->redirectToRoute('security_login');// }return $this->render('security/registration.html.twig', [// 'form' => $form->createView()]);}/*** @Route("/login", name="security_login")* @param AuthenticationUtils $authenticationUtils* @return Response*/public function login(AuthenticationUtils $authenticationUtils): Response{// diable local login only for prodif (strtoupper($this->getParameter('app.env')) === "PROD")return $this->redirectToRoute('security_ldap_login');// get the login error if there is one$error = $authenticationUtils->getLastAuthenticationError();// last username entered by the user$lastUsername = $authenticationUtils->getLastUsername();return $this->render('security/login.html.twig', ['last_username' => $lastUsername,'error' => $error,]);// return $this->redirectToRoute('home');}/*** @Route("/profile/{id}", name="profile")* @param Request $request* @param UserPasswordHasherInterface $encoder* @param EntityManagerInterface $manager* @param int $id* @return RedirectResponse|Response*/public function profile(Request $request,UserPasswordHasherInterface $encoder,EntityManagerInterface $manager,int $id = -1): RedirectResponse|Response {$selfId = false;if ($id !== -1 &&!$this->isGranted("ROLE_IT") &&!$this->isGranted("ROLE_API") &&!$this->isGranted("ROLE_MANAGER") &&!$this->isGranted("ROLE_RH") &&!$this->isGranted("ROLE_MG") &&!$this->isGranted("ROLE_ADMIN")) {return $this->redirectToRoute('home');}if ($id !== -1) {/*** @var Employee $employee*/$employee = $this->doctrine->getRepository(Employee::class)->findOneById($id);/*** @var User $user*/$user = $employee->getUser();} else {/*** @var User $user*/$user = $this->getUser();$employee = $user->getEmployee();}/*** @var User $userConnected*/$userConnected = $this->getUser();if ($user != null && $user->getId() === $userConnected->getId()) {$selfId = true;}$form = null;if ($selfId === true) {$form = $this->createForm(UserPasswordType::class, $user);$form->handleRequest($request);if ($form->isSubmitted() && $form->isValid()) {$hash = $encoder->hashPassword($user, $user->getPassword());$user->setPassword($hash);$manager->persist($user);$manager->flush();return $this->redirectToRoute('security_ldap_login');}}$renderArg = ['user' => $user,'employeeByUser' => $employee,'percentTasks' => ($employee !== NULL ? $employee->getPercentTasks() : 0),'form' => NULL];if ($form) {$renderArg['form'] = $form->createView();}// return $this->render('security/profile.html.twig', $renderArg);return $this->redirectToRoute('employee', ['id' => $employee->getId(),]);}/*** @Route ("/profilepdf/{id}", name="profilepdf")* @param EntityManagerInterface $manager* @param Pdf $snappy* @param int $id* @return Response*/public function profilepdf(EntityManagerInterface $manager, Pdf $snappy, int $id = -1): Response{/*** @var User $user*/$user = $this->getUser();if ($id !== -1) {/*** @var Employee $employee*/$employee = $manager->getRepository(Employee::class)->findOneById($id);} else {/*** @var Employee $employee*/$employee = $user->getEmployee();}$html = $this->renderView('security/profilepdf.html.twig', ['employee' => $employee,]);$footer = $this->renderView('security/footer-pdf.html.twig');$snappy->setOption("enable-local-file-access", true);$snappy->setOption('disable-javascript', true);return new Response($snappy->getOutputFromHtml($html, array('footer-html' => $footer)),200,["Content-Type" => 'application/pdf',"Content-Disposition" => 'attachement; filename="' . $employee->getName() . '_' . $employee->getFirstname() . '.pdf']);}/*** @Route("/employeeByUser/image/edition", name="edit_employeeByUser_image")* @param EntityManagerInterface $manager* @return JsonResponse*/public function ajaxImageEmployeeByUser(EntityManagerInterface $manager): JsonResponse{if (isset($_FILES['fileAjax'])) {$finalName = $_FILES['fileAjax']['name'];$tmpname = explode('.', $finalName);$newName = $tmpname[0] . '-' . uniqid('', false) . '.' . $tmpname[1];$destination = $this->getParameter('kernel.project_dir').'/public/uploads/employee/images/'.$newName;move_uploaded_file($_FILES['fileAjax']['tmp_name'], $destination);/*** @var User $user*/$user = $this->getUser();$employeeByUser = $user->getEmployee();if ($employeeByUser) {$employeeByUser->setImageFilename($finalName);$manager->persist($employeeByUser);$manager->flush();}return new JsonResponse($finalName);}return new JsonResponse();}/*** @Route("/employeeByUser/image/editiontest/{id}", name="edit_employeeByUser_image_test")* @param EntityManagerInterface $manager* @param int $id* @return JsonResponse*/public function ajaxImageEmployeeTest(EntityManagerInterface $manager,int $id = -1): JsonResponse {if (isset($_POST["image"])) {$data = $_POST["image"];$image_array_1 = explode(";", $data);$image_array_2 = explode(",", $image_array_1[1]);$extension = explode('/', $image_array_1[0])[1];$data = base64_decode($image_array_2[1]);$newName = uniqid('', false) . '.' . $extension;$destination = $this->getParameter('kernel.project_dir').'/public/uploads/employee/images/'.$newName;file_put_contents($destination, $data);$employeeByUser = $this->doctrine->getRepository(Employee::class)->findOneBy(['id' => $id]);if ($employeeByUser) {$employeeByUser->setImageFilename($newName);$manager->flush();return new JsonResponse($newName);}return new JsonResponse("Error : Employee by user no exist.");}return new JsonResponse("ko");}/*** @Route("/logout", name="security_logout")* @Route("/ldap_auth/logout", name="security_ldap_logout")*/public function logout(): void {// throw new \Exception('Will be intercepted before getting here');}/*** @Route("/ldap_auth/login", name="security_ldap_login")* @param AuthenticationUtils $authenticationUtils* @return Response*/public function ldap_login(AuthenticationUtils $authenticationUtils): Response{// diable lap login only for preprodif (strtoupper($this->getParameter('app.env')) === "PREPROD")return $this->redirectToRoute('security_login');// get the login error if there is one$error = $authenticationUtils->getLastAuthenticationError();// last username entered by the user$lastUsername = $authenticationUtils->getLastUsername();return $this->render('security/login_ldap.html.twig', ['last_username' => $lastUsername,'error' => $error,]);// return $this->redirectToRoute('home');}}