src/Controller/User/UserController.php line 94

Open in your IDE?
  1. <?php
  2. namespace App\Controller\User ;
  3. use App\Entity\User;
  4. use App\Entity\Log;
  5. use App\Form\UserType;
  6. use App\Repository\EmailTemplateRepository;
  7. use App\Repository\UserRepository;
  8. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\HttpFoundation\Response;
  11. use Symfony\Component\Routing\Annotation\Route;
  12. use Knp\Component\Pager\PaginatorInterface;
  13. use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
  14. use Symfony\Component\Mailer\MailerInterface;
  15. use App\Services\MailerService;
  16. #[Route('/user')]
  17. class UserController extends AbstractController
  18. {
  19.     #[Route('/'name'user_index'methods: ['GET'])]
  20.     public function index(UserRepository $userRepositoryPaginatorInterface $paginatorRequest $request): Response
  21.     {
  22.         // $this->denyAccessUnlessGranted('user_index');
  23.         $users $paginator->paginate($userRepository->findBy([],['id'=>'desc']), $request->query->getInt('page'1), 10);
  24.         return $this->render('user_management/users/index.html.twig', [
  25.             'users' => $users,
  26.         ]);
  27.     }
  28.     #[Route('/new'name'user_new'methods: ['GET''POST'])]
  29.     public function new(Request $request,UserPasswordEncoderInterface $userPasswordEncoderInterfaceMailerInterface $mailerMailerService $mserviceEmailTemplateRepository $emailTemplateRepository): Response
  30.     {
  31.         $this->denyAccessUnlessGranted('user_create');
  32.         $user = new User();
  33.         $form $this->createForm(UserType::class, $user);
  34.         $form->handleRequest($request);
  35.         if ($form->isSubmitted() && $form->isValid()) {
  36.             $entityManager $this->getDoctrine()->getManager();
  37.             $user->setCreatedBy($this->getUser());
  38.             $user->setCreatedAt(new \DateTime());
  39.             $user->setIsActive(true);
  40.             $user->setStatus(1);
  41.             $user->setRoles(['ROLE_USER']);
  42.             $user->setUsername($request->request->get('user')["email"]);
  43.             $password $this->randomPassword();
  44.             $user->setPassword($userPasswordEncoderInterface->encodePassword($user,$password));
  45.             $entityManager->persist($user);
  46.             $entityManager->flush();
  47.             $template $emailTemplateRepository->findOneBy(['code' => 'user_account_created']);
  48.             $message =  $template->getContent();
  49.             $message str_replace('$user'$user->getFullName(), $message);
  50.             $message str_replace('$email'$user->getEmail(), $message);
  51.             $message str_replace('$password'$password$message);
  52.             $recievers = array();
  53.             array_push($recievers$user->getEmail());
  54.           
  55.             $resp $mservice->sendEmail($mailer$recievers$template->getName(), $message);
  56.          
  57.             $log = new Log();
  58.             $log =  $log->logEvent($request->getClientIp(),$this->getUser(),$user->getId(),"User","CREATE"$user);
  59.             $entityManager->persist($log);
  60.             $entityManager->flush();
  61.             $this->addFlash("success","created user successfully.");
  62.             return $this->redirectToRoute('user_index');
  63.         }
  64.         return $this->render('user_management/users/new.html.twig', [
  65.             'user' => $user,
  66.             'form' => $form->createView(),
  67.         ]);
  68.     }
  69.     private function randomPassword()
  70.     {
  71.         $alphabet 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnPpQqRrSsTtUuVvWwXxYyZz1234567890!@#$%^&*().?';
  72.         $pass = array();
  73.         $alphaLength strlen($alphabet) - 1;
  74.         for ($i 0$i 8$i++) {
  75.             $n rand(0$alphaLength);
  76.             $pass[] = $alphabet[$n];
  77.         }
  78.         $password implode($pass);
  79.         return $password
  80.     }
  81.     #[Route('/{id}'name'user_show'methods: ['GET'])]
  82.     public function show(User $user): Response
  83.     {
  84.         $this->denyAccessUnlessGranted('user_show');
  85.         return $this->render('user_management/users/show.html.twig', [
  86.             'user' => $user,
  87.         ]);
  88.     }
  89.     #[Route('/{id}/edit'name'user_edit'methods: ['GET''POST'])]
  90.     public function edit(Request $requestUser $user): Response
  91.     {
  92.         $this->denyAccessUnlessGranted('user_edit');
  93.         $form $this->createForm(UserType::class, $user);
  94.         $original = clone $user;
  95.         $form->handleRequest($request);
  96.         if ($form->isSubmitted() && $form->isValid()) {
  97.             $entityManager $this->getDoctrine()->getManager();
  98.             $log = new Log();
  99.             $log =  $log->logEvent($request->getClientIp(),$this->getUser(),$user->getId(),"User","UPDATE",$original$user);
  100.             $entityManager->persist($log);
  101.             $entityManager->flush();
  102.             $this->addFlash("success","Updated user successfully.");
  103.             return $this->redirectToRoute('user_index');
  104.         }
  105.         return $this->render('user_management/users/edit.html.twig', [
  106.             'user' => $user,
  107.             'form' => $form->createView(),
  108.         ]);
  109.     }
  110.     #[Route('/{id}'name'user_delete'methods: ['POST'])]
  111.     public function delete(Request $requestUser $user): Response
  112.     {
  113.         $this->denyAccessUnlessGranted('user_delete');
  114.         if ($this->isCsrfTokenValid('delete'.$user->getId(), $request->request->get('_token'))) {
  115.             $entityManager $this->getDoctrine()->getManager();
  116.             $entityManager->remove($user);
  117.             $log = new Log();
  118.             $log =  $log->logEvent($request->getClientIp(),$this->getUser(),$user->getId(),"User","DELETE"$user);
  119.             $entityManager->persist($log);
  120.             $entityManager->flush();
  121.         }
  122.         $this->addFlash("success","Deleted user successfully.");
  123.         return $this->redirectToRoute('user_index');
  124.     }
  125. }