4949use Symfony \Component \Security \Http \Authenticator \AbstractAuthenticator ;
5050use Symfony \Component \Security \Http \Authenticator \AbstractLoginFormAuthenticator ;
5151use Symfony \Component \Security \Http \Authenticator \Passport \Badge \CsrfTokenBadge ;
52+ use Symfony \Component \Security \Http \Authenticator \Passport \Badge \RememberMeBadge ;
5253use Symfony \Component \Security \Http \Authenticator \Passport \Badge \UserBadge ;
5354use Symfony \Component \Security \Http \Authenticator \Passport \Credentials \PasswordCredentials ;
5455use Symfony \Component \Security \Http \Authenticator \Passport \Passport ;
@@ -66,6 +67,9 @@ final class MakeAuthenticator extends AbstractMaker
6667 private const AUTH_TYPE_EMPTY_AUTHENTICATOR = 'empty-authenticator ' ;
6768 private const AUTH_TYPE_FORM_LOGIN = 'form-login ' ;
6869
70+ private const REMEMBER_ME_TYPE_ALWAYS = 'always ' ;
71+ private const REMEMBER_ME_TYPE_CHECKBOX = 'checkbox ' ;
72+
6973 public function __construct (
7074 private FileManager $ fileManager ,
7175 private SecurityConfigUpdater $ configUpdater ,
@@ -184,6 +188,34 @@ function ($answer) {
184188 true
185189 )
186190 );
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+ }
187219 }
188220 }
189221
@@ -192,12 +224,16 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
192224 $ manipulator = new YamlSourceManipulator ($ this ->fileManager ->getFileContents ('config/packages/security.yaml ' ));
193225 $ securityData = $ manipulator ->getData ();
194226
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+
195230 $ this ->generateAuthenticatorClass (
196231 $ securityData ,
197232 $ input ->getArgument ('authenticator-type ' ),
198233 $ input ->getArgument ('authenticator-class ' ),
199234 $ 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 ,
201237 );
202238
203239 // update security.yaml with guard config
@@ -215,7 +251,9 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
215251 $ input ->getOption ('firewall-name ' ),
216252 $ entryPoint ,
217253 $ 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
219257 );
220258 $ generator ->dumpFile ($ path , $ newYaml );
221259 $ securityYamlUpdated = true ;
@@ -226,7 +264,9 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
226264 $ this ->generateFormLoginFiles (
227265 $ input ->getArgument ('controller-class ' ),
228266 $ input ->getArgument ('username-field ' ),
229- $ input ->getArgument ('logout-setup ' )
267+ $ input ->getArgument ('logout-setup ' ),
268+ $ supportRememberMe ,
269+ $ alwaysRememberMe ,
230270 );
231271 }
232272
@@ -241,12 +281,14 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
241281 $ input ->getArgument ('authenticator-class ' ),
242282 $ securityData ,
243283 $ 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
245287 )
246288 );
247289 }
248290
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
250292 {
251293 $ useStatements = new UseStatementGenerator ([
252294 Request::class,
@@ -288,6 +330,10 @@ private function generateAuthenticatorClass(array $securityData, string $authent
288330 $ useStatements ->addUseStatement (LegacySecurity::class);
289331 }
290332
333+ if ($ supportRememberMe ) {
334+ $ useStatements ->addUseStatement (RememberMeBadge::class);
335+ }
336+
291337 $ userClassNameDetails = $ this ->generator ->createClassNameDetails (
292338 '\\' .$ userClass ,
293339 'Entity \\'
@@ -305,11 +351,12 @@ private function generateAuthenticatorClass(array $securityData, string $authent
305351 'username_field_var ' => Str::asLowerCamelCase ($ userNameField ),
306352 'user_needs_encoder ' => $ this ->userClassHasEncoder ($ securityData , $ userClass ),
307353 'user_is_entity ' => $ this ->doctrineHelper ->isClassAMappedEntity ($ userClass ),
354+ 'remember_me_badge ' => $ supportRememberMe ,
308355 ]
309356 );
310357 }
311358
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
313360 {
314361 $ controllerClassNameDetails = $ this ->generator ->createClassNameDetails (
315362 $ controllerClass ,
@@ -362,11 +409,13 @@ private function generateFormLoginFiles(string $controllerClass, string $userNam
362409 'username_is_email ' => false !== stripos ($ userNameField , 'email ' ),
363410 'username_label ' => ucfirst (Str::asHumanWords ($ userNameField )),
364411 'logout_setup ' => $ logoutSetup ,
412+ 'support_remember_me ' => $ supportRememberMe ,
413+ 'always_remember_me ' => $ alwaysRememberMe ,
365414 ]
366415 );
367416 }
368417
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
370419 {
371420 $ nextTexts = ['Next: ' ];
372421 $ nextTexts [] = '- Customize your new authenticator. ' ;
@@ -377,7 +426,9 @@ private function generateNextMessage(bool $securityYamlUpdated, string $authenti
377426 'main ' ,
378427 null ,
379428 $ authenticatorClass ,
380- $ logoutSetup
429+ $ logoutSetup ,
430+ $ supportRememberMe ,
431+ $ alwaysRememberMe
381432 );
382433 $ nextTexts [] = "- Your <info>security.yaml</info> could not be updated automatically. You'll need to add the following config manually: \n\n" .$ yamlExample ;
383434 }
0 commit comments