Guest User

Untitled

a guest
Feb 28th, 2017
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.07 KB | None | 0 0
  1. /**
  2.      * @Rest\Route("/changePassword", methods={"PATCH"})
  3.      * @ApiDoc(
  4.      *     section="User",
  5.      *     resource=true
  6.      * )
  7.      *
  8.      * @Security("has_role('ROLE_USER')")
  9.      *
  10.      * @Rest\RequestParam(name="oldPassword", description="old password", nullable=false)
  11.      * @Rest\RequestParam(name="newPassword", description="new password", nullable=false, requirements="[\d\w\!\@\#\$\%\^\&\*\?]{4,8}")
  12.      * @Rest\RequestParam(name="confirmNewPassword", description="confirm new password", nullable=false, requirements="[\d\w\!\@\#\$\%\^\&\*\?]{4,8}"))
  13.      * @param ParamFetcherInterface $fetcher
  14.      * @return JsonResponse
  15.      * @throws \Symfony\Component\HttpKernel\Exception\BadRequestHttpException
  16.      */
  17.     public function patchUserPasswordAction(ParamFetcherInterface $fetcher)
  18.     {
  19.         try {
  20.             $user = $this->getUser();
  21.             $encoder = $this->get('security.password_encoder');
  22.  
  23.             $oldPassword = $fetcher->get('oldPassword');
  24.             $newPassword = $fetcher->get('newPassword');
  25.             $confirmNewPassword = $fetcher->get('confirmNewPassword');
  26.  
  27.  
  28.             if (!$encoder->isPasswordValid($user, $fetcher->get('oldPassword'))) {
  29.                 throw new BadRequestHttpException('Старый пароль введен неверно');
  30.             }
  31.  
  32.             if ($newPassword === $oldPassword) {
  33.                 throw new BadRequestHttpException('Старый и новый пароль не должны совпадать');
  34.             }
  35.  
  36.             if ($newPassword !== $confirmNewPassword) {
  37.                 throw new BadRequestHttpException('Пароли не совпадают');
  38.             }
  39.  
  40.             $this->get('app.service.user_provider')->updatePassword($user, $encoder->encodePassword($user, $fetcher->get('newPassword')));
  41.  
  42.             return new JsonResponse(
  43.                 [
  44.                     'ok' => 200,
  45.                 ]
  46.             );
  47.  
  48.  
  49.         } catch (\Exception $e) {
  50.             throw new BadRequestHttpException($e->getMessage(), $e);
  51.         }
  52.  
  53.     }
Add Comment
Please, Sign In to add comment