【问题标题】:Symfony UserPassword validation in ajax formajax 形式的 Symfony UserPassword 验证
【发布时间】:2014-07-29 21:16:50
【问题描述】:

我在 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;
}

在此先感谢,对我的英语感到抱歉

【问题讨论】:

    标签: ajax forms validation passwords symfony-2.4


    【解决方案1】:

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

    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"));
            }
    
    }
    

    这对我来说非常有用。我希望这可以帮助某人。 :)

    【讨论】:

      猜你喜欢
      • 2014-07-23
      • 2011-06-27
      • 2017-04-02
      • 1970-01-01
      • 2019-03-19
      • 2018-11-16
      • 2015-03-30
      • 2023-02-23
      • 1970-01-01
      相关资源
      最近更新 更多