src/Controller/AdminController.php line 138

  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\User;
  4. use App\Form\RegistrationFormType;
  5. use App\Form\UsersFormType;
  6. use App\Security\EmailVerifier;
  7. use DateTime;
  8. use Doctrine\ORM\EntityManagerInterface;
  9. use Doctrine\Persistence\ManagerRegistry;
  10. use Exception;
  11. use Symfony\Bridge\Twig\Mime\TemplatedEmail;
  12. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  13. use Symfony\Component\HttpFoundation\JsonResponse;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Symfony\Component\HttpFoundation\Response;
  16. use Symfony\Component\Mime\Address;
  17. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  18. use Symfony\Component\Routing\Annotation\Route;
  19. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  20. use Symfony\Component\Serializer\Encoder\JsonEncoder;
  21. use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
  22. use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer;
  23. use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
  24. use Symfony\Component\Serializer\Serializer;
  25. use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
  26. use Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface;
  27. use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
  28. use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
  29. use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
  30. use Symfony\Contracts\HttpClient\HttpClientInterface;
  31. #[Route('/admin')]
  32. class AdminController extends AbstractController
  33. {
  34.     public function __construct(private readonly ManagerRegistry $doctrine, private EmailVerifier $emailVerifier) {}
  35.     
  36.     #[Route(path'/agencyClients'name'app_admin_agencyClients_index')]
  37.     public function agencyClients_index(Request $request): Response
  38.     {
  39.         return $this->render('agency_clients/table.html.twig');
  40.     }
  41.     
  42.     #[Route(path'/users'name'app_admin_users_index')]
  43.     public function users_index(): Response
  44.     {
  45.         return $this->render('users_list/users.html.twig');
  46.     }
  47.     
  48.     #[Route(path'/forms'name'app_admin_forms')]
  49.     public function forms_index(): Response
  50.     {
  51.         return $this->render('form_elements/index.html.twig');
  52.     }
  53.     
  54.     #[Route(path'/users/getUsers'name'api_users_getUsers')]
  55.     public function getUsers(): Response
  56.     {
  57.         $user $this->getUser();
  58.         if ($user->isRoot()) {
  59.             $users $this->doctrine->getRepository(User::class)->findAll();
  60.         } else if ($user->isAdmin()) {
  61.             $users $this->doctrine->getRepository(User::class)->findBy(['adminId' => $user->getId()]);
  62.         }
  63.         
  64.         return $this->json([
  65.             "count" => count($users),
  66.             "data" => $users
  67.         ]);
  68.     }
  69.     
  70.     #[Route('/users/{user}'name'app_admin_edit_user')]
  71.     public function usersEdit(Request $requestUserPasswordHasherInterface $userPasswordHasherUser $user): Response
  72.     {
  73.         $admins = [];
  74.         $users $this->doctrine->getRepository(User::class)->findAll();
  75.         
  76.         foreach ($users as $admin) {
  77.             if ($admin->isAdmin()) $admins[$admin->getEmail()] = $admin->getId();
  78.         }
  79.         
  80.         $currentUser $this->getUser();
  81.         $isRoot false;
  82.         if ($currentUser instanceof User) {
  83.             $isRoot $currentUser->isRoot();
  84.         }
  85.         
  86.         $form $this->createForm(UsersFormType::class, $user, [
  87.             'admins' => $admins,
  88.             'isRoot' => $isRoot,
  89.         ]);
  90.         
  91.         $form->handleRequest($request);
  92.         
  93.         if ($form->isSubmitted()) {
  94.             if ($form->get('plainPassword')->getData()) {
  95.                 $user->setPassword(
  96.                     $userPasswordHasher->hashPassword(
  97.                         $user,
  98.                         $form->get('plainPassword')->getData()
  99.                     )
  100.                 );
  101.             }
  102.             
  103.             $this->doctrine->getManager()->persist($user);
  104.             $this->doctrine->getManager()->flush();
  105.             
  106.             return $this->redirectToRoute('app_admin_users_index');
  107.         }
  108.         return $this->render('users_list/index.html.twig', [
  109.             'form' => $form->createView(),
  110.             'user' => $user,
  111.             'createUser' => false,
  112.         ]);
  113.     }
  114.     
  115.     #[Route('/users/{user}/delete'name'app_admin_delete_user')]
  116.     public function usersDelete(Request $requestUser $user): Response
  117.     {
  118.         $this->doctrine->getManager()->remove($user);
  119.         $this->doctrine->getManager()->flush();
  120.         
  121.         return $this->redirectToRoute('app_admin_users_index');
  122.     }
  123.     
  124.     #[Route('/new/user'name'app_admin_create_user')]
  125.     public function usersCreate(Request $requestUserPasswordHasherInterface $userPasswordHasherEntityManagerInterface $entityManager): Response
  126.     {
  127.         $admin $this->getUser();
  128.         
  129.         $user = new User();
  130.         
  131.         $admins = [];
  132.         $admins['-'] = null;
  133.         
  134.         if ($admin->isAdmin()) {
  135.             $admins[$admin->getEmail()] = $admin->getId();
  136.         } else {
  137.             $users $this->doctrine->getRepository(User::class)->findAll();
  138.             
  139.             foreach ($users as $admin) {
  140.                 if ($admin->isAdmin()) $admins[$admin->getEmail()] = $admin->getId();
  141.             }
  142.         }
  143.         
  144.         $form $this->createForm(UsersFormType::class, $user, [
  145.             'admins' => $admins
  146.         ]);
  147.         $form->handleRequest($request);
  148.         
  149.         if ($form->isSubmitted() && $form->isValid()) {
  150.             $user->setPassword(
  151.                 $userPasswordHasher->hashPassword(
  152.                     $user,
  153.                     $form->get('plainPassword')->getData()
  154.                 )
  155.             );
  156.             
  157.             $user->setCreatedAt(new DateTime());
  158.             
  159.             $entityManager->persist($user);
  160.             $entityManager->flush();
  161.             
  162.             return $this->redirectToRoute('app_admin_users_index');
  163.         }
  164.         
  165.         return $this->render('users_list/index.html.twig', [
  166.             'form' => $form->createView(),
  167.             'user' => $user,
  168.             'createUser' => true,
  169.             'admins' => $admins
  170.         ]);
  171.     }
  172. }