以ajax形式的Symfony UserPassword验证

时间:2014-06-09 10:37:31

标签: ajax forms validation passwords symfony-2.4

我在Symfony 2.4中验证用户密码时遇到问题。 我有一个用twig中的html代码创建的表单,我没有使用表单构建器,因为我通过ajax提交表单。

表单是更改密码表单,我有一个密码字段,必须与用户密码匹配。

代码:

表单的Html.twig代码:

        <form id="changePassword" name="changePassword">
            <label id="labelPassword">Write your current password </label>
            <input type="password" id="CurrentPassword" name="CurrentPassword" />
            <label id="labelNewPassword">Write your new password </label>
            <input type="password" id="NewPassword" name ="NewPassword" />
            <label id="labelNewPassword2">Repeat your new password</label>
            <input type="password"  id="NewPassword2" name ="NewPassword2" />
            <input type="submit" class="btn-primary btn" value="Change"/>
        </form>

ajax代码:

        var ServerData;
        $(document).ready(function() {
           $("form").submit(function(e) {
              e.preventDefault();
              var data = $(this).serialize();
              var url = $(this).attr("name");
              var id = $(this).attr("id");
              if(validates(url)){
                 $.ajax({
                    url: url+"/" ,
                    method: "post",
                    dataType: "json",
                    data: data,
                    success: function (ServerData){                    
                      successFunction(); 
                    },
                    error: function (){
                      errorFunction();
                    }
                 });
              }
              else{
                 novalidFunction();
              }

          });
       });





function validate(url){
//Just length and matching new password with repeat new password validations
}
// succesFunction(), errorFunction() and novalidFunction() and all this code are
//working great

控制器的php代码:

public function changePasswordAction ($request Request){
   $user=  $this->getUser();
   $password = $user->getPassword();
   $currentPassword = $request->get("CurrentPassword");
   $newPassword = $request->get("NewPassword");
   //here is where i need the code to compare $password with $currentPassword;
   //the problem is that $password is encoded

   //then i got the code to insert new values in Users table and its working;
}

提前致谢并对我的英语表示抱歉

1 个答案:

答案 0 :(得分:0)

我已经解决了这个问题: 由于您无法解码用户密码,因此您必须对新密码进行编码。这是完成我上一个代码的代码:

public function changePasswordAction(Request $request){
        $user = $this->getUser();
        $upassword = $user->getPassword();
        $password =  $request ->get("CurrentPassword");
        $newPassword = $request ->get("NewPassword");
        $factory = $this->get('security.encoder_factory');
        $encoder = $factory->getEncoder($user);
        $salt = $user->getSalt();
        $passwordSecure = $encoder->encodePassword($password, $salt);
        $em = $this->getDoctrine()->getManager();
        if ($passwordSecure == $upassword){
            if($newPassword == $newPasswordtwo){
                $newsalt = md5(time() * rand(1, 9999));//just a random number 
                $user->setSalt($newsalt);
                $user->setPassword($encoder->encodePassword($newPassword, $newsalt));
                $em->persist($user);
                $em->flush();
                return new \Symfony\Component\HttpFoundation\JsonResponse(array("estado" => "success", "msg" => "Password Changed"));
            }
            else{
                return new \Symfony\Component\HttpFoundation\JsonResponse(array("estado" => "error", "msg" => "New password doesn't match in both fields"));
            }
        }
        else{
            return new \Symfony\Component\HttpFoundation\JsonResponse(array("estado" => "error", "msg" => "User password is not correct"));
        }

}

这对我很有用。我希望这可以帮助别人。 :)