src/Controller/UserController.php line 26

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. /*
  4.  * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
  5.  * Click nbfs://nbhost/SystemFileSystem/Templates/Scripting/PHPClass.php to edit this template
  6.  */
  7. use App\Entity\Users;
  8. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  9. use Symfony\Component\HttpFoundation\JsonResponse;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\HttpFoundation\Response;
  12. use Symfony\Component\HttpFoundation\Session\Session;
  13. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  14. use App\Form\UsersType;
  15. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  16. use Doctrine\Persistence\ManagerRegistry as PersistenceManagerRegistry;
  17. use Knp\Component\Pager\PaginatorInterface;
  18. /**
  19.  * Description of UserController
  20.  *
  21.  * @author inkubes
  22.  */
  23. class UserController extends AbstractController {
  24.     
  25.     private $em null;
  26.     public function __construct(  PersistenceManagerRegistry $doctrine){
  27.         $this->session = new Session();
  28.         $this->em $doctrine->getManager();
  29.     }
  30.     
  31.     public function login(AuthenticationUtils $authenticationUtils){
  32.         if(is_object($this->getUser()))
  33.             return $this->redirectToRoute("analytics");
  34.         // get the login error if there is one
  35.         $error $authenticationUtils->getLastAuthenticationError();
  36.         // last username entered by the user
  37.         $lastUsername $authenticationUtils->getLastUsername();
  38.         return $this->render('auth/login.html.twig',array(
  39.             'error'=>$error,
  40.             'last_username'=>$lastUsername,
  41.             'title'=>'Entrar'
  42.         ));
  43.     }
  44.     
  45.     public function listUsers(
  46.             Request $request
  47.             PaginatorInterface $paginator,
  48.             UserPasswordHasherInterface $encoder
  49.             ) {
  50.          if (!$this->getUser() || !is_object($this->getUser())) {         
  51.             return $this->redirectToRoute('logout');
  52.         }
  53.         $users $this->em->createQueryBuilder()->select("u")
  54.                 ->from("App\Entity\Users","u")
  55.                 ->where("u.role = 'ADMIN' OR u.role = 'ACCOUNT_MANAGER'")
  56.                 ->getQuery()
  57.                 ->getResult();
  58.         
  59.         $usuarios $paginator->paginate(
  60.                 $users$request->query->getInt('page'1), 20);
  61.         
  62.         $user_new = new Users();
  63.         $form $this->createForm(UsersType::class,$user_new);
  64.         
  65.         $form->handleRequest($request);
  66.         
  67.         if($form->isSubmitted()){
  68.             $user_new->setRole('ADMIN');
  69.            
  70.             $user_new->setPassword($encoder->hashPassword($user_new,$user_new->getPassword()));
  71.             $user_new->setDatecreated(new \DateTime());
  72.             $this->em->persist($user_new);
  73.             $flush $this->em->flush();
  74.             if($flush == null){
  75.                 $status "User added correctly";
  76.                 $type 1;
  77.             }else{
  78.                 $status "Error on add user";
  79.                 $type=0;
  80.             }
  81.             $this->session->getFlashBag()->add("status",$status);
  82.             $this->session->getFlashBag()->add("type",$type);
  83.             return $this->redirectToRoute("users");
  84.         }
  85.         
  86.         return $this->render('user/list.html.twig',array(
  87.             'title'=>'Users',
  88.             'users'=>$usuarios,
  89.             'form'=>$form->createView()
  90.         ));
  91.     }
  92.     
  93.     public function editUser(Request $request) {
  94.          if (!$this->getUser() || !is_object($this->getUser())) {         
  95.             return $this->redirectToRoute('logout');
  96.         }
  97.         $id $request->get("id");
  98.         if($id){
  99.             $user $this->em->getRepository(Users::class)->find($id);
  100.             $form $this->createForm(UsersType::class,$user,array(
  101.                 'password'=>false
  102.             ));
  103.             $form->handleRequest($request);
  104.             if($form->isSubmitted()){
  105.                 
  106.                 
  107.                 $this->em->persist($user);
  108.                 $flush $this->em->flush();
  109.                 if($flush == null){
  110.                     $status "User edited correctly";
  111.                     $type 1;
  112.                 }else{
  113.                     $status "Error on edit user";
  114.                     $type=0;
  115.                 }
  116.                 $this->session->getFlashBag()->add("status",$status);
  117.                 $this->session->getFlashBag()->add("type",$type);
  118.                 return $this->redirectToRoute("users");
  119.                 
  120.             }
  121.         }else{
  122.             $status "User not found";
  123.             $type=0;
  124.             $this->session->getFlashBag()->add("status",$status);
  125.             $this->session->getFlashBag()->add("type",$type);
  126.             return $this->redirectToRoute("users");
  127.         }
  128.         return $this->render('user/edit.html.twig',array(
  129.             'user'=>$id,
  130.             'form'=>$form->createView()
  131.         ));
  132.     }
  133.     
  134.     public function editPass(Request $requestUserPasswordHasherInterface $encoder){
  135.          if (!$this->getUser() || !is_object($this->getUser())) {         
  136.             return $this->redirectToRoute('logout');
  137.         }
  138.         $id $request->get("id");
  139.         if($id){
  140.             $user $this->em->getRepository(Users::class)->find($id);
  141.             $form $this->createForm(UsersType::class,$user,array(
  142.                 'password'=>true,
  143.                 'datos'=>false
  144.             ));
  145.             $form->handleRequest($request);
  146.             if($form->isSubmitted()){
  147.                 $user->setPassword($encoder->hashPassword($user,$user->getPassword()));
  148.                 $this->em->persist($user);
  149.                 $flush $this->em->flush();
  150.                 if($flush == null){
  151.                     $status "User password edited correctly";
  152.                     $type 1;
  153.                 }else{
  154.                     $status "Error on edit user";
  155.                     $type=0;
  156.                 }
  157.                 $this->session->getFlashBag()->add("status",$status);
  158.                 $this->session->getFlashBag()->add("type",$type);
  159.                 return $this->redirectToRoute("users");
  160.                 
  161.             }
  162.         }else{
  163.             $status "User not found";
  164.             $type=0;
  165.             $this->session->getFlashBag()->add("status",$status);
  166.             $this->session->getFlashBag()->add("type",$type);
  167.             return $this->redirectToRoute("users");
  168.         }
  169.         return $this->render('user/editPass.html.twig',array(
  170.             'user'=>$id,
  171.             'form'=>$form->createView()
  172.         ));
  173.     }
  174.     
  175.     public function deleteUser(Request $request){
  176.          if (!$this->getUser() || !is_object($this->getUser())) {         
  177.             return $this->redirectToRoute('logout');
  178.         }
  179.         $id $request->get("id");
  180.         if($id){
  181.             $user $this->em->getRepository(Users::class)->find($id);
  182.             $this->em->remove($user);
  183.             $f $this->em->flush();
  184.             if($f == null){
  185.                 $status "User removed correctly";
  186.                 $type 1;
  187.             }else{
  188.                 $status "Error on delete user";
  189.                 $type=0;
  190.             }
  191.         }else{
  192.             $status "User not found";
  193.             $type=0;
  194.         }
  195.         $this->session->getFlashBag()->add("status",$status);
  196.         $this->session->getFlashBag()->add("type",$type);
  197.         return $this->redirectToRoute("users");
  198.     }
  199. }