From c32ebf6aee9c811ab52a6232727700a348d6a7cb Mon Sep 17 00:00:00 2001 From: Marat Salakhov Date: Tue, 12 Apr 2022 12:57:42 +0300 Subject: [PATCH] refactoring --- .../Controller/Admin/UserCrudController.php | 4 +- app/src/Controller/ClientController.php | 181 +++--------------- app/src/Controller/InsuranceController.php | 144 ++++++++++++++ app/src/Controller/MainController.php | 31 +++ app/src/Controller/RegistrationController.php | 12 +- .../Controller/ResetPasswordController.php | 19 +- .../Controller/UserAuthSecurityController.php | 6 +- .../client/insuranceObjects.html.twig | 2 +- 8 files changed, 214 insertions(+), 185 deletions(-) create mode 100644 app/src/Controller/InsuranceController.php create mode 100644 app/src/Controller/MainController.php diff --git a/app/src/Controller/Admin/UserCrudController.php b/app/src/Controller/Admin/UserCrudController.php index 954eff2..423e54a 100644 --- a/app/src/Controller/Admin/UserCrudController.php +++ b/app/src/Controller/Admin/UserCrudController.php @@ -18,9 +18,7 @@ class UserCrudController extends AbstractCrudController { - public function __construct(private UserRepository $userRepository) - { - } + public function __construct(private UserRepository $userRepository) {} public static function getEntityFqcn(): string { diff --git a/app/src/Controller/ClientController.php b/app/src/Controller/ClientController.php index f0f0342..6897b58 100755 --- a/app/src/Controller/ClientController.php +++ b/app/src/Controller/ClientController.php @@ -1,14 +1,14 @@ getRoles())) { - $clients = $clientRepository->findAll(); - return $this->render('admin/index.html.twig', [ - 'title' => 'Admin section', - 'clients' => $clients, - ]); - } else { - $clients = $clientRepository->findBy(['user' => $user->getId()]); - return $this->render('client/index.html.twig', [ - 'title' => 'Your clients', - 'clients' => $clients, - ]); - } - } - - #[Route('/client/create')] + #[Route('/create')] public function create(UserInterface $user, Request $request, ManagerRegistry $managerRegistry): Response { $client = new Client(); @@ -60,7 +43,7 @@ public function create(UserInterface $user, Request $request, ManagerRegistry $m try { $photo->move( - $this->getParameter('photoDirAbs'), + $this->getParameter('photoDirAbs'), $fileName ); } catch (FileException $e) { @@ -72,6 +55,7 @@ public function create(UserInterface $user, Request $request, ManagerRegistry $m $client->setPhoto($fileName); } + $client->setUser($user); $entityManager = $managerRegistry->getManager(); @@ -88,8 +72,8 @@ public function create(UserInterface $user, Request $request, ManagerRegistry $m ]); } - #[Route('/client/delete/{id}', methods:['DELETE'])] - public function delete($id, UserInterface $user, ClientRepository $clientRepository, ManagerRegistry $managerRegistry) + #[Route('/delete/{id}', methods: ['DELETE'])] + public function delete(int $id, UserInterface $user, ClientRepository $clientRepository, ManagerRegistry $managerRegistry) { $client = $clientRepository->find($id); @@ -107,8 +91,8 @@ public function delete($id, UserInterface $user, ClientRepository $clientReposit $response->send(); } - #[Route('/client/edit/{id}')] - public function edit(Request $request, $id, UserInterface $user, ClientRepository $clientRepository, ManagerRegistry $managerRegistry): Response + #[Route('/edit/{id}')] + public function edit(Request $request, int $id, UserInterface $user, ClientRepository $clientRepository, ManagerRegistry $managerRegistry): Response { $client = $clientRepository->find($id); @@ -122,7 +106,7 @@ public function edit(Request $request, $id, UserInterface $user, ClientRepositor $photo = new File($this->getParameter('photoDirAbs') . '/' . $client->getPhoto()); $fileName = $photo->getFilename(); - $client->setPhoto($photo); + $client->setPhoto((string)$photo); } $form = $this->createForm(CreateClientFormType::class, $client); @@ -135,7 +119,7 @@ public function edit(Request $request, $id, UserInterface $user, ClientRepositor try { $photo->move( - $this->getParameter('photoDirAbs'), + $this->getParameter('photoDirAbs'), $fileName ); } catch (FileException $e) { @@ -144,8 +128,6 @@ public function edit(Request $request, $id, UserInterface $user, ClientRepositor $imageOptimazer = new ImageOptimizer(); $imageOptimazer->resize($this->getParameter('photoDirAbs') . '/' . $fileName); - - } $client->setPhoto($fileName); @@ -163,8 +145,8 @@ public function edit(Request $request, $id, UserInterface $user, ClientRepositor ]); } - #[Route('/client/{id}', name: 'insuranceList')] - public function insuranceObjects($id, UserInterface $user, ClientRepository $clientRepository, ClientInsuranceRepository $clientInsuranceRepository, InsuranceAttachmentsRepository $insuranceAttachmentsRepository, AttachmentsRepository $attachmentsRepository) + #[Route('/{id}', name: 'insuranceList')] + public function insuranceObjects(int $id, UserInterface $user, ClientRepository $clientRepository, ClientInsuranceRepository $clientInsuranceRepository, InsuranceAttachmentsRepository $insuranceAttachmentsRepository, AttachmentsRepository $attachmentsRepository) { $client = $clientRepository->find($id); @@ -212,10 +194,11 @@ public function insuranceObjects($id, UserInterface $user, ClientRepository $cli ]); } - #[Route('/client/{id}/add-insurance', name: 'add-insurance')] - public function addInsurance($id, ClientRepository $clientRepository, UserInterface $user, Request $request, ManagerRegistry $managerRegistry) + #[Route('/{id}/add-insurance', name: 'add-insurance')] + public function addInsurance(int $id, ClientRepository $clientRepository, UserInterface $user, Request $request, ManagerRegistry $managerRegistry) { $client = $clientRepository->find($id); + if ($client->getUser()->getId() != $user->getId()) { if (!in_array('ADMIN', $user->getRoles())) { throw new AccessDeniedException(); @@ -244,131 +227,12 @@ public function addInsurance($id, ClientRepository $clientRepository, UserInterf ]); } - #[Route('/client/insurance/delete/{id}', name: 'delete-ins', methods:['DELETE'])] - public function deleteInsurance($id, ClientRepository $clientRepository, UserInterface $user, ClientInsuranceRepository $clientInsuranceRepository, ManagerRegistry $managerRegistry) - { - $clientInsurance = $clientInsuranceRepository->find($id); - - $client = $clientRepository->find($clientInsurance->getClientId()); - if ($client->getUser()->getId() != $user->getId()) { - if (!in_array('ADMIN', $user->getRoles())) { - throw new AccessDeniedException(); - } - } - - $entityManager = $managerRegistry->getManager(); - - $entityManager->remove($clientInsurance); - $entityManager->flush(); - - $response = new Response(); - $response->send(); - } - - #[Route('/client/insurance/edit/{id}', name: 'edit-ins')] - public function editInsurance(UserInterface $user, Request $request, $id, ClientRepository $clientRepository, ClientInsuranceRepository $clientInsuranceRepository, ManagerRegistry $managerRegistry): Response - { - $insuranse = $clientInsuranceRepository->find($id); - - $client = $clientRepository->find($insuranse->getClientId()); - if ($client->getUser()->getId() != $user->getId()) { - if (!in_array('ADMIN', $user->getRoles())) { - throw new AccessDeniedException(); - } - } - - $form = $this->createForm(CreateClientInsuranceFormType::class, $insuranse); - $form->handleRequest($request); - - if ($form->isSubmitted() && $form->isValid()) { - $entityManager = $managerRegistry->getManager(); - $entityManager->flush(); - - return $this->redirectToRoute('insuranceList', ['id' => $insuranse->getClientId()]); - } - - return $this->render('client/editInsurance.html.twig', [ - 'controller_name' => 'ClientController', - 'addInsuranceForm' => $form->createView(), - 'clientId' => $insuranse->getClientId() - ]); - } - - #[Route('/client/{id}/insurance/{insId}/upload-file', name: 'insurance-upload-file')] - public function uploadIns(Request $request, $id, $insId, ClientRepository $clientRepository, UserInterface $user, ManagerRegistry $managerRegistry): Response - { - $client = $clientRepository->find($id); - if ($client->getUser()->getId() != $user->getId()) { - if (!in_array('ADMIN', $user->getRoles())) { - throw new AccessDeniedException(); - } - } - - $attachment = new InsuranceAttachments(); - $form = $this->createForm(InsuranceAttachmentsFormType::class, $attachment); - $form->handleRequest($request); - - if ($form->isSubmitted() && $form->isValid()) { - /** @var UploadedFile $file */ - if ($file = $form->get('path')->getData()) { - $fileName = base64_encode($file->getClientOriginalName()); - $encodedFileName = bin2hex(random_bytes(6)) . '.' . $file->guessExtension(); - - try { - $file->move( - $this->getParameter('photoDirAbs'), - $encodedFileName - ); - } catch (FileException $e) { - //unable to upload - } - - $attachment->setPath($encodedFileName); - $attachment->setInsuranceId($insId); - $attachment->setName($fileName); - } - - $entityManager = $managerRegistry->getManager(); - $entityManager->persist($attachment); - $entityManager->flush(); - - return $this->redirectToRoute('insuranceList', ['id' => $id]); - } - - return $this->render('client/insurance-upload-file.html.twig', [ - 'controller_name' => 'ClientController', - 'attachmentForm' => $form->createView(), - 'title' => 'Attach insurance file' - ]); - } - - #[Route('/client/insurance/delete-attachment/{attachmentId}', name: 'delete-ins-attachment', methods:['DELETE'])] - public function deleteInsAttachment($attachmentId, ClientRepository $clientRepository, UserInterface $user, InsuranceAttachmentsRepository $insuranceAttachmentsRepository, ClientInsuranceRepository $clientInsuranceRepository, ManagerRegistry $managerRegistry) - { - $attachment = $insuranceAttachmentsRepository->find($attachmentId); - $insurance = $clientInsuranceRepository->find($attachment->getInsuranceId()); - - $client = $clientRepository->find($insurance->getClientId()); - if ($client->getUser()->getId() != $user->getId()) { - if (!in_array('ADMIN', $user->getRoles())) { - throw new AccessDeniedException(); - } - } - - $entityManager = $managerRegistry->getManager(); - $entityManager->remove($attachment); - $entityManager->flush(); - - $response = new Response(); - $response->send(); - } - - #[Route('/client/delete-attachment/{id}', name: 'delete-attachment', methods:['DELETE'])] - public function deleteAttachment($id, ClientRepository $clientRepository, UserInterface $user, AttachmentsRepository $attachmentsRepository, ManagerRegistry $managerRegistry) + #[Route('/delete-attachment/{id}', name: 'delete-attachment', methods: ['DELETE'])] + public function deleteAttachment(int $id, ClientRepository $clientRepository, UserInterface $user, AttachmentsRepository $attachmentsRepository, ManagerRegistry $managerRegistry) { $attachment = $attachmentsRepository->find($id); - $client = $clientRepository->find($attachment->getUserId()); + if ($client->getUser()->getId() != $user->getId()) { if (!in_array('ADMIN', $user->getRoles())) { throw new AccessDeniedException(); @@ -383,10 +247,11 @@ public function deleteAttachment($id, ClientRepository $clientRepository, UserIn $response->send(); } - #[Route('/client/{id}/upload-file', name: 'upload-file')] - public function upload(Request $request, $id, ClientRepository $clientRepository, UserInterface $user, ManagerRegistry $managerRegistry): Response + #[Route('/{id}/upload-file', name: 'upload-file')] + public function upload(Request $request, int $id, ClientRepository $clientRepository, UserInterface $user, ManagerRegistry $managerRegistry): Response { $client = $clientRepository->find($id); + if ($client->getUser()->getId() != $user->getId()) { if (!in_array('ADMIN', $user->getRoles())) { throw new AccessDeniedException(); diff --git a/app/src/Controller/InsuranceController.php b/app/src/Controller/InsuranceController.php new file mode 100644 index 0000000..e522cfb --- /dev/null +++ b/app/src/Controller/InsuranceController.php @@ -0,0 +1,144 @@ +find($insId); + $client = $clientRepository->find($clientInsurance->getClientId()); + + if ($client->getUser()->getId() != $user->getId()) { + if (!in_array('ADMIN', $user->getRoles())) { + throw new AccessDeniedException(); + } + } + + $entityManager = $managerRegistry->getManager(); + $entityManager->remove($clientInsurance); + $entityManager->flush(); + + $response = new Response(); + $response->send(); + } + + #[Route('/edit/{insId}', name: 'edit-ins')] + public function editInsurance(UserInterface $user, Request $request, int $insId, ClientRepository $clientRepository, ClientInsuranceRepository $clientInsuranceRepository, ManagerRegistry $managerRegistry): Response + { + $insuranse = $clientInsuranceRepository->find($insId); + $client = $clientRepository->find($insuranse->getClientId()); + + if ($client->getUser()->getId() != $user->getId()) { + if (!in_array('ADMIN', $user->getRoles())) { + throw new AccessDeniedException(); + } + } + + $form = $this->createForm(CreateClientInsuranceFormType::class, $insuranse); + $form->handleRequest($request); + + if ($form->isSubmitted() && $form->isValid()) { + $entityManager = $managerRegistry->getManager(); + $entityManager->flush(); + + return $this->redirectToRoute('insuranceList', ['id' => $insuranse->getClientId()]); + } + + return $this->render('client/editInsurance.html.twig', [ + 'controller_name' => 'ClientController', + 'addInsuranceForm' => $form->createView(), + 'clientId' => $insuranse->getClientId() + ]); + } + + #[Route('/{insId}/upload-file', name: 'insurance-upload-file')] + public function uploadIns(Request $request, int $insId, ClientRepository $clientRepository, UserInterface $user, ManagerRegistry $managerRegistry, ClientInsuranceRepository $clientInsuranceRepository): Response + { + $clientId = $clientInsuranceRepository->find($insId)->getClientId(); + $client = $clientRepository->find($clientId); + + if ($client->getUser()->getId() != $user->getId()) { + if (!in_array('ADMIN', $user->getRoles())) { + throw new AccessDeniedException(); + } + } + + $attachment = new InsuranceAttachments(); + $form = $this->createForm(InsuranceAttachmentsFormType::class, $attachment); + $form->handleRequest($request); + + if ($form->isSubmitted() && $form->isValid()) { + /** @var UploadedFile $file */ + if ($file = $form->get('path')->getData()) { + $fileName = base64_encode($file->getClientOriginalName()); + $encodedFileName = bin2hex(random_bytes(6)) . '.' . $file->guessExtension(); + + try { + $file->move( + $this->getParameter('photoDirAbs'), + $encodedFileName + ); + } catch (FileException $e) { + //unable to upload + } + + $attachment->setPath($encodedFileName); + $attachment->setInsuranceId($insId); + $attachment->setName($fileName); + } + + $entityManager = $managerRegistry->getManager(); + $entityManager->persist($attachment); + $entityManager->flush(); + + return $this->redirectToRoute('insuranceList', ['id' => $clientId]); + } + + return $this->render('client/insurance-upload-file.html.twig', [ + 'controller_name' => 'ClientController', + 'attachmentForm' => $form->createView(), + 'title' => 'Attach insurance file' + ]); + } + + #[Route('/delete-attachment/{attachmentId}', name: 'delete-ins-attachment', methods: ['DELETE'])] + public function deleteInsAttachment(int $attachmentId, ClientRepository $clientRepository, UserInterface $user, InsuranceAttachmentsRepository $insuranceAttachmentsRepository, ClientInsuranceRepository $clientInsuranceRepository, ManagerRegistry $managerRegistry) + { + $attachment = $insuranceAttachmentsRepository->find($attachmentId); + $insurance = $clientInsuranceRepository->find($attachment->getInsuranceId()); + $client = $clientRepository->find($insurance->getClientId()); + + if ($client->getUser()->getId() != $user->getId()) { + if (!in_array('ADMIN', $user->getRoles())) { + throw new AccessDeniedException(); + } + } + + $entityManager = $managerRegistry->getManager(); + $entityManager->remove($attachment); + $entityManager->flush(); + + $response = new Response(); + $response->send(); + } +} diff --git a/app/src/Controller/MainController.php b/app/src/Controller/MainController.php new file mode 100644 index 0000000..a2196fe --- /dev/null +++ b/app/src/Controller/MainController.php @@ -0,0 +1,31 @@ +getRoles())) { + $clients = $clientRepository->findAll(); + return $this->render('admin/index.html.twig', [ + 'title' => 'Admin section', + 'clients' => $clients, + ]); + } else { + $clients = $clientRepository->findBy(['user' => $user->getId()]); + return $this->render('client/index.html.twig', [ + 'title' => 'Your clients', + 'clients' => $clients, + ]); + } + } +} diff --git a/app/src/Controller/RegistrationController.php b/app/src/Controller/RegistrationController.php index ef5dae6..41991eb 100644 --- a/app/src/Controller/RegistrationController.php +++ b/app/src/Controller/RegistrationController.php @@ -5,6 +5,7 @@ use App\Entity\User; use App\Form\RegistrationFormType; use App\Security\EmailVerifier; +use Doctrine\Persistence\ManagerRegistry; use Symfony\Bridge\Twig\Mime\TemplatedEmail; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Request; @@ -16,15 +17,10 @@ class RegistrationController extends AbstractController { - private $emailVerifier; - - public function __construct(EmailVerifier $emailVerifier) - { - $this->emailVerifier = $emailVerifier; - } + public function __construct(private EmailVerifier $emailVerifier) {} #[Route('/register', name: 'app_register')] - public function register(Request $request, UserPasswordHasherInterface $passwordHasher, string $adminEmail): Response + public function register(Request $request, UserPasswordHasherInterface $passwordHasher, string $adminEmail, ManagerRegistry $managerRegistry): Response { $user = new User(); $form = $this->createForm(RegistrationFormType::class, $user); @@ -39,7 +35,7 @@ public function register(Request $request, UserPasswordHasherInterface $password ) ); - $entityManager = $this->getDoctrine()->getManager(); + $entityManager = $managerRegistry->getManager(); $entityManager->persist($user); $entityManager->flush(); diff --git a/app/src/Controller/ResetPasswordController.php b/app/src/Controller/ResetPasswordController.php index 7353256..1030844 100644 --- a/app/src/Controller/ResetPasswordController.php +++ b/app/src/Controller/ResetPasswordController.php @@ -5,6 +5,8 @@ use App\Entity\User; use App\Form\ChangePasswordFormType; use App\Form\ResetPasswordRequestFormType; +use App\Repository\UserRepository; +use Doctrine\Persistence\ManagerRegistry; use Symfony\Bridge\Twig\Mime\TemplatedEmail; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\RedirectResponse; @@ -23,14 +25,7 @@ class ResetPasswordController extends AbstractController { use ResetPasswordControllerTrait; - private $resetPasswordHelper; - private $adminEmail; - - public function __construct(ResetPasswordHelperInterface $resetPasswordHelper, string $adminEmail) - { - $this->resetPasswordHelper = $resetPasswordHelper; - $this->adminEmail = $adminEmail; - } + public function __construct(private ResetPasswordHelperInterface $resetPasswordHelper, private string $adminEmail) {} /** * Display & process form to request a password reset. @@ -75,7 +70,7 @@ public function checkEmail(): Response * Validates and process the reset URL that the user clicked in their email. */ #[Route('/reset/{token}', name: 'app_reset_password')] - public function reset(Request $request, UserPasswordHasherInterface $passwordHasher, string $token = null): Response + public function reset(Request $request, UserPasswordHasherInterface $passwordHasher, ?string $token, ManagerRegistry $managerRegistry): Response { if ($token) { // We store the token in session and remove it from the URL, to avoid the URL being @@ -116,7 +111,7 @@ public function reset(Request $request, UserPasswordHasherInterface $passwordHas ); $user->setPassword($encodedPassword); - $this->getDoctrine()->getManager()->flush(); + $managerRegistry->getManager()->flush(); // The session is cleaned up after the password has been changed. $this->cleanSessionAfterReset(); @@ -129,9 +124,9 @@ public function reset(Request $request, UserPasswordHasherInterface $passwordHas ]); } - private function processSendingPasswordResetEmail(string $emailFormData, MailerInterface $mailer): RedirectResponse + private function processSendingPasswordResetEmail(string $emailFormData, MailerInterface $mailer, UserRepository $userRepository): RedirectResponse { - $user = $this->getDoctrine()->getRepository(User::class)->findOneBy([ + $user = $userRepository->findOneBy([ 'email' => $emailFormData, ]); diff --git a/app/src/Controller/UserAuthSecurityController.php b/app/src/Controller/UserAuthSecurityController.php index b128325..8dde9bb 100644 --- a/app/src/Controller/UserAuthSecurityController.php +++ b/app/src/Controller/UserAuthSecurityController.php @@ -12,9 +12,9 @@ class UserAuthSecurityController extends AbstractController #[Route('/login', name: 'app_login')] public function login(AuthenticationUtils $authenticationUtils): Response { - // if ($this->getUser()) { - // return $this->redirectToRoute('target_path'); - // } + if ($this->getUser()) { + return $this->redirectToRoute('homepage'); + } // get the login error if there is one $error = $authenticationUtils->getLastAuthenticationError(); diff --git a/app/templates/client/insuranceObjects.html.twig b/app/templates/client/insuranceObjects.html.twig index 7eede40..49ed904 100755 --- a/app/templates/client/insuranceObjects.html.twig +++ b/app/templates/client/insuranceObjects.html.twig @@ -57,7 +57,7 @@

Year - {{ insObjInfo.year | date("Y") }}

Delete - Edit + Edit
{% if insObjInfo.name %}