Skip to content

Commit 16d3b9b

Browse files
committed
Making the make:registration-form work without authenticating
1 parent 14ade82 commit 16d3b9b

File tree

11 files changed

+192
-11
lines changed

11 files changed

+192
-11
lines changed

src/Maker/MakeRegistrationForm.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
use Symfony\Component\Console\Input\InputOption;
3333
use Symfony\Component\Form\AbstractType;
3434
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
35+
use Symfony\Component\Routing\RouterInterface;
3536
use Symfony\Component\Validator\Constraints\NotBlank;
3637
use Symfony\Component\Validator\Validation;
3738

@@ -44,10 +45,13 @@ final class MakeRegistrationForm extends AbstractMaker
4445

4546
private $formTypeRenderer;
4647

47-
public function __construct(FileManager $fileManager, FormTypeRenderer $formTypeRenderer)
48+
private $router;
49+
50+
public function __construct(FileManager $fileManager, FormTypeRenderer $formTypeRenderer, RouterInterface $router)
4851
{
4952
$this->fileManager = $fileManager;
5053
$this->formTypeRenderer = $formTypeRenderer;
54+
$this->router = $router;
5155
}
5256

5357
public static function getCommandName(): string
@@ -64,6 +68,7 @@ public function configureCommand(Command $command, InputConfiguration $inputConf
6468
->addArgument('password-field', InputArgument::OPTIONAL, 'Field on your User class that stores the hashed password')
6569
->addOption('auto-login-authenticator', null, InputOption::VALUE_REQUIRED, 'Authenticator class to use for logging in')
6670
->addOption('firewall-name', null, InputOption::VALUE_REQUIRED, 'Firewall key used for authentication')
71+
->addOption('redirect-route-name', null, InputOption::VALUE_REQUIRED, 'Route for redirecting if not authenticating')
6772
->setHelp(file_get_contents(__DIR__.'/../Resources/help/MakeRegistrationForm.txt'))
6873
;
6974

@@ -82,7 +87,7 @@ public function interact(InputInterface $input, ConsoleStyle $io, Command $comma
8287

8388
$manipulator = new YamlSourceManipulator($this->fileManager->getFileContents($path));
8489
$securityData = $manipulator->getData();
85-
$providersData = $securityData['security']['providers'];
90+
$providersData = $securityData['security']['providers'] ?? [];
8691

8792
$input->setArgument(
8893
'user-class',
@@ -112,6 +117,15 @@ public function interact(InputInterface $input, ConsoleStyle $io, Command $comma
112117
$securityData,
113118
$command
114119
);
120+
} else {
121+
$routeNames = array_keys($this->router->getRouteCollection()->all());
122+
$input->setOption(
123+
'redirect-route-name',
124+
$io->choice(
125+
'What route should the user be redirected to after registration?',
126+
$routeNames
127+
)
128+
);
115129
}
116130
}
117131

@@ -182,6 +196,7 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
182196
'authenticator_class_name' => $authenticatorClassName ? Str::getShortClassName($authenticatorClassName) : null,
183197
'authenticator_full_class_name' => $authenticatorClassName,
184198
'firewall_name' => $input->getOption('firewall-name'),
199+
'redirect_route_name' => $input->getOption('redirect-route-name'),
185200
]
186201
);
187202

src/Resources/config/makers.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
<service id="maker.maker.make_registration_form" class="Symfony\Bundle\MakerBundle\Maker\MakeRegistrationForm">
5656
<argument type="service" id="maker.file_manager" />
5757
<argument type="service" id="maker.renderer.form_type_renderer" />
58+
<argument type="service" id="router" />
5859
<tag name="maker.command" />
5960
</service>
6061

src/Resources/skeleton/registration/RegistrationController.tpl.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,7 @@ public function register(Request $request, UserPasswordEncoderInterface $passwor
5252
'<?= $firewall_name; ?>' // firewall name in security.yaml
5353
);
5454
<?php else: ?>
55-
throw new \Exception('TODO - in RegistrationController, decide where to redirect the user');
56-
57-
return $this->redirectToRoute('<?= $route_name ?>_index');
55+
return $this->redirectToRoute('<?= $redirect_route_name ?>');
5856
<?php endif; ?>
5957
}
6058

tests/Maker/FunctionalTest.php

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -700,13 +700,34 @@ function (string $output, string $directory) {
700700
])
701701
->setFixtureFilesPath(__DIR__.'/../fixtures/MakeRegistrationFormEntity')
702702
->configureDatabase()
703-
->setGuardAuthenticator('main', 'App\\Security\\StubAuthenticator')
704703
->updateSchemaAfterCommand()
705704
];
706705

707-
// non-standard email & password field
708-
// no authenticators
709-
// saying no to auto-login
706+
// sanity check on all the interactive questions
707+
yield 'registration_form_no_guessing' => [MakerTestDetails::createTest(
708+
$this->getMakerInstance(MakeRegistrationForm::class),
709+
[
710+
'App\\Entity\\User',
711+
'emailAlt', // username field
712+
'passwordAlt', // password field
713+
'', // yes authenticate after
714+
'main', // firewall
715+
'1' // authenticator
716+
])
717+
->setFixtureFilesPath(__DIR__.'/../fixtures/MakeRegistrationFormNoGuessing')
718+
];
719+
720+
yield 'registration_form_entity_no_authenticate' => [MakerTestDetails::createTest(
721+
$this->getMakerInstance(MakeRegistrationForm::class),
722+
[
723+
// all basic data guessed
724+
'n', // no authenticate after
725+
'app_anonymous', // route name to redirect to
726+
])
727+
->setFixtureFilesPath(__DIR__.'/../fixtures/MakeRegistrationFormEntity')
728+
->configureDatabase()
729+
->updateSchemaAfterCommand()
730+
];
710731
}
711732

712733
public function getCommandEntityTests()
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
app_homepage:
22
path: /
33
controller: App\Controller\TestingController::homepage
4+
5+
app_anonymous:
6+
path: /anonymous
7+
controller: App\Controller\TestingController::anonymous

tests/fixtures/MakeRegistrationFormEntity/src/Controller/TestingController.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ public function homepage()
1111
{
1212
$this->denyAccessUnlessGranted('ROLE_USER');
1313

14-
return new Response('Homepage Success');
14+
return new Response('Page Success');
15+
}
16+
17+
public function anonymous()
18+
{
19+
return new Response('Page Success');
1520
}
1621
}

tests/fixtures/MakeRegistrationFormEntity/tests/RegistrationFormTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,6 @@ public function testGeneratedEntity()
2929
$this->assertSame(302, $client->getResponse()->getStatusCode());
3030
$client->followRedirect();
3131
$this->assertSame(200, $client->getResponse()->getStatusCode());
32-
$this->assertSame('Homepage Success', $client->getResponse()->getContent());
32+
$this->assertSame('Page Success', $client->getResponse()->getContent());
3333
}
3434
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
security:
2+
encoders:
3+
App\Entity\User: bcrypt
4+
5+
firewalls:
6+
main_other:
7+
anonymous: true
8+
main:
9+
anonymous: true
10+
guard:
11+
entry_point: App\Security\AuthenticatorFirst
12+
authenticators:
13+
- App\Security\AuthenticatorFirst
14+
- App\Security\AuthenticatorSecond
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
<?php
2+
3+
namespace App\Entity;
4+
5+
use Doctrine\ORM\Mapping as ORM;
6+
use Symfony\Component\Security\Core\User\UserInterface;
7+
8+
/**
9+
* @ORM\Entity()
10+
*/
11+
class User implements UserInterface
12+
{
13+
/**
14+
* @ORM\Id()
15+
* @ORM\GeneratedValue()
16+
* @ORM\Column(type="integer")
17+
*/
18+
private $id;
19+
20+
/**
21+
* @ORM\Column(type="string", length=180, unique=true)
22+
*/
23+
private $emailAlt;
24+
25+
/**
26+
* @ORM\Column(type="array")
27+
*/
28+
private $roles = [];
29+
30+
/**
31+
* @var string The hashed password
32+
* @ORM\Column(type="string")
33+
*/
34+
private $passwordAlt;
35+
36+
public function getId()
37+
{
38+
return $this->id;
39+
}
40+
41+
public function getEmailAlt()
42+
{
43+
return $this->emailAlt;
44+
}
45+
46+
public function setEmailAlt(string $email): self
47+
{
48+
$this->emailAlt = $email;
49+
50+
return $this;
51+
}
52+
53+
/**
54+
* A visual identifier that represents this user.
55+
*
56+
* @see UserInterface
57+
*/
58+
public function getUsername(): string
59+
{
60+
return (string) $this->emailAlt;
61+
}
62+
63+
/**
64+
* @see UserInterface
65+
*/
66+
public function getRoles(): array
67+
{
68+
$roles = $this->roles;
69+
// guarantee every user at least has ROLE_USER
70+
$roles[] = 'ROLE_USER';
71+
72+
return array_unique($roles);
73+
}
74+
75+
public function setRoles(array $roles): self
76+
{
77+
$this->roles = $roles;
78+
79+
return $this;
80+
}
81+
82+
/**
83+
* @see UserInterface
84+
*/
85+
public function getPasswordAlt(): string
86+
{
87+
return (string) $this->passwordAlt;
88+
}
89+
90+
public function setPasswordAlt(string $password): self
91+
{
92+
$this->passwordAlt = $password;
93+
94+
return $this;
95+
}
96+
97+
public function getPassword()
98+
{
99+
return $this->passwordAlt;
100+
}
101+
102+
public function getSalt()
103+
{
104+
}
105+
106+
public function eraseCredentials()
107+
{
108+
}
109+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace App\Security;
4+
5+
class AuthenticatorFirst
6+
{
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace App\Security;
4+
5+
class AuthenticatorSecond
6+
{
7+
}

0 commit comments

Comments
 (0)