src/Controller/DefaultController.php line 116

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  4. use App\Form\ContactType;
  5. use App\Model\User;
  6. use App\Entity\Auditoria;
  7. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
  8. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
  9. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  10. use App\Form\ChangePasswordType;
  11. use Symfony\Component\HttpFoundation\RedirectResponse;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  14. use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
  15. use Symfony\Component\Mailer\MailerInterface;
  16. use Symfony\Component\Mime\Email;
  17. use App\Model\EnvioEmail;
  18. class DefaultController extends AbstractController
  19. {
  20.  /**
  21.      * Lists all Usuarios entities.
  22.      *
  23.      * @Route("/inicio", name="inicio")
  24.      * @Template()
  25.      */
  26.     public function indexAction(Request $request)
  27.     {
  28.         $session $request->getSession();
  29.         $name=$session->get('nombreusuario');
  30.         return $this->render('Default/index.html.twig', array('usuario' => $name));
  31.     }
  32.     private function createCreateForm()
  33.     {
  34.         $form $this->createForm(ContactType::class, null, array(
  35.             'action' => $this->generateUrl('inventario_create'),
  36.             'method' => 'POST',
  37.         ));
  38. //        $form->add('submit', SubmitType::class, array('label' => 'Create'));
  39.         return $form;
  40.     }
  41.  /**
  42.      * Lists all Usuarios entities.
  43.      *
  44.      * @Route("/login", name="login")
  45.      * @Template()
  46.      */
  47.      public function loginAction(Request $request)
  48.     {
  49. //         $form = $this->get('form.factory')->create(new ContactType());
  50.          $form =  $this->createCreateForm();
  51. //        $request = $this->get('request');
  52.        
  53.         if ($request->getMethod() == 'POST') {
  54.             $form->handleRequest($request);
  55.             if ($form->isValid()) {
  56.                 $data $form->getData();
  57.                 $username $data['usuario'];
  58.                 $clave $data['clave'];
  59.                 $clave=md5($clave);
  60.                 $em $this->getDoctrine()->getManager();
  61.                 if (User::autenticar($username$clave,$em)) {
  62.                     $infoUser=User::getinfoUser($username,$em);
  63.                  
  64.                     $securityContext $this->container->get('security.token_storage');
  65.     
  66.                     $rol=User::getRolByUser($infoUser[0]->getTipousuarios()->getId(),$em);
  67.               
  68.                     $token = new UsernamePasswordToken($username$clave"secured_area", array($rol->getRol()));
  69.                     $securityContext->setToken($token);
  70.                     $session $request->getSession();
  71.                     $session->set('_security_secured_area'serialize($token));
  72.                     $session->set('userid'$infoUser[0]->getId());
  73.                     $session->set('username'$username);
  74.                     $session->set('atiendevip'$infoUser[0]->getAtiendevip());
  75.                     $session->set('nombreusuario'$infoUser[0]->getNombres());
  76.                     $auditoria  = new Auditoria();
  77.                     $user $this->container->get('security.token_storage')->getToken()->getUser();
  78.                     $auditoria->setUsuarios($infoUser[0]); 
  79.                     $auditoria->setEvento("login_valido");
  80.                     $fecha=date('Y-m-d H:i:s');
  81.                     $auditoria->setFechaevento(new \DateTime($fecha));
  82.                      $em2 $this->getDoctrine()->getManager();
  83.                      $em2->persist($auditoria);
  84.                      $em2->flush();
  85.                 } 
  86.                 else 
  87.                 {
  88.                     $this->get('session')->getFlashBag()->set('warning''Usuario o clave incorrecta!!!!');
  89.                     return new RedirectResponse($this->generateUrl('bufete_acceso_login'));
  90.                 }
  91.                 $esAfiliado $this->isGranted('ROLE_AFILIADO');
  92.                 if ($esAfiliado)
  93.                     return new RedirectResponse($this->generateUrl('cotasegurados_new'));
  94.                 else
  95.                 return new RedirectResponse($this->generateUrl('home'));
  96.             } 
  97.             else 
  98.             {
  99.                 $logger $this->get('logger');
  100.                 $logger->err('credenciales invalidas');
  101.                 echo "form invalido";
  102.             }
  103.         }
  104.         return $this->render('Default/login.html.twig', array(
  105.                     'form' => $form->createView(),
  106.                 ));
  107.     }
  108.          public function changePasswordAction(Request $request,MailerInterface $mailer)
  109.     {
  110.         $form $this->get('form.factory')->create(ChangePasswordType::class);
  111. //        $request = $this->get('request');
  112.         $session $request-> getSession();
  113.         if ($request->getMethod() == 'POST') {
  114.             $form->handleRequest($request);
  115.             if ($form->isValid()) {
  116.                 $data $form->getData();
  117.                 $claveactual $data['claveactual'];
  118.                 $clavenueva $data['clavenueva'];
  119.                 $repitaclavenueva $data['repitaclavenueva'];
  120.                 $em $this->getDoctrine()->getManager();
  121.                 $usuario=User::buscaruserbyuserid($session->get('userid'),$em);
  122.                 if ($usuario
  123.                 {
  124.                         if(trim($clavenueva)==trim($repitaclavenueva))
  125.                         {
  126.                             if($clave=md5(trim($claveactual))==$usuario->getClave())
  127.                             {
  128.                                 $cambio=User::Cambiarclave($usuario,trim($clavenueva),$em);
  129.                                 if ($usuario->getEmail()!="")
  130.                                 {
  131.                                     $asunto="CSL: Cambio de contraseña de acceso. !";
  132.                                     $mensaje="Su contraseña fue actualizada de forma corracta, si usted no realizó este cambio por favor comuniquese con nuestros operadores.";
  133.                                     EnvioEmail::EnviarEmail($usuario->getEmail(),$asunto,$mensaje,$mailer);
  134.                                 }
  135.                                 $this->get('session')->getFlashBag()->add('success''La clave fue cambiada correctamente');
  136.                             }
  137.                             else
  138.                             {
  139.                                $this->get('session')->getFlashBag()->add('warning''La clave actual que ingreso no coincide con la registrada en el sistema');
  140.                             }
  141.                         }
  142.                         else
  143.                         {
  144.                            $this->get('session')->getFlashBag()->add('warning''La clave nueva debe coincidir con el campo repita clave nueva'); 
  145.                         }
  146.                         
  147.                 } 
  148.                 else 
  149.                 {
  150.                     $this->get('session')->getFlashBag()->add('error''No se encontro la data del usuario autenticado intente autenticarse de nuevo');
  151.                 }
  152.             } 
  153.         }
  154.         return $this->render('Default/changepassword.html.twig', array(
  155.                     'form' => $form->createView(),
  156.                 ));
  157.     }
  158. //    public function logoutAction(Request $request)
  159. //    {
  160. //        $session=$request->getSession();
  161. //        $session->
  162. //    }
  163. }