Esempio n. 1
0
 /**
  * @Security("has_role('ROLE_USER')")
  * @Route("/user/change-pass", name="user-change-pass", options={"expose"=true})
  * @param Request $request
  * @return JsonResponse
  */
 public function changePasswordAction(Request $request)
 {
     $user_id = $this->getUser()->getId();
     $data = json_decode($request->getContent(), true);
     $data = (object) $data['changes'];
     /** @var EncoderFactory $encoderFactory */
     $encoderFactory = $this->get('security.encoder_factory');
     /** @var User $user */
     $user = $this->getUser();
     $encoder = $encoderFactory->getEncoder($user);
     $encodedOldPass = $encoder->encodePassword($data->opass, $user->getSalt());
     if ($user->getPassword() == $encodedOldPass && $data->pass && $data->pass == $data->rpass) {
         $encodedPass = $encoder->encodePassword($data->pass, $user->getSalt());
         /** @var EntityManager $em */
         $em = $this->getDoctrine()->getManager();
         try {
             User::changePassword($em, $user_id, $encodedPass);
         } catch (\Exception $ex) {
             $from = "Class: User, function: changePassword";
             $this->get('error_logger')->registerException($ex, $from);
             return new JsonResponse(-1);
         }
         return new JsonResponse(1);
     } else {
         return new JsonResponse(0);
     }
 }