Exemplo n.º 1
0
 public function setUser()
 {
     $obj = new \AppBundle\Entity\Users();
     $obj->setUsername('Aldo Cervantes');
     $obj->setEmail('*****@*****.**');
     $encoder = $GLOBALS['kernel']->getContainer()->get('security.password_encoder');
     $obj->setPassword($encoder->encodePassword($obj, '2536'));
     $obj->setRoles('ROLE_SADMIN');
     $this->em->persist($obj);
     $this->em->flush();
     return;
 }
Exemplo n.º 2
0
 /**
  * @Route("gestionar-usuarios/", name="adGt-gestUsers", options={"expose":true})
  * @Method({"POST"})
  */
 public function gestUserAction(Request $req)
 {
     $em = $this->getDoctrine()->getManager();
     $getForm = TRUE;
     $passOld = '0';
     $saltOld = '0';
     if ($req->request->has('idu')) {
         $ax = $req->request->get('idu');
     } else {
         $ax = '0';
     }
     if ($ax !== '0') {
         //Opcion para Editar un Usuario de tipo admin
         $obj = $em->find('AppBundle:Users', (int) $ax);
         $passOld = $obj->getPassword();
         $saltOld = $obj->getSalt();
     } else {
         //Opcion para adicionar un nuevo Usuario de tipo admin
         $obj = new \AppBundle\Entity\Users();
     }
     $obj->setRoles('ROLE_ADMIN');
     $frm = $this->createForm(new \AppBundle\Form\Back\UsersType(), $obj);
     if ($req->request->has($frm->getName())) {
         $frmData = $req->request->get($frm->getName());
         //En caso de que no se desee cambiar la contraseña
         if ($frmData['password']['first'] === "") {
             $frmData['password']['first'] = 'passtmp';
             $frmData['password']['second'] = 'passtmp';
         }
         $frm->submit($frmData);
         if ($frm->isValid()) {
             $obj = $frm->getData();
             if ($obj->getPassword() == 'passtmp') {
                 $obj->setPassword($passOld);
                 $obj->setSalt($saltOld);
             } else {
                 $encoder = $this->container->get('security.password_encoder');
                 $obj->setPassword($encoder->encodePassword($obj, $obj->getPassword()));
             }
             //Comprobar si quiere cambiar la password
             $em->persist($obj);
             $em->flush();
             $getForm = FALSE;
         }
     }
     if ($getForm) {
         $respuesta = $this->renderView('::/back/ajax/addUser.html.twig', array('frm' => $frm->createView(), 'ax' => $ax));
     } else {
         $respuesta = 'ok';
     }
     return new \Symfony\Component\HttpFoundation\Response($respuesta, 200);
 }