<?php
namespace App\Controller;
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Scripting/PHPClass.php to edit this template
*/
use App\Entity\Users;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
use App\Form\UsersType;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Doctrine\Persistence\ManagerRegistry as PersistenceManagerRegistry;
use Knp\Component\Pager\PaginatorInterface;
/**
* Description of UserController
*
* @author inkubes
*/
class UserController extends AbstractController {
private $em = null;
public function __construct( PersistenceManagerRegistry $doctrine){
$this->session = new Session();
$this->em = $doctrine->getManager();
}
public function login(AuthenticationUtils $authenticationUtils){
if(is_object($this->getUser()))
return $this->redirectToRoute("analytics");
// get the login error if there is one
$error = $authenticationUtils->getLastAuthenticationError();
// last username entered by the user
$lastUsername = $authenticationUtils->getLastUsername();
return $this->render('auth/login.html.twig',array(
'error'=>$error,
'last_username'=>$lastUsername,
'title'=>'Entrar'
));
}
public function listUsers(
Request $request,
PaginatorInterface $paginator,
UserPasswordHasherInterface $encoder
) {
if (!$this->getUser() || !is_object($this->getUser())) {
return $this->redirectToRoute('logout');
}
$users = $this->em->createQueryBuilder()->select("u")
->from("App\Entity\Users","u")
->where("u.role = 'ADMIN' OR u.role = 'ACCOUNT_MANAGER'")
->getQuery()
->getResult();
$usuarios = $paginator->paginate(
$users, $request->query->getInt('page', 1), 20);
$user_new = new Users();
$form = $this->createForm(UsersType::class,$user_new);
$form->handleRequest($request);
if($form->isSubmitted()){
$user_new->setRole('ADMIN');
$user_new->setPassword($encoder->hashPassword($user_new,$user_new->getPassword()));
$user_new->setDatecreated(new \DateTime());
$this->em->persist($user_new);
$flush = $this->em->flush();
if($flush == null){
$status = "User added correctly";
$type = 1;
}else{
$status = "Error on add user";
$type=0;
}
$this->session->getFlashBag()->add("status",$status);
$this->session->getFlashBag()->add("type",$type);
return $this->redirectToRoute("users");
}
return $this->render('user/list.html.twig',array(
'title'=>'Users',
'users'=>$usuarios,
'form'=>$form->createView()
));
}
public function editUser(Request $request) {
if (!$this->getUser() || !is_object($this->getUser())) {
return $this->redirectToRoute('logout');
}
$id = $request->get("id");
if($id){
$user = $this->em->getRepository(Users::class)->find($id);
$form = $this->createForm(UsersType::class,$user,array(
'password'=>false
));
$form->handleRequest($request);
if($form->isSubmitted()){
$this->em->persist($user);
$flush = $this->em->flush();
if($flush == null){
$status = "User edited correctly";
$type = 1;
}else{
$status = "Error on edit user";
$type=0;
}
$this->session->getFlashBag()->add("status",$status);
$this->session->getFlashBag()->add("type",$type);
return $this->redirectToRoute("users");
}
}else{
$status = "User not found";
$type=0;
$this->session->getFlashBag()->add("status",$status);
$this->session->getFlashBag()->add("type",$type);
return $this->redirectToRoute("users");
}
return $this->render('user/edit.html.twig',array(
'user'=>$id,
'form'=>$form->createView()
));
}
public function editPass(Request $request, UserPasswordHasherInterface $encoder){
if (!$this->getUser() || !is_object($this->getUser())) {
return $this->redirectToRoute('logout');
}
$id = $request->get("id");
if($id){
$user = $this->em->getRepository(Users::class)->find($id);
$form = $this->createForm(UsersType::class,$user,array(
'password'=>true,
'datos'=>false
));
$form->handleRequest($request);
if($form->isSubmitted()){
$user->setPassword($encoder->hashPassword($user,$user->getPassword()));
$this->em->persist($user);
$flush = $this->em->flush();
if($flush == null){
$status = "User password edited correctly";
$type = 1;
}else{
$status = "Error on edit user";
$type=0;
}
$this->session->getFlashBag()->add("status",$status);
$this->session->getFlashBag()->add("type",$type);
return $this->redirectToRoute("users");
}
}else{
$status = "User not found";
$type=0;
$this->session->getFlashBag()->add("status",$status);
$this->session->getFlashBag()->add("type",$type);
return $this->redirectToRoute("users");
}
return $this->render('user/editPass.html.twig',array(
'user'=>$id,
'form'=>$form->createView()
));
}
public function deleteUser(Request $request){
if (!$this->getUser() || !is_object($this->getUser())) {
return $this->redirectToRoute('logout');
}
$id = $request->get("id");
if($id){
$user = $this->em->getRepository(Users::class)->find($id);
$this->em->remove($user);
$f = $this->em->flush();
if($f == null){
$status = "User removed correctly";
$type = 1;
}else{
$status = "Error on delete user";
$type=0;
}
}else{
$status = "User not found";
$type=0;
}
$this->session->getFlashBag()->add("status",$status);
$this->session->getFlashBag()->add("type",$type);
return $this->redirectToRoute("users");
}
}