From 18864ca9faf2e08e04e520add5bc9dc1f1677574 Mon Sep 17 00:00:00 2001 From: Hugo Sales Date: Sun, 13 Mar 2022 15:22:32 +0000 Subject: [PATCH] [CONTROLLER][Security] Override the `_next` form field in Security->register to redirect to login page --- src/Controller/Security.php | 61 +++++++++++++++++++++---------------- src/Core/Form.php | 5 +-- 2 files changed, 37 insertions(+), 29 deletions(-) diff --git a/src/Controller/Security.php b/src/Controller/Security.php index 2bd35b8190..f8deb43883 100644 --- a/src/Controller/Security.php +++ b/src/Controller/Security.php @@ -11,6 +11,7 @@ use App\Core\Event; use App\Core\Form; use function App\Core\I18n\_m; use App\Core\Log; +use App\Core\Router\Router; use App\Entity\Actor; use App\Entity\Feed; use App\Entity\LocalUser; @@ -33,6 +34,7 @@ use Component\Subscription\Entity\ActorSubscription; use Doctrine\DBAL\Exception\UniqueConstraintViolationException; use LogicException; use Symfony\Component\Form\Extension\Core\Type\EmailType; +use Symfony\Component\Form\Extension\Core\Type\HiddenType; use Symfony\Component\Form\Extension\Core\Type\SubmitType; use Symfony\Component\Form\Extension\Core\Type\TextType; use Symfony\Component\HttpFoundation\Request; @@ -96,33 +98,37 @@ class Security extends Controller // \App\Core\Security $authenticator, // UserAuthenticatorInterface $user_authenticator, ): array|Response { - $form = Form::create([ - ['nickname', TextType::class, [ - 'label' => _m('Nickname'), - 'help' => _m('Your desired nickname (e.g., j0hnD03)'), - 'constraints' => [ - new NotBlank(['message' => _m('Please enter a nickname')]), - new Length([ - 'max' => Nickname::MAX_LEN, - 'maxMessage' => _m(['Your nickname must be at most # characters long'], ['count' => Nickname::MAX_LEN]), - ]), - ], - 'block_name' => 'nickname', - 'label_attr' => ['class' => 'section-form-label'], - 'invalid_message' => _m('Nickname not valid. Please provide a valid nickname.'), - ]], - ['email', EmailType::class, [ - 'label' => _m('Email'), - 'help' => _m('Desired email for this account (e.g., john@provider.com)'), - 'constraints' => [new NotBlank(['message' => _m('Please enter an email')])], - 'block_name' => 'email', - 'label_attr' => ['class' => 'section-form-label'], - 'invalid_message' => _m('Email not valid. Please provide a valid email.'), - 'attr' => ['autocomplete' => 'email'], - ]], - FormFields::repeated_password(['attr' => ['autocomplete' => 'new-password']]), - ['register', SubmitType::class, ['label' => _m('Register')]], - ], form_options: ['block_prefix' => 'register']); + $form = Form::create( + [ + ['nickname', TextType::class, [ + 'label' => _m('Nickname'), + 'help' => _m('Your desired nickname (e.g., j0hnD03)'), + 'constraints' => [ + new NotBlank(['message' => _m('Please enter a nickname')]), + new Length([ + 'max' => Nickname::MAX_LEN, + 'maxMessage' => _m(['Your nickname must be at most # characters long'], ['count' => Nickname::MAX_LEN]), + ]), + ], + 'block_name' => 'nickname', + 'label_attr' => ['class' => 'section-form-label'], + 'invalid_message' => _m('Nickname not valid. Please provide a valid nickname.'), + ]], + ['email', EmailType::class, [ + 'label' => _m('Email'), + 'help' => _m('Desired email for this account (e.g., john@provider.com)'), + 'constraints' => [new NotBlank(['message' => _m('Please enter an email')])], + 'block_name' => 'email', + 'label_attr' => ['class' => 'section-form-label'], + 'invalid_message' => _m('Email not valid. Please provide a valid email.'), + 'attr' => ['autocomplete' => 'email'], + ]], + FormFields::repeated_password(['attr' => ['autocomplete' => 'new-password']]), + ['register', SubmitType::class, ['label' => _m('Register')]] + ], + extra_data: ['_next' => Router::url('security_login')], + form_options: ['block_prefix' => 'register'] + ); $form->handleRequest($request); @@ -205,6 +211,7 @@ class Security extends Controller $user->setIsEmailVerified(true); } + return Form::forceRedirect($form, $request); // return $user_authenticator->authenticateUser($user, $authenticator, $request); } diff --git a/src/Core/Form.php b/src/Core/Form.php index 9a7a4787e1..0143d75fbe 100644 --- a/src/Core/Form.php +++ b/src/Core/Form.php @@ -100,8 +100,9 @@ abstract class Form string $type = '\Symfony\Component\Form\Extension\Core\Type\FormType', array $form_options = [], ): SymfFormInterface { - $name = $form[array_key_last($form)][0]; - $r = Common::getRequest(); + $name = $form[array_key_last($form)][0]; + $r = Common::getRequest(); + $form[] = ['_next', HiddenType::class, ['data' => $r->get('next') ?? $r->get('_next') ?? $r->getRequestUri()]]; $fb = self::$form_factory->createNamedBuilder($name, $type, data: null, options: array_merge($form_options, ['translation_domain' => false]));