diff --git a/src/Controller/AccountController.php b/src/Controller/AccountController.php index 1464557e..217b2387 100644 --- a/src/Controller/AccountController.php +++ b/src/Controller/AccountController.php @@ -17,6 +17,7 @@ use App\EventListener\AuthenticationLoginListener; use App\Form\LoginFormType; +use App\Form\PasswordMaxLengthTrait; use App\Form\RegistrationFormHandler; use App\Form\RegistrationFormType; use App\Model\Customer; @@ -54,6 +55,7 @@ */ class AccountController extends BaseController { + use PasswordMaxLengthTrait; /** * @Route("/account/login", name="account-login") * @@ -213,6 +215,10 @@ public function registerAction( $customer->setActive(true); try { + if(!$hidePassword) { + $this->checkPassword($form->getData()['password']); + } + $customer->save(); if ($form->getData()['newsletter']) { @@ -509,24 +515,35 @@ public function resetPasswordAction(Request $request, PasswordRecoveryService $s { $token = $request->get('token'); $customer = $service->getCustomerByToken($token); - if (!$customer) { - //TODO render error page - throw new NotFoundHttpException('Invalid token'); - } + $error = null; + try { + if (!$customer) { + throw new NotFoundHttpException('Invalid token'); + } - if ($request->isMethod(Request::METHOD_POST)) { - $newPassword = $request->get('password'); - $service->setPassword($token, $newPassword); + if ($request->isMethod(Request::METHOD_POST)) { - $this->addFlash('success', $translator->trans('account.password-reset-successful')); + $newPassword = $request->get('password'); - return $this->redirectToRoute('account-login', ['no-referer-redirect' => true]); + $this->checkPassword($newPassword); + + $service->setPassword($token, $newPassword); + + $this->addFlash('success', $translator->trans('account.password-reset-successful')); + + return $this->redirectToRoute('account-login', ['no-referer-redirect' => true]); + + } + + } catch (\Exception $exception) { + $error = $exception->getMessage(); } return $this->render('account/reset_password.html.twig', [ 'hideBreadcrumbs' => true, 'token' => $token, - 'email' => $customer->getEmail() + 'email' => $customer?->getEmail(), + 'error' => $error ]); } } diff --git a/src/Form/LoginFormType.php b/src/Form/LoginFormType.php index af52bbf8..cc80a924 100644 --- a/src/Form/LoginFormType.php +++ b/src/Form/LoginFormType.php @@ -36,6 +36,7 @@ use Symfony\Component\Form\Extension\Core\Type\SubmitType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolver; +use Symfony\Component\PasswordHasher\PasswordHasherInterface; class LoginFormType extends AbstractType { @@ -55,6 +56,9 @@ public function buildForm(FormBuilderInterface $builder, array $options) 'label' => 'user.password', 'label_attr' => [ 'class' => 'sr-only' + ], + 'attr' => [ + 'maxlength' => PasswordHasherInterface::MAX_PASSWORD_LENGTH ] ]) ->add('_target_path', HiddenType::class) diff --git a/src/Form/PasswordMaxLengthTrait.php b/src/Form/PasswordMaxLengthTrait.php new file mode 100644 index 00000000..29ce16e2 --- /dev/null +++ b/src/Form/PasswordMaxLengthTrait.php @@ -0,0 +1,37 @@ +isPasswordTooLong($password)) { + throw new ValidationException("Given password is too long."); + } + } +} diff --git a/src/Form/RegistrationFormType.php b/src/Form/RegistrationFormType.php index befb3d25..2ed3db6a 100644 --- a/src/Form/RegistrationFormType.php +++ b/src/Form/RegistrationFormType.php @@ -38,6 +38,7 @@ use Symfony\Component\Form\Extension\Core\Type\TextType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolver; +use Symfony\Component\PasswordHasher\PasswordHasherInterface; class RegistrationFormType extends AbstractType { @@ -61,7 +62,10 @@ public function buildForm(FormBuilderInterface $builder, array $options) ]); if (!$options['hidePassword']) { $builder->add('password', PasswordType::class, [ - 'label' => 'general.password' + 'label' => 'general.password', + 'attr' => [ + 'maxlength' => PasswordHasherInterface::MAX_PASSWORD_LENGTH + ] ]); } diff --git a/templates/account/reset_password.html.twig b/templates/account/reset_password.html.twig index 0e1ad645..1c50fecf 100644 --- a/templates/account/reset_password.html.twig +++ b/templates/account/reset_password.html.twig @@ -8,6 +8,11 @@