Estimados, junto con saludar escribo porque no entiendo que esta pasando. tengo el tipico formulario basico de cambio de clave, el problema se presenta cuando cambio por una clave distinta al nombre de usuario cuando esto pasa no ingresa al sistema, pero si ingreso la misma clave con el mismo nombre de usuario, me permite acceder al sistema:
usuario: Carlos clave: Carlos --> Ingresa
usuario: Carlos clave: 1234 ---> error
les dejo el codigo por si me pueden ayudar.
EL FORMULARIO
Código:
namespace scp\scpBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
/**
* Form
*
* @author Nombre Apellido <[email protected]>
*/
class CambiarpassType extends AbstractType
{
/**
* Construye form
*
* @param \Symfony\Component\Form\FormBuilderInterface $builder Builder del Form
* @param array $options Options
*
* @return form
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add( 'password','repeated',array(
'type' => 'password',
'invalid_message' => 'Los campos de contraseña deben coincidir.',
'options' => array('attr' => array('class' => 'password-field')),
'required' => true,
'first_options' => array('label' => 'Nueva clave'),
'second_options' => array('label' => 'Repetir clave'),))
->add(
'save', 'submit', array(
'translation_domain' => 'MWSimpleCrudGeneratorBundle',
'label' => 'views.new.save',
'attr' => array('class' => 'btn btn-success')
)
);
}
/**
* Set Defaults options
*
* @param \Symfony\Component\OptionsResolver\OptionsResolverInterface $resolver Resolver interface
*
* @return array
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(
array('data_class' => 'scp\scpBundle\Entity\User','csrf_protection' => false, )
);
}
/**
* Nombre
*
* @return string
*/
public function getName()
{
return 'scp_scpbundle_user';
}
}
EL Controller
Código PHP:
namespace scpscpBundleController;
use SymfonyBundleFrameworkBundleControllerController;
use SymfonyComponentSecurityCoreSecurityContext;
use SymfonyComponentSecurityCoreAuthenticationTokenUsernamePasswordToken;
use DoctrineORMEntityManager;
use SymfonyComponentHttpFoundationRequest;
use SymfonyComponentHttpFoundationResponse;
use SymfonyComponentSecurityCoreUtilSecureRandom;
use scpscpBundleEntityUser;
use scpscpBundleFormCambiarpassType;
use scpscpBundleFormUsuariosloginType;
class UsuarioController extends Controller
{
public function indexAction()
{
return $this->render('scpBundle:Usuario:index.html.twig');
}
public function loginAction()
{
if ($this->get('request')->attributes->has(SecurityContext::AUTHENTICATION_ERROR)) {
$error = $this->get('request')->attributes->get(SecurityContext::AUTHENTICATION_ERROR);
} else {
$error = $this->get('request')->getSession()->get(SecurityContext::AUTHENTICATION_ERROR);
}
return $this->render('scpBundle:Usuario:login.html.twig', array(
'last_username' => $this->get('request')->getSession()->get(SecurityContext::LAST_USERNAME),
'error' => $error,
));
}
public function denegadoAction()
{
return $this->render('scpBundle:Usuario:denegado.html.twig');
}
public function changepasswordAction()
{
$usuario = $this->get('security.context')->getToken()->getUser();
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('scpBundle:User')->find($usuario);
// $entity = new User();
$iduser = $entity->getId();
if (!$entity) {
throw $this->createNotFoundException('Unable to find User entity.');
}
$editForm = $this->createForm(new CambiarpassType(), $entity);
return $this->render('scpBundle:Usuario:changepassword.html.twig', array(
'entity' => $entity,
'edit_form' => $editForm->createView(),
'id' => $iduser,
// 'delete_form' => $deleteForm->createView(),
));
}
/**
* Edits an existing User entity.
*
*/
public function changepassword_updateAction(Request $request, $id)
{
$em = $this->getDoctrine()->getManager();
$generator = new SecureRandom();
$random = $generator->nextBytes(10);
$entity = $em->getRepository('scpBundle:User')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find User entity.');
}
// $deleteForm = $this->_createDeleteForm($id);
$editForm = $this->createForm(new CambiarpassType(), $entity);
$editForm->bind($request);
$pass= $editForm->get('password')->getData();
//var_dump($entity->getUsername());
// var_dump($pass);
if ($editForm->isValid()) {
// $entity->setUsername($usua);
// $entity->setEmail($email);
// $entity->setIsActive($acti);
// Codificamos el password
$factory = $this->get('security.encoder_factory');
$codificador = $factory->getEncoder($entity);
$entity->setSalt(md5($random));
// $salt = $codificador->encodePassword($pass, $entity->getSalt());
$password=$codificador->encodePassword($entity->setPassword($pass), $entity->getSalt());
$entity->setSalt($entity->getSalt());
$entity->setPassword($password);
$em->persist($entity);
$em->flush();
$this->get('session')->getFlashBag()->add('success', 'Clave modificada satisfactoriamente.');
return $this->redirect($this->generateUrl('portada'));
//return $this->generateUrl('portada');
}
$this->get('session')->getFlashBag()->add('error', 'flash.update.error');
return $this->render('scpBundle:Usuario:changepassword.html.twig', array(
'entity' => $entity,
'edit_form' => $editForm->createView(),
'id' => $id,
));
}
}//FIN CLASE
La VISTA
Código HTML:
{% extends "scpBundle::layout.html.twig" %}
{% block title %}Login{% endblock %}
{% block pageid 'usuarioscp' %}
{% block contenido %}
<h1>Accede a tu cuenta</h1>
{% if error %}
<div class="error" style="color:#FF0000">{{ error.message|trans({},'messages') }}</div>
{% endif %}
<form action="{{ path("login_check") }}" method="post" id="login">
<div>
<label for="username">Centro de costo</label>
<input type="text" id="username" name="_username" value="{{ last_username }}" />
</div>
<div>
<label for="password">Clave</label>
<input type="password" id="password" name="_password" />
</div>
<input type="submit" class="symfony-button-grey" value="Entrar" />
</form>
{% endblock %}
{% block lateral %}
{# En esta página no mostramos nada en el lateral #}
{% endblock %}