Skip to content

Commit ffe29b6

Browse files
Check for minimum password length upon self registration.
Fixes #2458.
1 parent 93f05c3 commit ffe29b6

File tree

3 files changed

+63
-45
lines changed

3 files changed

+63
-45
lines changed

webapp/src/Controller/SecurityController.php

Lines changed: 51 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace App\Controller;
44

5+
use App\Controller\Jury\UserController;
56
use App\Entity\Team;
67
use App\Entity\TeamAffiliation;
78
use App\Entity\TeamCategory;
@@ -12,6 +13,7 @@
1213
use Doctrine\ORM\EntityManagerInterface;
1314
use Ramsey\Uuid\Uuid;
1415
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
16+
use Symfony\Component\Form\FormInterface;
1517
use Symfony\Component\HttpFoundation\Request;
1618
use Symfony\Component\HttpFoundation\Response;
1719
use Symfony\Component\HttpKernel\Exception\HttpException;
@@ -103,58 +105,62 @@ public function registerAction(
103105
$registration_form->handleRequest($request);
104106
if ($registration_form->isSubmitted() && $registration_form->isValid()) {
105107
$plainPass = $registration_form->get('plainPassword')->getData();
106-
$password = $passwordHasher->hashPassword($user, $plainPass);
107-
$user->setPassword($password);
108-
if ($user->getName() === null) {
109-
$user->setName($user->getUsername());
110-
}
108+
if (strlen($plainPass) < UserController::MIN_PASSWORD_LENGTH) {
109+
$this->addFlash('danger', "Password should be " . UserController::MIN_PASSWORD_LENGTH . "+ chars.");
110+
} else {
111+
$password = $passwordHasher->hashPassword($user, $plainPass);
112+
$user->setPassword($password);
113+
if ($user->getName() === null) {
114+
$user->setName($user->getUsername());
115+
}
111116

112-
$teamName = $registration_form->get('teamName')->getData();
117+
$teamName = $registration_form->get('teamName')->getData();
113118

114-
if ($selfRegistrationCategoriesCount === 1) {
115-
$teamCategory = $em->getRepository(TeamCategory::class)->findOneBy(['allow_self_registration' => 1]);
116-
} else {
117-
// $selfRegistrationCategoriesCount > 1, 'teamCategory' field exists
118-
$teamCategory = $registration_form->get('teamCategory')->getData();
119-
}
119+
if ($selfRegistrationCategoriesCount === 1) {
120+
$teamCategory = $em->getRepository(TeamCategory::class)->findOneBy(['allow_self_registration' => 1]);
121+
} else {
122+
// $selfRegistrationCategoriesCount > 1, 'teamCategory' field exists
123+
$teamCategory = $registration_form->get('teamCategory')->getData();
124+
}
120125

121-
// Create a team to go with the user, then set some team attributes.
122-
$team = new Team();
123-
$user->setTeam($team);
124-
$team
125-
->setExternalid(Uuid::uuid4()->toString())
126-
->addUser($user)
127-
->setName($teamName)
128-
->setCategory($teamCategory)
129-
->setInternalComments('Registered by ' . $this->dj->getClientIp() . ' on ' . date('r'));
130-
131-
if ($this->config->get('show_affiliations')) {
132-
switch ($registration_form->get('affiliation')->getData()) {
133-
case 'new':
134-
$affiliation = new TeamAffiliation();
135-
$affiliation
136-
->setExternalid(Uuid::uuid4()->toString())
137-
->setName($registration_form->get('affiliationName')->getData())
138-
->setShortname($registration_form->get('affiliationShortName')->getData());
139-
if ($registration_form->has('affiliationCountry')) {
140-
$affiliation->setCountry($registration_form->get('affiliationCountry')->getData());
141-
}
142-
$team->setAffiliation($affiliation);
143-
$em->persist($affiliation);
144-
break;
145-
case 'existing':
146-
$team->setAffiliation($registration_form->get('existingAffiliation')->getData());
147-
break;
126+
// Create a team to go with the user, then set some team attributes.
127+
$team = new Team();
128+
$user->setTeam($team);
129+
$team
130+
->setExternalid(Uuid::uuid4()->toString())
131+
->addUser($user)
132+
->setName($teamName)
133+
->setCategory($teamCategory)
134+
->setInternalComments('Registered by ' . $this->dj->getClientIp() . ' on ' . date('r'));
135+
136+
if ($this->config->get('show_affiliations')) {
137+
switch ($registration_form->get('affiliation')->getData()) {
138+
case 'new':
139+
$affiliation = new TeamAffiliation();
140+
$affiliation
141+
->setExternalid(Uuid::uuid4()->toString())
142+
->setName($registration_form->get('affiliationName')->getData())
143+
->setShortname($registration_form->get('affiliationShortName')->getData());
144+
if ($registration_form->has('affiliationCountry')) {
145+
$affiliation->setCountry($registration_form->get('affiliationCountry')->getData());
146+
}
147+
$team->setAffiliation($affiliation);
148+
$em->persist($affiliation);
149+
break;
150+
case 'existing':
151+
$team->setAffiliation($registration_form->get('existingAffiliation')->getData());
152+
break;
153+
}
148154
}
149-
}
150155

151-
$em->persist($user);
152-
$em->persist($team);
153-
$em->flush();
156+
$em->persist($user);
157+
$em->persist($team);
158+
$em->flush();
154159

155-
$this->addFlash('success', 'Account registered successfully. Please log in.');
160+
$this->addFlash('success', 'Account registered successfully. Please log in.');
156161

157-
return $this->redirectToRoute('login');
162+
return $this->redirectToRoute('login');
163+
}
158164
}
159165

160166
return $this->render('security/register.html.twig', ['registration_form' => $registration_form]);

webapp/src/Form/Type/UserRegistrationType.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace App\Form\Type;
44

5+
use App\Controller\Jury\UserController;
56
use App\Entity\Role;
67
use App\Entity\Team;
78
use App\Entity\TeamAffiliation;
@@ -171,6 +172,7 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
171172
'placeholder' => 'Password',
172173
'autocomplete' => 'new-password',
173174
'spellcheck' => 'false',
175+
'minlength' => UserController::MIN_PASSWORD_LENGTH,
174176
],
175177
],
176178
'second_options' => [
@@ -179,6 +181,7 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
179181
'placeholder' => 'Repeat Password',
180182
'autocomplete' => 'new-password',
181183
'spellcheck' => 'false',
184+
'minlength' => UserController::MIN_PASSWORD_LENGTH,
182185
],
183186
],
184187
'mapped' => false,

webapp/templates/security/register.html.twig

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,15 @@
1313
<main>
1414
<div style="text-align: center;">
1515
<img class="mb-4" src="{{ asset('images/DOMjudgelogo.svg') }}" alt="DOMjudge" width="72">
16+
</div>
17+
<div class="container-fluid">
18+
<div class="row">
19+
<div class="col-12">
20+
{% block messages %}
21+
{% include 'partials/messages.html.twig' %}
22+
{% endblock %}
23+
</div>
24+
</div>
1625
</div>
1726
{{ form_start(registration_form, { 'attr': {'class': 'form-signin'} }) }}
1827
<h1 class="h3 mb-3 fw-normal">Register Account</h1>

0 commit comments

Comments
 (0)