Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * @Rest\Route("/changePassword", methods={"PATCH"})
- * @ApiDoc(
- * section="User",
- * resource=true
- * )
- *
- * @Security("has_role('ROLE_USER')")
- *
- * @Rest\RequestParam(name="oldPassword", description="old password", nullable=false)
- * @Rest\RequestParam(name="newPassword", description="new password", nullable=false, requirements="[\d\w\!\@\#\$\%\^\&\*\?]{4,8}")
- * @Rest\RequestParam(name="confirmNewPassword", description="confirm new password", nullable=false, requirements="[\d\w\!\@\#\$\%\^\&\*\?]{4,8}"))
- * @param ParamFetcherInterface $fetcher
- * @return JsonResponse
- * @throws \Symfony\Component\HttpKernel\Exception\BadRequestHttpException
- */
- public function patchUserPasswordAction(ParamFetcherInterface $fetcher)
- {
- try {
- $user = $this->getUser();
- $encoder = $this->get('security.password_encoder');
- $oldPassword = $fetcher->get('oldPassword');
- $newPassword = $fetcher->get('newPassword');
- $confirmNewPassword = $fetcher->get('confirmNewPassword');
- if (!$encoder->isPasswordValid($user, $fetcher->get('oldPassword'))) {
- throw new BadRequestHttpException('Старый пароль введен неверно');
- }
- if ($newPassword === $oldPassword) {
- throw new BadRequestHttpException('Старый и новый пароль не должны совпадать');
- }
- if ($newPassword !== $confirmNewPassword) {
- throw new BadRequestHttpException('Пароли не совпадают');
- }
- $this->get('app.service.user_provider')->updatePassword($user, $encoder->encodePassword($user, $fetcher->get('newPassword')));
- return new JsonResponse(
- [
- 'ok' => 200,
- ]
- );
- } catch (\Exception $e) {
- throw new BadRequestHttpException($e->getMessage(), $e);
- }
- }
Add Comment
Please, Sign In to add comment