26
26
use Symfony \Bundle \MakerBundle \InputConfiguration ;
27
27
use Symfony \Bundle \MakerBundle \Renderer \FormTypeRenderer ;
28
28
use Symfony \Bundle \MakerBundle \Security \InteractiveSecurityHelper ;
29
+ use Symfony \Bundle \MakerBundle \Security \Model \Authenticator ;
30
+ use Symfony \Bundle \MakerBundle \Security \Model \AuthenticatorType ;
29
31
use Symfony \Bundle \MakerBundle \Str ;
30
32
use Symfony \Bundle \MakerBundle \Util \ClassDetails ;
31
33
use Symfony \Bundle \MakerBundle \Util \ClassNameDetails ;
34
36
use Symfony \Bundle \MakerBundle \Util \UseStatementGenerator ;
35
37
use Symfony \Bundle \MakerBundle \Util \YamlSourceManipulator ;
36
38
use Symfony \Bundle \MakerBundle \Validator ;
39
+ use Symfony \Bundle \SecurityBundle \Security ;
37
40
use Symfony \Bundle \SecurityBundle \SecurityBundle ;
38
41
use Symfony \Bundle \TwigBundle \TwigBundle ;
39
42
use Symfony \Component \Console \Command \Command ;
49
52
use Symfony \Component \Routing \Attribute \Route ;
50
53
use Symfony \Component \Routing \RouterInterface ;
51
54
use Symfony \Component \Security \Core \User \UserInterface ;
52
- use Symfony \Component \Security \Http \Authentication \UserAuthenticatorInterface ;
53
55
use Symfony \Component \Translation \Translator ;
54
56
use Symfony \Component \Validator \Validation ;
55
57
use Symfony \Contracts \Translation \TranslatorInterface ;
@@ -75,8 +77,7 @@ final class MakeRegistrationForm extends AbstractMaker
75
77
private $ emailGetter ;
76
78
private $ fromEmailAddress ;
77
79
private $ fromEmailName ;
78
- private $ autoLoginAuthenticator ;
79
- private $ firewallName ;
80
+ private ?Authenticator $ autoLoginAuthenticator = null ;
80
81
private $ redirectRouteName ;
81
82
private $ addUniqueEntityConstraint ;
82
83
@@ -110,7 +111,7 @@ public function interact(InputInterface $input, ConsoleStyle $io, Command $comma
110
111
$ interactiveSecurityHelper = new InteractiveSecurityHelper ();
111
112
112
113
if (null === $ this ->router ) {
113
- throw new RuntimeCommandException ('Router have been explicitely disabled in your configuration. This command needs to use the router. ' );
114
+ throw new RuntimeCommandException ('Router have been explicitly disabled in your configuration. This command needs to use the router. ' );
114
115
}
115
116
116
117
if (!$ this ->fileManager ->fileExists ($ path = 'config/packages/security.yaml ' )) {
@@ -184,32 +185,20 @@ public function interact(InputInterface $input, ConsoleStyle $io, Command $comma
184
185
185
186
private function interactAuthenticatorQuestions (ConsoleStyle $ io , InteractiveSecurityHelper $ interactiveSecurityHelper , array $ securityData ): void
186
187
{
187
- $ firewallsData = $ securityData ['security ' ]['firewalls ' ] ?? [];
188
- $ firewallName = $ interactiveSecurityHelper ->guessFirewallName (
189
- $ io ,
190
- $ securityData ,
191
- 'Which firewall key in security.yaml holds the authenticator you want to use for logging in? '
192
- );
188
+ // get list of authenticators
189
+ $ authenticators = $ interactiveSecurityHelper ->getAuthenticatorsFromConfig ($ securityData ['security ' ]['firewalls ' ] ?? []);
193
190
194
- if (! isset ( $ firewallsData [ $ firewallName ] )) {
195
- $ io ->note ('No firewalls found - skipping authentication after registration. You might want to configure your security before running this command . ' );
191
+ if (empty ( $ authenticators )) {
192
+ $ io ->note ('No authenticators found - so your user won \' t be automatically authenticated after registering . ' );
196
193
197
194
return ;
198
195
}
199
196
200
- $ this ->firewallName = $ firewallName ;
201
-
202
- // get list of guard authenticators
203
- $ authenticatorClasses = $ interactiveSecurityHelper ->getAuthenticatorClasses ($ firewallsData [$ firewallName ]);
204
- if (empty ($ authenticatorClasses )) {
205
- $ io ->note ('No Guard authenticators found - so your user won \'t be automatically authenticated after registering. ' );
206
- } else {
207
- $ this ->autoLoginAuthenticator =
208
- 1 === \count ($ authenticatorClasses ) ? $ authenticatorClasses [0 ] : $ io ->choice (
209
- 'Which authenticator \'s onAuthenticationSuccess() should be used after logging in? ' ,
210
- $ authenticatorClasses
211
- );
212
- }
197
+ $ this ->autoLoginAuthenticator =
198
+ 1 === \count ($ authenticators ) ? $ authenticators [0 ] : $ io ->choice (
199
+ 'Which authenticator should be used to login the user? ' ,
200
+ $ authenticators
201
+ );
213
202
}
214
203
215
204
public function generate (InputInterface $ input , ConsoleStyle $ io , Generator $ generator ): void
@@ -312,11 +301,22 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
312
301
}
313
302
}
314
303
315
- if ($ this ->autoLoginAuthenticator ) {
304
+ $ autoLoginVars = [
305
+ 'login_after_registration ' => null !== $ this ->autoLoginAuthenticator ,
306
+ ];
307
+
308
+ if (null !== $ this ->autoLoginAuthenticator ) {
316
309
$ useStatements ->addUseStatement ([
317
- $ this ->autoLoginAuthenticator ,
318
- UserAuthenticatorInterface::class,
310
+ Security::class,
319
311
]);
312
+
313
+ $ autoLoginVars ['firewall ' ] = $ this ->autoLoginAuthenticator ->firewallName ;
314
+ $ autoLoginVars ['authenticator ' ] = sprintf ('\'%s \'' , $ this ->autoLoginAuthenticator ->type ->value );
315
+
316
+ if (AuthenticatorType::CUSTOM === $ this ->autoLoginAuthenticator ->type ) {
317
+ $ useStatements ->addUseStatement ($ this ->autoLoginAuthenticator ->authenticatorClass );
318
+ $ autoLoginVars ['authenticator ' ] = sprintf ('%s::class ' , Str::getShortClassName ($ this ->autoLoginAuthenticator ->authenticatorClass ));
319
+ }
320
320
}
321
321
322
322
if ($ isTranslatorAvailable = class_exists (Translator::class)) {
@@ -339,14 +339,11 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
339
339
'from_email ' => $ this ->fromEmailAddress ,
340
340
'from_email_name ' => addslashes ($ this ->fromEmailName ),
341
341
'email_getter ' => $ this ->emailGetter ,
342
- 'authenticator_class_name ' => $ this ->autoLoginAuthenticator ? Str::getShortClassName ($ this ->autoLoginAuthenticator ) : null ,
343
- 'authenticator_full_class_name ' => $ this ->autoLoginAuthenticator ,
344
- 'firewall_name ' => $ this ->firewallName ,
345
342
'redirect_route_name ' => $ this ->redirectRouteName ,
346
- 'password_hasher_class_details ' => $ generator ->createClassNameDetails (UserPasswordHasherInterface::class, '\\' ),
347
343
'translator_available ' => $ isTranslatorAvailable ,
348
344
],
349
- $ userRepoVars
345
+ $ userRepoVars ,
346
+ $ autoLoginVars ,
350
347
)
351
348
);
352
349
0 commit comments