Skip to content

Commit 5257b67

Browse files
committed
form login : PR review : batch 5
1 parent cd86b3b commit 5257b67

File tree

10 files changed

+28
-108
lines changed

10 files changed

+28
-108
lines changed

src/Maker/MakeAuthenticator.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ private function generateAuthenticatorClass(array $securityData, string $authent
237237
'user_fully_qualified_class_name' => trim($userClassNameDetails->getFullName(), '\\'),
238238
'user_class_name' => $userClassNameDetails->getShortName(),
239239
'username_field' => $userNameField,
240-
'user_needs_encoder' => $this->userNeedsEncoder($securityData, $userClass),
240+
'user_needs_encoder' => $this->userClassHasEncoder($securityData, $userClass),
241241
'user_is_entity' => $this->doctrineHelper->isClassAMappedEntity($userClass),
242242
]
243243
);
@@ -280,6 +280,8 @@ private function generateFormLoginFiles(string $controllerClass, string $userNam
280280
'authenticator/login_form.tpl.php',
281281
[
282282
'username_field' => $userNameField,
283+
'username_is_email' => false !== stripos($userNameField, 'email'),
284+
'username_label' => ucfirst(implode(' ', preg_split('/(?=[A-Z])/', 'oneTwoThree'))),
283285
]
284286
);
285287
}
@@ -300,22 +302,23 @@ private function generateNextMessage(bool $securityYamlUpdated, string $authenti
300302
}
301303

302304
if (self::AUTH_TYPE_FORM_LOGIN === $authenticatorType) {
303-
$nextTexts[] = sprintf('- You must provide a valid redirection in the method <info>%s::onAuthenticationSuccess()</info>.', $authenticatorClass);
304-
$nextTexts[] = '- Review & adapt the login template : <info>/templates/security/login.html.twig</info>.';
305+
$nextTexts[] = sprintf('- Finish the redirect "TODO" in the <info>%s::onAuthenticationSuccess()</info> method.', $authenticatorClass);
305306

306307
if (!$this->doctrineHelper->isClassAMappedEntity($userClass)) {
307-
$nextTexts[] = sprintf('- Review <info>%s::getUser()</info>, if it match your needs.', $authenticatorClass);
308+
$nextTexts[] = sprintf('- Review <info>%s::getUser()</info> to make sure it matches your needs.', $authenticatorClass);
308309
}
309310

310-
if (!$this->userNeedsEncoder($securityData, $userClass)) {
311-
$nextTexts[] = sprintf('- Check user\'s password in <info>%s::checkCredentials()</info>.', $authenticatorClass);
311+
if (!$this->userClassHasEncoder($securityData, $userClass)) {
312+
$nextTexts[] = sprintf('- Check the user\'s password in <info>%s::checkCredentials()</info>.', $authenticatorClass);
312313
}
314+
315+
$nextTexts[] = '- Review & adapt the login template: <info>templates/security/login.html.twig</info>.';
313316
}
314317

315318
return $nextTexts;
316319
}
317320

318-
private function userNeedsEncoder(array $securityData, string $userClass): bool
321+
private function userClassHasEncoder(array $securityData, string $userClass): bool
319322
{
320323
$userNeedsEncoder = false;
321324
if (isset($securityData['security']['encoders']) && $securityData['security']['encoders']) {

src/Resources/skeleton/authenticator/LoginFormAuthenticator.tpl.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public function getUser($credentials, UserProviderInterface $userProvider)
7272
public function checkCredentials($credentials, UserInterface $user)
7373
{
7474
<?= $user_needs_encoder ? "return \$this->passwordEncoder->isPasswordValid(\$user, \$credentials['password']);\n"
75-
: "// Check the users password or other credentials and return true or false
75+
: "// Check the user's password or other credentials and return true or false
7676
// If there are no credentials to check, you can just return true
7777
throw new \Exception('TODO: check the credentials inside '.__FILE__);\n" ?>
7878
}
@@ -83,8 +83,8 @@ public function onAuthenticationSuccess(Request $request, TokenInterface $token,
8383
return new RedirectResponse($targetPath);
8484
}
8585

86-
// e.g. : return new RedirectResponse($this->router->generate('some_route'));
87-
throw new \Exception('TODO: provide a valid redirection inside '.__FILE__);
86+
// For example : return new RedirectResponse($this->router->generate('some_route'));
87+
throw new \Exception('TODO: provide a valid redirect inside '.__FILE__);
8888
}
8989

9090
protected function getLoginUrl()

src/Resources/skeleton/authenticator/login_form.tpl.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
{% endif %}
1010

1111
<h1 class="h3 mb-3 font-weight-normal">Please sign in</h1>
12-
<label for="inputEmail" class="sr-only">Email address</label>
13-
<input type="email" value="{{ last_username }}" name="<?= $username_field; ?>" id="inputEmail" class="form-control" placeholder="Email address" required autofocus>
12+
<label for="input<?= ucfirst($username_field); ?>" class="sr-only"><?= $username_label; ?></label>
13+
<input type="<?= $username_is_email ? 'email' : 'text'; ?>" value="{{ last_username }}" name="<?= $username_field; ?>" id="input<?= ucfirst($username_field); ?>" class="form-control" placeholder="<?= $username_label; ?>" required autofocus>
1414
<label for="inputPassword" class="sr-only">Password</label>
1515
<input type="password" name="password" id="inputPassword" class="form-control" placeholder="Password" required>
1616

tests/fixtures/MakeAuthenticatorLoginFormCustomUsernameField/src/Security/UserProvider.php

Lines changed: 3 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,13 @@
22

33
namespace App\Security;
44

5-
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
65
use Symfony\Component\Security\Core\User\UserInterface;
76
use Symfony\Component\Security\Core\User\UserProviderInterface;
87

98
class UserProvider implements UserProviderInterface
109
{
1110
/**
12-
* Symfony calls this method if you use features like switch_user
13-
* or remember_me.
14-
*
15-
* If you're not using these features, you do not need to implement
16-
* this method.
17-
*
18-
* @return UserInterface
11+
* {@inheritdoc}
1912
*/
2013
public function loadUserByUsername($username)
2114
{
@@ -25,36 +18,16 @@ public function loadUserByUsername($username)
2518
}
2619

2720
/**
28-
* Refreshes the user after being reloaded from the session.
29-
*
30-
* When a user is logged in, at the beginning of each request, the
31-
* User object is loaded from the session and then this method is
32-
* called. Your job is to make sure the user's data is still fresh by,
33-
* for example, re-querying for fresh User data.
34-
*
35-
* If your firewall is "stateless: false" (for a pure API), this
36-
* method is not called.
37-
*
38-
* @return UserInterface
21+
* {@inheritdoc}
3922
*/
4023
public function refreshUser(UserInterface $user)
4124
{
42-
if (!$user instanceof User) {
43-
throw new UnsupportedUserException(sprintf('Invalid user class "%s".', get_class($user)));
44-
}
45-
46-
/* @var User $user */
47-
48-
// Return a User object after making sure its data is "fresh".
49-
// Or throw a UsernameNotFoundException if the user no longer exists.
50-
throw new \Exception('TODO: fill in refreshUser() inside '.__FILE__);
5125
}
5226

5327
/**
54-
* Tells Symfony to use this provider for this User class.
28+
* {@inheritdoc}
5529
*/
5630
public function supportsClass($class)
5731
{
58-
return User::class === $class;
5932
}
6033
}

tests/fixtures/MakeAuthenticatorLoginFormCustomUsernameField/tests/SecurityControllerTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,6 @@ public function testCommand()
4141
);
4242
$client->submit($form);
4343

44-
$this->assertContains('TODO: provide a valid redirection', $client->getResponse()->getContent());
44+
$this->assertContains('TODO: provide a valid redirect', $client->getResponse()->getContent());
4545
}
4646
}

tests/fixtures/MakeAuthenticatorLoginFormExistingController/tests/SecurityControllerTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,6 @@ public function testCommand()
5050
);
5151
$client->submit($form);
5252

53-
$this->assertContains('TODO: provide a valid redirection', $client->getResponse()->getContent());
53+
$this->assertContains('TODO: provide a valid redirect', $client->getResponse()->getContent());
5454
}
5555
}

tests/fixtures/MakeAuthenticatorLoginFormUserEntity/tests/SecurityControllerTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public function testCommand()
5959
);
6060
$client->submit($form);
6161

62-
$this->assertContains('TODO: provide a valid redirection', $client->getResponse()->getContent());
62+
$this->assertContains('TODO: provide a valid redirect', $client->getResponse()->getContent());
6363
$this->assertNotNull($token = $client->getContainer()->get('security.token_storage')->getToken());
6464
$this->assertInstanceOf(User::class, $token->getUser());
6565
}

tests/fixtures/MakeAuthenticatorLoginFormUserNotEntity/src/Security/UserProvider.php

Lines changed: 3 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,13 @@
22

33
namespace App\Security;
44

5-
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
6-
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
75
use Symfony\Component\Security\Core\User\UserInterface;
86
use Symfony\Component\Security\Core\User\UserProviderInterface;
97

108
class UserProvider implements UserProviderInterface
119
{
1210
/**
13-
* Symfony calls this method if you use features like switch_user
14-
* or remember_me.
15-
*
16-
* If you're not using these features, you do not need to implement
17-
* this method.
18-
*
19-
* @return UserInterface
11+
* {@inheritdoc}
2012
*/
2113
public function loadUserByUsername($username)
2214
{
@@ -26,36 +18,16 @@ public function loadUserByUsername($username)
2618
}
2719

2820
/**
29-
* Refreshes the user after being reloaded from the session.
30-
*
31-
* When a user is logged in, at the beginning of each request, the
32-
* User object is loaded from the session and then this method is
33-
* called. Your job is to make sure the user's data is still fresh by,
34-
* for example, re-querying for fresh User data.
35-
*
36-
* If your firewall is "stateless: false" (for a pure API), this
37-
* method is not called.
38-
*
39-
* @return UserInterface
21+
* {@inheritdoc}
4022
*/
4123
public function refreshUser(UserInterface $user)
4224
{
43-
if (!$user instanceof User) {
44-
throw new UnsupportedUserException(sprintf('Invalid user class "%s".', get_class($user)));
45-
}
46-
47-
/* @var User $user */
48-
49-
// Return a User object after making sure its data is "fresh".
50-
// Or throw a UsernameNotFoundException if the user no longer exists.
51-
throw new \Exception('TODO: fill in refreshUser() inside '.__FILE__);
5225
}
5326

5427
/**
55-
* Tells Symfony to use this provider for this User class.
28+
* {@inheritdoc}
5629
*/
5730
public function supportsClass($class)
5831
{
59-
return User::class === $class;
6032
}
6133
}

tests/fixtures/MakeAuthenticatorLoginFormUserNotEntity/tests/SecurityControllerTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,6 @@ public function testCommand()
4141
);
4242
$client->submit($form);
4343

44-
$this->assertContains('TODO: provide a valid redirection', $client->getResponse()->getContent());
44+
$this->assertContains('TODO: provide a valid redirect', $client->getResponse()->getContent());
4545
}
4646
}

tests/fixtures/MakeAuthenticatorLoginFormUserNotEntityNoEncoder/src/Security/UserProvider.php

Lines changed: 3 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,13 @@
22

33
namespace App\Security;
44

5-
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
6-
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
75
use Symfony\Component\Security\Core\User\UserInterface;
86
use Symfony\Component\Security\Core\User\UserProviderInterface;
97

108
class UserProvider implements UserProviderInterface
119
{
1210
/**
13-
* Symfony calls this method if you use features like switch_user
14-
* or remember_me.
15-
*
16-
* If you're not using these features, you do not need to implement
17-
* this method.
18-
*
19-
* @return UserInterface
11+
* {@inheritdoc}
2012
*/
2113
public function loadUserByUsername($username)
2214
{
@@ -26,36 +18,16 @@ public function loadUserByUsername($username)
2618
}
2719

2820
/**
29-
* Refreshes the user after being reloaded from the session.
30-
*
31-
* When a user is logged in, at the beginning of each request, the
32-
* User object is loaded from the session and then this method is
33-
* called. Your job is to make sure the user's data is still fresh by,
34-
* for example, re-querying for fresh User data.
35-
*
36-
* If your firewall is "stateless: false" (for a pure API), this
37-
* method is not called.
38-
*
39-
* @return UserInterface
21+
* {@inheritdoc}
4022
*/
4123
public function refreshUser(UserInterface $user)
4224
{
43-
if (!$user instanceof User) {
44-
throw new UnsupportedUserException(sprintf('Invalid user class "%s".', get_class($user)));
45-
}
46-
47-
/* @var User $user */
48-
49-
// Return a User object after making sure its data is "fresh".
50-
// Or throw a UsernameNotFoundException if the user no longer exists.
51-
throw new \Exception('TODO: fill in refreshUser() inside '.__FILE__);
5225
}
5326

5427
/**
55-
* Tells Symfony to use this provider for this User class.
28+
* {@inheritdoc}
5629
*/
5730
public function supportsClass($class)
5831
{
59-
return User::class === $class;
6032
}
6133
}

0 commit comments

Comments
 (0)