[CONTROLLER][Security] Override the _next form field in Security->register to redirect to login page

This commit is contained in:
Hugo Sales 2022-03-13 15:22:32 +00:00
parent 390c532456
commit 18864ca9fa
No known key found for this signature in database
GPG Key ID: 7D0C7EAFC9D835A0
2 changed files with 37 additions and 29 deletions

View File

@ -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);
}

View File

@ -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]));