|
36 | 36 | use Symfony\Component\Console\Input\InputOption;
|
37 | 37 | use Symfony\Component\Console\Question\Question;
|
38 | 38 | use Symfony\Component\Form\Form;
|
| 39 | +use Symfony\Component\HttpFoundation\RedirectResponse; |
| 40 | +use Symfony\Component\HttpFoundation\Request; |
| 41 | +use Symfony\Component\HttpFoundation\Response; |
39 | 42 | use Symfony\Component\HttpKernel\Kernel;
|
| 43 | +use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
| 44 | +use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; |
40 | 45 | use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
|
| 46 | +use Symfony\Component\Security\Core\Exception\InvalidCsrfTokenException; |
| 47 | +use Symfony\Component\Security\Core\Exception\UsernameNotFoundException; |
| 48 | +use Symfony\Component\Security\Core\Security; |
| 49 | +use Symfony\Component\Security\Core\User\UserInterface; |
| 50 | +use Symfony\Component\Security\Core\User\UserProviderInterface; |
| 51 | +use Symfony\Component\Security\Csrf\CsrfToken; |
| 52 | +use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface; |
41 | 53 | use Symfony\Component\Security\Guard\Authenticator\AbstractFormLoginAuthenticator;
|
| 54 | +use Symfony\Component\Security\Guard\PasswordAuthenticatedInterface; |
| 55 | +use Symfony\Component\Security\Http\Authenticator\AbstractLoginFormAuthenticator; |
| 56 | +use Symfony\Component\Security\Http\Authenticator\Passport\Badge\CsrfTokenBadge; |
42 | 57 | use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;
|
| 58 | +use Symfony\Component\Security\Http\Authenticator\Passport\Credentials\PasswordCredentials; |
| 59 | +use Symfony\Component\Security\Http\Authenticator\Passport\Passport; |
| 60 | +use Symfony\Component\Security\Http\Authenticator\Passport\PassportInterface; |
| 61 | +use Symfony\Component\Security\Http\Util\TargetPathTrait; |
43 | 62 | use Symfony\Component\Yaml\Yaml;
|
44 | 63 |
|
45 | 64 | /**
|
@@ -280,68 +299,77 @@ private function generateAuthenticatorClass(array $securityData, string $authent
|
280 | 299 | );
|
281 | 300 |
|
282 | 301 | $useStatements = [
|
283 |
| - \Symfony\Component\HttpFoundation\RedirectResponse::class, |
284 |
| - \Symfony\Component\HttpFoundation\Request::class, |
285 |
| - \Symfony\Component\Routing\Generator\UrlGeneratorInterface::class, |
286 |
| - \Symfony\Component\Security\Core\Authentication\Token\TokenInterface::class, |
287 |
| - \Symfony\Component\Security\Core\Security::class, |
288 |
| - \Symfony\Component\Security\Http\Util\TargetPathTrait::class, |
| 302 | + RedirectResponse::class, |
| 303 | + Request::class, |
| 304 | + UrlGeneratorInterface::class, |
| 305 | + TokenInterface::class, |
| 306 | + Security::class, |
| 307 | + TargetPathTrait::class, |
289 | 308 | ];
|
290 | 309 |
|
291 | 310 | $guardUseStatements = [
|
292 |
| - \Symfony\Component\Security\Core\Exception\InvalidCsrfTokenException::class, |
293 |
| - \Symfony\Component\Security\Core\Exception\UsernameNotFoundException::class, |
294 |
| - \Symfony\Component\Security\Core\User\UserInterface::class, |
295 |
| - \Symfony\Component\Security\Core\User\UserProviderInterface::class, |
296 |
| - \Symfony\Component\Security\Csrf\CsrfToken::class, |
297 |
| - \Symfony\Component\Security\Csrf\CsrfTokenManagerInterface::class, |
298 |
| - \Symfony\Component\Security\Guard\Authenticator\AbstractFormLoginAuthenticator::class, |
| 311 | + InvalidCsrfTokenException::class, |
| 312 | + UsernameNotFoundException::class, |
| 313 | + UserInterface::class, |
| 314 | + UserProviderInterface::class, |
| 315 | + CsrfToken::class, |
| 316 | + CsrfTokenManagerInterface::class, |
| 317 | + AbstractFormLoginAuthenticator::class, |
299 | 318 | ];
|
300 | 319 |
|
301 | 320 | $security53UseStatements = [
|
302 |
| - \Symfony\Component\HttpFoundation\Response::class, |
303 |
| - \Symfony\Component\Security\Http\Authenticator\AbstractLoginFormAuthenticator::class, |
304 |
| - \Symfony\Component\Security\Http\Authenticator\Passport\Badge\CsrfTokenBadge::class, |
305 |
| - \Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge::class, |
306 |
| - \Symfony\Component\Security\Http\Authenticator\Passport\Credentials\PasswordCredentials::class, |
307 |
| - \Symfony\Component\Security\Http\Authenticator\Passport\Passport::class, |
308 |
| - \Symfony\Component\Security\Http\Authenticator\Passport\PassportInterface::class, |
| 321 | + Response::class, |
| 322 | + AbstractLoginFormAuthenticator::class, |
| 323 | + CsrfTokenBadge::class, |
| 324 | + UserBadge::class, |
| 325 | + PasswordCredentials::class, |
| 326 | + Passport::class, |
| 327 | + PassportInterface::class, |
309 | 328 | ];
|
310 | 329 |
|
311 | 330 | $useStatements = array_merge($useStatements, ($this->useSecurity52 ? $security53UseStatements : $guardUseStatements));
|
312 | 331 |
|
313 | 332 | $isEntity = $this->doctrineHelper->isClassAMappedEntity($userClass);
|
314 | 333 | $hasEncoder = $this->userClassHasEncoder($securityData, $userClass);
|
| 334 | + $guardPasswordAuthenticated = $hasEncoder && interface_exists( |
| 335 | + PasswordAuthenticatedInterface::class); |
| 336 | + |
| 337 | + $guardTemplateVariables = [ |
| 338 | + 'user_class_name' => $userClassNameDetails->getShortName(), |
| 339 | + 'user_needs_encoder' => $hasEncoder, |
| 340 | + 'user_is_entity' => $isEntity, |
| 341 | + 'provider_key_type_hint' => $this->providerKeyTypeHint(), |
| 342 | + 'password_authenticated' => $guardPasswordAuthenticated, |
| 343 | + ]; |
315 | 344 |
|
316 |
| - if (!$this->useSecurity52 && $isEntity) { |
317 |
| - $useStatements[] = $userClassNameDetails->getFullName(); |
318 |
| - $useStatements[] = EntityManagerInterface::class; |
319 |
| - } |
| 345 | + $security53TemplateVariables = [ |
| 346 | + 'username_field_var' => Str::asLowerCamelCase($userNameField), |
| 347 | + ]; |
320 | 348 |
|
321 |
| - if (!$this->useSecurity52 && $hasEncoder) { |
322 |
| - $useStatements[] = UserPasswordEncoderInterface::class; |
323 |
| - } |
| 349 | + if (!$this->useSecurity52) { |
| 350 | + if ($isEntity) { |
| 351 | + $useStatements[] = $userClassNameDetails->getFullName(); |
| 352 | + $useStatements[] = EntityManagerInterface::class; |
| 353 | + } |
324 | 354 |
|
325 |
| - $guardPasswordAuthenticated = $hasEncoder && interface_exists(\Symfony\Component\Security\Guard\PasswordAuthenticatedInterface::class); |
| 355 | + if ($hasEncoder) { |
| 356 | + $useStatements[] = UserPasswordEncoderInterface::class; |
| 357 | + } |
326 | 358 |
|
327 |
| - if (!$this->useSecurity52 && $guardPasswordAuthenticated) { |
328 |
| - $useStatements[] = \Symfony\Component\Security\Guard\PasswordAuthenticatedInterface::class; |
| 359 | + if ($guardPasswordAuthenticated) { |
| 360 | + $useStatements[] = PasswordAuthenticatedInterface::class; |
| 361 | + } |
329 | 362 | }
|
330 | 363 |
|
331 | 364 | $this->generator->generateClass(
|
332 | 365 | $authenticatorClass,
|
333 | 366 | sprintf('authenticator/%sLoginFormAuthenticator.tpl.php', $this->useSecurity52 ? 'Security52' : ''),
|
334 |
| - [ |
| 367 | + array_merge([ |
335 | 368 | 'use_statements' => TemplateComponentGenerator::generateUseStatements($useStatements),
|
336 |
| - 'user_class_name' => $userClassNameDetails->getShortName(), |
337 | 369 | 'username_field' => $userNameField,
|
338 | 370 | 'username_field_label' => Str::asHumanWords($userNameField),
|
339 |
| - 'username_field_var' => Str::asLowerCamelCase($userNameField), |
340 |
| - 'user_needs_encoder' => $hasEncoder, |
341 |
| - 'user_is_entity' => $isEntity, |
342 |
| - 'provider_key_type_hint' => $this->providerKeyTypeHint(), |
343 |
| - 'password_authenticated' => $guardPasswordAuthenticated |
344 |
| - ] |
| 371 | + ], ($this->useSecurity52 ? $security53TemplateVariables : $guardTemplateVariables) |
| 372 | + ) |
345 | 373 | );
|
346 | 374 | }
|
347 | 375 |
|
|
0 commit comments