Skip to content

Commit eadaddb

Browse files
committed
login ok
1 parent a7d9422 commit eadaddb

File tree

5 files changed

+178
-1
lines changed

5 files changed

+178
-1
lines changed

README.md

+13
Original file line numberDiff line numberDiff line change
@@ -289,3 +289,16 @@ composer require easycorp/easyadmin-bundle
289289
```
290290

291291
```bash
292+
php bin/console make:admin:dashboard
293+
```
294+
295+
## Création de login
296+
297+
```bash
298+
php bin/console make:security:form-login
299+
```
300+
301+
```bash
302+
php bin/console security:hash-password
303+
```
304+

config/packages/security.yaml

+9-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@ security:
1616
main:
1717
lazy: true
1818
provider: app_user_provider
19+
form_login:
20+
login_path: app_login
21+
check_path: app_login
22+
enable_csrf: true
23+
logout:
24+
path: app_logout
25+
# where to redirect after logout
26+
target: admin
1927

2028
# activate different ways to authenticate
2129
# https://symfony.com/doc/current/security.html#the-firewall
@@ -26,7 +34,7 @@ security:
2634
# Easy way to control access for large sections of your site
2735
# Note: Only the *first* access control that matches will be used
2836
access_control:
29-
# - { path: ^/admin, roles: ROLE_ADMIN }
37+
- { path: ^/admin, roles: ROLE_ADMIN }
3038
# - { path: ^/profile, roles: ROLE_USER }
3139

3240
when@test:

src/Controller/SecurityController.php

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace App\Controller;
4+
5+
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
6+
use Symfony\Component\HttpFoundation\Response;
7+
use Symfony\Component\Routing\Attribute\Route;
8+
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
9+
10+
class SecurityController extends AbstractController
11+
{
12+
#[Route(path: '/login', name: 'app_login')]
13+
public function login(AuthenticationUtils $authenticationUtils): Response
14+
{
15+
// get the login error if there is one
16+
$error = $authenticationUtils->getLastAuthenticationError();
17+
18+
// last username entered by the user
19+
$lastUsername = $authenticationUtils->getLastUsername();
20+
21+
return $this->render('security/login.html.twig', [
22+
'last_username' => $lastUsername,
23+
'error' => $error,
24+
]);
25+
}
26+
27+
#[Route(path: '/logout', name: 'app_logout')]
28+
public function logout(): void
29+
{
30+
throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
31+
}
32+
}

templates/security/login.html.twig

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
{% extends 'base.html.twig' %}
2+
3+
{% block title %}Log in!{% endblock %}
4+
5+
{% block body %}
6+
<form method="post">
7+
{% if error %}
8+
<div class="alert alert-danger">{{ error.messageKey|trans(error.messageData, 'security') }}</div>
9+
{% endif %}
10+
11+
{% if app.user %}
12+
<div class="mb-3">
13+
You are logged in as {{ app.user.userIdentifier }}, <a href="{{ path('app_logout') }}">Logout</a>
14+
</div>
15+
{% endif %}
16+
17+
<h1 class="h3 mb-3 font-weight-normal">Please sign in</h1>
18+
<label for="username">Username</label>
19+
<input type="text" value="{{ last_username }}" name="_username" id="username" class="form-control" autocomplete="username" required autofocus>
20+
<label for="password">Password</label>
21+
<input type="password" name="_password" id="password" class="form-control" autocomplete="current-password" required>
22+
23+
<input type="hidden" name="_csrf_token"
24+
value="{{ csrf_token('authenticate') }}"
25+
>
26+
27+
{#
28+
Uncomment this section and add a remember_me option below your firewall to activate remember me functionality.
29+
See https://symfony.com/doc/current/security/remember_me.html
30+
31+
<div class="checkbox mb-3">
32+
<input type="checkbox" name="_remember_me" id="_remember_me">
33+
<label for="_remember_me">Remember me</label>
34+
</div>
35+
#}
36+
37+
<button class="btn btn-lg btn-primary" type="submit">
38+
Sign in
39+
</button>
40+
</form>
41+
{% endblock %}

tests/LoginControllerTest.php

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
namespace App\Tests;
3+
4+
use App\Entity\User;
5+
use Doctrine\ORM\EntityManager;
6+
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
7+
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
8+
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
9+
10+
class LoginControllerTest extends WebTestCase
11+
{
12+
private KernelBrowser $client;
13+
14+
protected function setUp(): void
15+
{
16+
$this->client = static::createClient();
17+
$container = static::getContainer();
18+
$em = $container->get('doctrine.orm.entity_manager');
19+
$userRepository = $em->getRepository(User::class);
20+
21+
// Remove any existing users from the test database
22+
foreach ($userRepository->findAll() as $user) {
23+
$em->remove($user);
24+
}
25+
26+
$em->flush();
27+
28+
// Create a User fixture
29+
/** @var UserPasswordHasherInterface $passwordHasher */
30+
$passwordHasher = $container->get('security.user_password_hasher');
31+
32+
$user = (new User())->setEmail('[email protected]');
33+
$user->setPassword($passwordHasher->hashPassword($user, 'password'));
34+
35+
$em->persist($user);
36+
$em->flush();
37+
}
38+
39+
public function testLogin(): void
40+
{
41+
// Denied - Can't login with invalid email address.
42+
$this->client->request('GET', '/login');
43+
self::assertResponseIsSuccessful();
44+
45+
$this->client->submitForm('Sign in', [
46+
'_username' => '[email protected]',
47+
'_password' => 'password',
48+
]);
49+
50+
self::assertResponseRedirects('/login');
51+
$this->client->followRedirect();
52+
53+
// Ensure we do not reveal if the user exists or not.
54+
self::assertSelectorTextContains('.alert-danger', 'Invalid credentials.');
55+
56+
// Denied - Can't login with invalid password.
57+
$this->client->request('GET', '/login');
58+
self::assertResponseIsSuccessful();
59+
60+
$this->client->submitForm('Sign in', [
61+
'_username' => '[email protected]',
62+
'_password' => 'bad-password',
63+
]);
64+
65+
self::assertResponseRedirects('/login');
66+
$this->client->followRedirect();
67+
68+
// Ensure we do not reveal the user exists but the password is wrong.
69+
self::assertSelectorTextContains('.alert-danger', 'Invalid credentials.');
70+
71+
// Success - Login with valid credentials is allowed.
72+
$this->client->submitForm('Sign in', [
73+
'_username' => '[email protected]',
74+
'_password' => 'password',
75+
]);
76+
77+
self::assertResponseRedirects('/');
78+
$this->client->followRedirect();
79+
80+
self::assertSelectorNotExists('.alert-danger');
81+
self::assertResponseIsSuccessful();
82+
}
83+
}

0 commit comments

Comments
 (0)