49
49
use Symfony \Component \Security \Http \Authenticator \AbstractAuthenticator ;
50
50
use Symfony \Component \Security \Http \Authenticator \AbstractLoginFormAuthenticator ;
51
51
use Symfony \Component \Security \Http \Authenticator \Passport \Badge \CsrfTokenBadge ;
52
+ use Symfony \Component \Security \Http \Authenticator \Passport \Badge \RememberMeBadge ;
52
53
use Symfony \Component \Security \Http \Authenticator \Passport \Badge \UserBadge ;
53
54
use Symfony \Component \Security \Http \Authenticator \Passport \Credentials \PasswordCredentials ;
54
55
use Symfony \Component \Security \Http \Authenticator \Passport \Passport ;
@@ -66,6 +67,9 @@ final class MakeAuthenticator extends AbstractMaker
66
67
private const AUTH_TYPE_EMPTY_AUTHENTICATOR = 'empty-authenticator ' ;
67
68
private const AUTH_TYPE_FORM_LOGIN = 'form-login ' ;
68
69
70
+ private const REMEMBER_ME_TYPE_ALWAYS = 'always ' ;
71
+ private const REMEMBER_ME_TYPE_CHECKBOX = 'checkbox ' ;
72
+
69
73
public function __construct (
70
74
private FileManager $ fileManager ,
71
75
private SecurityConfigUpdater $ configUpdater ,
@@ -184,6 +188,34 @@ function ($answer) {
184
188
true
185
189
)
186
190
);
191
+
192
+ $ command ->addArgument ('support-remember-me ' , InputArgument::REQUIRED );
193
+ $ input ->setArgument (
194
+ 'support-remember-me ' ,
195
+ $ io ->confirm (
196
+ 'Do you want to support remember me? ' ,
197
+ true
198
+ )
199
+ );
200
+
201
+ if ($ input ->getArgument ('support-remember-me ' )) {
202
+ $ supportRememberMeValues = [
203
+ 'Activate when the user checks a box ' => self ::REMEMBER_ME_TYPE_CHECKBOX ,
204
+ 'Always activate remember me ' => self ::REMEMBER_ME_TYPE_ALWAYS ,
205
+ ];
206
+ $ command ->addArgument ('always-remember-me ' , InputArgument::REQUIRED );
207
+
208
+ $ supportRememberMeType = $ io ->choice (
209
+ 'When activate the remember me? ' ,
210
+ array_keys ($ supportRememberMeValues ),
211
+ key ($ supportRememberMeValues )
212
+ );
213
+
214
+ $ input ->setArgument (
215
+ 'always-remember-me ' ,
216
+ $ supportRememberMeValues [$ supportRememberMeType ]
217
+ );
218
+ }
187
219
}
188
220
}
189
221
@@ -192,12 +224,16 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
192
224
$ manipulator = new YamlSourceManipulator ($ this ->fileManager ->getFileContents ('config/packages/security.yaml ' ));
193
225
$ securityData = $ manipulator ->getData ();
194
226
227
+ $ supportRememberMe = $ input ->hasArgument ('support-remember-me ' ) ? $ input ->getArgument ('support-remember-me ' ) : false ;
228
+ $ alwaysRememberMe = $ input ->hasArgument ('always-remember-me ' ) ? $ input ->getArgument ('always-remember-me ' ) : false ;
229
+
195
230
$ this ->generateAuthenticatorClass (
196
231
$ securityData ,
197
232
$ input ->getArgument ('authenticator-type ' ),
198
233
$ input ->getArgument ('authenticator-class ' ),
199
234
$ input ->hasArgument ('user-class ' ) ? $ input ->getArgument ('user-class ' ) : null ,
200
- $ input ->hasArgument ('username-field ' ) ? $ input ->getArgument ('username-field ' ) : null
235
+ $ input ->hasArgument ('username-field ' ) ? $ input ->getArgument ('username-field ' ) : null ,
236
+ $ supportRememberMe ,
201
237
);
202
238
203
239
// update security.yaml with guard config
@@ -215,7 +251,9 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
215
251
$ input ->getOption ('firewall-name ' ),
216
252
$ entryPoint ,
217
253
$ input ->getArgument ('authenticator-class ' ),
218
- $ input ->hasArgument ('logout-setup ' ) ? $ input ->getArgument ('logout-setup ' ) : false
254
+ $ input ->hasArgument ('logout-setup ' ) ? $ input ->getArgument ('logout-setup ' ) : false ,
255
+ $ supportRememberMe ,
256
+ $ alwaysRememberMe
219
257
);
220
258
$ generator ->dumpFile ($ path , $ newYaml );
221
259
$ securityYamlUpdated = true ;
@@ -226,7 +264,9 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
226
264
$ this ->generateFormLoginFiles (
227
265
$ input ->getArgument ('controller-class ' ),
228
266
$ input ->getArgument ('username-field ' ),
229
- $ input ->getArgument ('logout-setup ' )
267
+ $ input ->getArgument ('logout-setup ' ),
268
+ $ supportRememberMe ,
269
+ $ alwaysRememberMe ,
230
270
);
231
271
}
232
272
@@ -241,12 +281,14 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
241
281
$ input ->getArgument ('authenticator-class ' ),
242
282
$ securityData ,
243
283
$ input ->hasArgument ('user-class ' ) ? $ input ->getArgument ('user-class ' ) : null ,
244
- $ input ->hasArgument ('logout-setup ' ) ? $ input ->getArgument ('logout-setup ' ) : false
284
+ $ input ->hasArgument ('logout-setup ' ) ? $ input ->getArgument ('logout-setup ' ) : false ,
285
+ $ supportRememberMe ,
286
+ $ alwaysRememberMe
245
287
)
246
288
);
247
289
}
248
290
249
- private function generateAuthenticatorClass (array $ securityData , string $ authenticatorType , string $ authenticatorClass , $ userClass , $ userNameField ): void
291
+ private function generateAuthenticatorClass (array $ securityData , string $ authenticatorType , string $ authenticatorClass , $ userClass , $ userNameField, bool $ supportRememberMe ): void
250
292
{
251
293
$ useStatements = new UseStatementGenerator ([
252
294
Request::class,
@@ -288,6 +330,10 @@ private function generateAuthenticatorClass(array $securityData, string $authent
288
330
$ useStatements ->addUseStatement (LegacySecurity::class);
289
331
}
290
332
333
+ if ($ supportRememberMe ) {
334
+ $ useStatements ->addUseStatement (RememberMeBadge::class);
335
+ }
336
+
291
337
$ userClassNameDetails = $ this ->generator ->createClassNameDetails (
292
338
'\\' .$ userClass ,
293
339
'Entity \\'
@@ -305,11 +351,12 @@ private function generateAuthenticatorClass(array $securityData, string $authent
305
351
'username_field_var ' => Str::asLowerCamelCase ($ userNameField ),
306
352
'user_needs_encoder ' => $ this ->userClassHasEncoder ($ securityData , $ userClass ),
307
353
'user_is_entity ' => $ this ->doctrineHelper ->isClassAMappedEntity ($ userClass ),
354
+ 'remember_me_badge ' => $ supportRememberMe ,
308
355
]
309
356
);
310
357
}
311
358
312
- private function generateFormLoginFiles (string $ controllerClass , string $ userNameField , bool $ logoutSetup ): void
359
+ private function generateFormLoginFiles (string $ controllerClass , string $ userNameField , bool $ logoutSetup, bool $ supportRememberMe , bool $ alwaysRememberMe ): void
313
360
{
314
361
$ controllerClassNameDetails = $ this ->generator ->createClassNameDetails (
315
362
$ controllerClass ,
@@ -362,11 +409,13 @@ private function generateFormLoginFiles(string $controllerClass, string $userNam
362
409
'username_is_email ' => false !== stripos ($ userNameField , 'email ' ),
363
410
'username_label ' => ucfirst (Str::asHumanWords ($ userNameField )),
364
411
'logout_setup ' => $ logoutSetup ,
412
+ 'support_remember_me ' => $ supportRememberMe ,
413
+ 'always_remember_me ' => $ alwaysRememberMe ,
365
414
]
366
415
);
367
416
}
368
417
369
- private function generateNextMessage (bool $ securityYamlUpdated , string $ authenticatorType , string $ authenticatorClass , array $ securityData , $ userClass , bool $ logoutSetup ): array
418
+ private function generateNextMessage (bool $ securityYamlUpdated , string $ authenticatorType , string $ authenticatorClass , array $ securityData , $ userClass , bool $ logoutSetup, bool $ supportRememberMe , bool $ alwaysRememberMe ): array
370
419
{
371
420
$ nextTexts = ['Next: ' ];
372
421
$ nextTexts [] = '- Customize your new authenticator. ' ;
@@ -377,7 +426,9 @@ private function generateNextMessage(bool $securityYamlUpdated, string $authenti
377
426
'main ' ,
378
427
null ,
379
428
$ authenticatorClass ,
380
- $ logoutSetup
429
+ $ logoutSetup ,
430
+ $ supportRememberMe ,
431
+ $ alwaysRememberMe
381
432
);
382
433
$ nextTexts [] = "- Your <info>security.yaml</info> could not be updated automatically. You'll need to add the following config manually: \n\n" .$ yamlExample ;
383
434
}
0 commit comments