Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/Maker/MakeAuthenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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),
]
Expand Down Expand Up @@ -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)),
]
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
<?= $user_needs_encoder ? "use Symfony\\Component\\Security\\Core\\Encoder\\UserPasswordEncoderInterface;\n" : null ?>
use Symfony\Component\Security\Core\Exception\InvalidCsrfTokenException;
use Symfony\Component\Security\Core\Security;
Expand Down Expand Up @@ -63,10 +64,17 @@ public function getUser($credentials, UserProviderInterface $userProvider)
throw new InvalidCsrfTokenException();
}

<?= $user_is_entity ? "return \$this->entityManager->getRepository($user_class_name::class)->findOneBy(['$username_field' => \$credentials['$username_field']]);\n"
<?= $user_is_entity ? "\$user = \$this->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('<?= ucfirst($username_field_label) ?> could not be found.');
}

return $user;
}

public function checkCredentials($credentials, UserInterface $user)
Expand Down
5 changes: 5 additions & 0 deletions src/Str.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}