【问题标题】:FOS user update password not working in rare caseFOS 用户更新密码在极少数情况下不起作用
【发布时间】:2016-04-04 12:12:08
【问题描述】:

我正在使用 symfony2 CRUD 生成器来管理用户。当我更新数据库中未更新的用户密码时,会发生非常罕见的事件。但是当我使用任何其他字段(例如姓名或电子邮件)更新用户密码时,密码就会更新。

下面是我的 UserType Form 类

class UserType extends AbstractType
{
/**
 * @param FormBuilderInterface $builder
 * @param array $options
 */
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('name')
        ->add('email')
        ->add('plainPassword','password')
        ->add('roles','choice',array('choices'=>array('ROLE_ADMIN'=>'Admin','ROLE_MEMBER'=>'Member') ,'multiple'  => true,'expanded'=>true))
        ->add('path','hidden')
        ->add('file');
    ;
}

/**
 * @param OptionsResolverInterface $resolver
 */
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => 'Ars\PlaybookBundle\Entity\User'
    ));
}

/**
 * @return string
 */
public function getName()
{
    return 'ars_playbookbundle_user';
}
}

我的 crud 控制器更新操作看起来像

/**
 * Edits an existing User entity.
 *
 * @Route("/{id}", name="user_update")
 * @Method("PUT")
 * @Template("ArsPlaybookBundle:User:edit.html.twig")
 */
public function updateAction(Request $request, $id) {
    $em = $this->getDoctrine()->getManager();

    $entity = $em->getRepository('ArsPlaybookBundle:User')->find($id);

    if (!$entity) {
        throw $this->createNotFoundException('Unable to find User entity.');
    }

    $deleteForm = $this->createDeleteForm($id);
    $editForm = $this->createEditForm($entity);


   $email=$request->request->all();
   $password   =     $email['ars_playbookbundle_user']['plainPassword'];


   if(isset($password)){
      $editForm->get('plainPassword')->setData($password); 
   }



    $editForm->handleRequest($request);

    //\Doctrine\Common\Util\Debug::dump($editForm->getData()); die;
    if ($editForm->isValid()) {

        $entity->upload();
        $em->flush();
        $this->get('session')->getFlashBag()->add(
                'notice', 'Your changes were saved!'
        );
        return $this->redirect($this->generateUrl('user_edit', array('id' => $id)));
    }

    return array(
        'entity' => $entity,
        'edit_form' => $editForm->createView(),
        'delete_form' => $deleteForm->createView(),
    );
}

【问题讨论】:

    标签: symfony fosuserbundle


    【解决方案1】:

    最后,我通过使用用户管理器而不是表单或实体管理器来实现它。现在我可以只更新密码字段而不更改任何其他字段。

    【讨论】:

      猜你喜欢
      • 2023-03-03
      • 2019-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-06
      相关资源
      最近更新 更多