diff --git a/src/Maker/MakeAuthenticator.php b/src/Maker/MakeAuthenticator.php index ed4f237c5..3e219f612 100644 --- a/src/Maker/MakeAuthenticator.php +++ b/src/Maker/MakeAuthenticator.php @@ -21,6 +21,7 @@ use Symfony\Bundle\MakerBundle\Security\InteractiveSecurityHelper; use Symfony\Bundle\MakerBundle\Security\SecurityConfigUpdater; use Symfony\Bundle\MakerBundle\Security\SecurityControllerBuilder; +use Symfony\Bundle\MakerBundle\Str; use Symfony\Bundle\MakerBundle\Util\ClassSourceManipulator; use Symfony\Bundle\MakerBundle\Util\YamlManipulationFailedException; use Symfony\Bundle\MakerBundle\Util\YamlSourceManipulator; @@ -237,6 +238,7 @@ private function generateAuthenticatorClass(array $securityData, string $authent 'user_fully_qualified_class_name' => trim($userClassNameDetails->getFullName(), '\\'), 'user_class_name' => $userClassNameDetails->getShortName(), 'username_field' => $userNameField, + 'username_field_label' => Str::asHumanWords($userNameField), 'user_needs_encoder' => $this->userClassHasEncoder($securityData, $userClass), 'user_is_entity' => $this->doctrineHelper->isClassAMappedEntity($userClass), ] @@ -281,7 +283,7 @@ private function generateFormLoginFiles(string $controllerClass, string $userNam [ 'username_field' => $userNameField, 'username_is_email' => false !== stripos($userNameField, 'email'), - 'username_label' => ucfirst(implode(' ', preg_split('/(?=[A-Z])/', $userNameField))), + 'username_label' => ucfirst(Str::asHumanWords($userNameField)), ] ); } diff --git a/src/Resources/skeleton/authenticator/LoginFormAuthenticator.tpl.php b/src/Resources/skeleton/authenticator/LoginFormAuthenticator.tpl.php index 1d451a0cf..0843b4b16 100644 --- a/src/Resources/skeleton/authenticator/LoginFormAuthenticator.tpl.php +++ b/src/Resources/skeleton/authenticator/LoginFormAuthenticator.tpl.php @@ -8,6 +8,7 @@ use Symfony\Component\HttpFoundation\Request; use Symfony\Component\Routing\RouterInterface; use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; +use Symfony\Component\Security\Core\Exception\CustomUserMessageAuthenticationException; use Symfony\Component\Security\Core\Exception\InvalidCsrfTokenException; use Symfony\Component\Security\Core\Security; @@ -63,10 +64,17 @@ public function getUser($credentials, UserProviderInterface $userProvider) throw new InvalidCsrfTokenException(); } - entityManager->getRepository($user_class_name::class)->findOneBy(['$username_field' => \$credentials['$username_field']]);\n" + entityManager->getRepository($user_class_name::class)->findOneBy(['$username_field' => \$credentials['$username_field']]);\n" : "// Load / create our user however you need. // You can do this by calling the user provider, or with custom logic here. - return \$userProvider->loadUserByUsername(\$credentials['$username_field']);\n"; ?> + \$user = \$userProvider->loadUserByUsername(\$credentials['$username_field']);\n"; ?> + + if (!$user) { + // fail authentication with a custom error + throw new CustomUserMessageAuthenticationException(' could not be found.'); + } + + return $user; } public function checkCredentials($credentials, UserInterface $user) diff --git a/src/Str.php b/src/Str.php index f92677601..e2e4f30dd 100644 --- a/src/Str.php +++ b/src/Str.php @@ -205,4 +205,9 @@ public static function areClassesAlphabetical(string $class1, string $class2) return $arr1[0] == $arr2[0]; } + + public static function asHumanWords(string $variableName): string + { + return implode(' ', preg_split('/(?=[A-Z])/', $variableName)); + } }