Implement change password in Symfony2

后端 未结 5 1239
孤城傲影
孤城傲影 2020-12-07 17:45

What is the best way to implement change password functionality in Symfony2? Right now I\'m using this:

$builder->add(\'password\', \'repeated\', array(
         


        
5条回答
  •  太阳男子
    2020-12-07 18:33

    I use a action from my controller:

    public function changepasswordAction(Request $request) {
        $session = $request->getSession();
    
        if($request->getMethod() == 'POST') {
            $old_pwd = $request->get('old_password');
            $new_pwd = $request->get('new_password');
            $user = $this->getUser();
            $encoder = $this->container->get('security.encoder_factory')->getEncoder($user);
            $old_pwd_encoded = $encoder->encodePassword($old_pwd, $user->getSalt());
    
            if($user->getPassword() != $old_pwd_encoded) {
                $session->getFlashBag()->set('error_msg', "Wrong old password!");
            } else {
                $new_pwd_encoded = $encoder->encodePassword($new_pwd, $user->getSalt());
                $user->setPassword($new_pwd_encoded);
                $manager = $this->getDoctrine()->getManager();
                $manager->persist($user);
    
                $manager->flush();
                $session->getFlashBag()->set('success_msg', "Password change successfully!");
            }
            return $this->render('@adminlte/profile/change_password.html.twig');
        }
    
        return $this->render('@adminlte/profile/change_password.html.twig', array(
    
        ));
    }
    

提交回复
热议问题