2014-06-09 4 views
0

Symfony 2.4에서 사용자 비밀번호의 유효성 검사에 문제가 있습니다. 나뭇 가지 안에 html 코드로 만든 양식이 있고 양식 작성기를 사용하지 않습니다. 양식을 ajax를 통해 제출하기 때문에 양식 빌더를 사용하지 않습니다.ajax 양식의 Symfony UserPassword 유효성 확인

양식은 암호 변경 양식이며 사용자 암호와 일치해야하는 암호 필드가 있습니다.

코드 :

형태의

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> 

아약스 코드 : 컨트롤러의

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

감사에서 내 engl에 대해 사전에 미안하다. ish

답변

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

} 

저에게는 큰 도움이되고 있습니다. 나는 그것이 누군가를 도울 수 있기를 바랍니다. :)