【问题标题】:Symfony2, FOSuser : Edit user with empty input passwordSymfony,FOS 用户:使用空输入密码编辑用户
【发布时间】:2015-02-19 14:53:45
【问题描述】:

我正在使用我自己的扩展 fos_user 的实体。

我实际上在 2 个不同的表中扩展了用户实体(扩展表用户的投资者)。一些数据,如密码和电子邮件存储在用户中。投资者可以为此访问 Fos_user 方法。

我有一个填充了用户数据的表单。我需要能够使用或不使用密码来更新用户。

这是怎么做的:

if(!empty($form->get('password')->getData())){
    $investor->setPlainPassword($form->get('password')->getData());
}

除非密码输入为空,否则更新工作正常。

SQLSTATE[23000]:违反完整性约束:1048 列“密码”不能为空

这就是我在表单 $builder 中声明输入的方式:

->add('password', 'repeated',
            array(
                'type' => 'password',
                'invalid_message' => 'The password fields have to be the same',
                'required' => false,
                'first_options'  => array('label' => 'New password'),
                'second_options' => array('label' => 'Confirm the new password')
            )
        )

这是我的控制器:

public function updateInvestorAction(Request $request)
{
    $user = $this->container->get('security.context')->getToken()->getUser();

    $investor = $this->getDoctrine()->getRepository('AppBundle:Investor')->findOneBy(array('id' => $user->getId()));

    $form = $this->createForm(new UpdateInvestorType(), $investor);

    $form->handleRequest($request);

    if ($form->isValid()) {

        if(!empty($form->get('password')->getData())){
            $investor->setPlainPassword($form->get('password')->getData());
        }

        $em = $this->getDoctrine()->getManager();
        $em->persist($investor);
        $em->flush();

        $session = $this->getRequest()->getSession();
        $session->getFlashBag()->add('message', 'Votre profil a été correctement modifié');

        return $this->redirect($this->generateUrl('home'));

    }

    return array(
        'form' => $form->createView(),
    );
}

如何在不提供新密码或旧密码的情况下更新我的用户?

【问题讨论】:

  • Ether 在数据库和/或实体中,密码设置为 nullable = false,这就是为什么不设置密码就无法更新它的原因。要允许你想要的,你必须改变这种行为,但我认为这是一个真正的大安全漏洞
  • 我在 FOSUserBundle 中也找不到它,但它是在数据库中设置的,只需在您的用户表中处理它即可。这是我知道它如何在不提交密码的情况下工作的唯一方法。但是你真的不应该这样做,因为允许用户没有密码是非常危险的。事实上这是不可接受的,如果密码为空,只需登录即可劫持来自人们的数据
  • 但是FOS User Bundle有一个如何编辑用户的功能。也许你可以看看他们是如何做到的,并根据你的代码进行调整!
  • 是的,你是对的,但我实际上在 2 个不同的表中扩展了用户实体(扩展表用户的投资者)。一些数据,如密码和电子邮件存储在用户中。投资者可以为此访问 Fos 用户方法。因此,如果我更新 fos 用户,我仍然需要保留我的投资者数据,我会得到同样的错误。

标签: php symfony doctrine-orm fosuserbundle


【解决方案1】:

我终于找到了解决办法:

可以使用 setPassword() 设置 Unhashed/Unsalted 密码;

因此,如果密码为空,我将使用之前的哈希密码设置密码,这样就不会再次对其进行哈希处理。如果它不为空,我将使用 setPlainPassword() 方法。

这里是新控制器:

public function updateInvestorAction(Request $request)
{
    $user = $this->container->get('security.context')->getToken()->getUser();

    $investor = $this->getDoctrine()->getRepository('AppBundle:Investor')->findOneBy(array('id' => $user->getId()));

    $password = $investor->getPassword();

    $form = $this->createForm(new UpdateInvestorType(), $investor);

    $form->handleRequest($request);

    if ($form->isValid()) {

        if(!empty($form->get('password')->getData())){
            $investor->setPlainPassword($form->get('password')->getData());
        }
        else{
            $investor->setPassword($password);
        }

        $em = $this->getDoctrine()->getManager();
        $em->persist($investor);
        $em->flush();

        $session = $this->getRequest()->getSession();
        $session->getFlashBag()->add('message', 'Votre profil a été correctement modifié');

        return $this->redirect($this->generateUrl('home'));

    }

    return array(
        'form' => $form->createView(),
    );
}

【讨论】:

  • 是的,这很棒,但正如我所解释的那样。 “我实际上在 2 个不同的表中扩展了用户实体(扩展表用户的投资者)。密码和电子邮件等一些数据存储在用户中。投资者可以访问 Fos_user 方法。”因此,如果我更新 fos 用户,我仍然需要保留我的投资者数据,我会得到同样的错误。但我很确定用户管理器是在其他情况下要走的路。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-05-19
  • 2018-07-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多