dppi5167 2015-02-19 14:53
浏览 13
已采纳

Symfony2,FOSuser:使用空输入密码编辑用户

I am using my own entity that extend fos_user.

I'm extending the User entity actually in 2 distinct tables (investor that extend the table user). Some data such as password and email are stored in user. Investor can access therefor to the Fos_user methods.

I have a form populated with the users's data. I need to be able to update the user with or without the password.

This is how it is done :

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

The update is perfectly working except if the password input is empty.

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'password' cannot be null

This is how i declare the input in my form $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')
            )
        )

And this is my Controller :

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(),
    );
}

How can i update my user without giving a new or the previous password?

  • 写回答

1条回答 默认 最新

  • duanliushua5026 2015-02-19 15:45
    关注

    I finally found a solution :

    It is possible to set Unhashed/Unsalted password with setPassword();

    Therefor if the password is empty i will set the password with the previous hashed password so it will not be hashed again. If it is not empty i will use setPlainPassword() method.

    Here the new controller :

    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(),
        );
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址