Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 26 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
LoginFormAuthenticator | |
0.00% |
0 / 26 |
|
0.00% |
0 / 4 |
56 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
authenticate | |
0.00% |
0 / 19 |
|
0.00% |
0 / 1 |
12 | |||
onAuthenticationSuccess | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
getLoginUrl | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | |
3 | namespace App\Security; |
4 | |
5 | use App\Exception\UserNotVerifiedException; |
6 | use App\Repository\UserRepository; |
7 | use Symfony\Component\HttpFoundation\RedirectResponse; |
8 | use Symfony\Component\HttpFoundation\Request; |
9 | use Symfony\Component\HttpFoundation\Response; |
10 | use Symfony\Component\Routing\RouterInterface; |
11 | use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; |
12 | use Symfony\Component\Security\Core\Exception\CustomUserMessageAuthenticationException; |
13 | use Symfony\Component\Security\Core\Exception\UserNotFoundException; |
14 | use Symfony\Component\Security\Core\Security; |
15 | use Symfony\Component\Security\Http\Authenticator\AbstractLoginFormAuthenticator; |
16 | use Symfony\Component\Security\Http\Authenticator\Passport\Badge\CsrfTokenBadge; |
17 | use Symfony\Component\Security\Http\Authenticator\Passport\Badge\RememberMeBadge; |
18 | use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge; |
19 | use Symfony\Component\Security\Http\Authenticator\Passport\Credentials\PasswordCredentials; |
20 | use Symfony\Component\Security\Http\Authenticator\Passport\Passport; |
21 | use Symfony\Component\Security\Http\Util\TargetPathTrait; |
22 | |
23 | /** |
24 | * |
25 | */ |
26 | class LoginFormAuthenticator extends AbstractLoginFormAuthenticator |
27 | { |
28 | use TargetPathTrait; |
29 | |
30 | public function __construct(private readonly UserRepository $userRepository, private readonly RouterInterface $router) |
31 | { |
32 | // empty body |
33 | } |
34 | |
35 | public function authenticate(Request $request): Passport |
36 | { |
37 | $username = $request->request->get(key: 'username'); |
38 | $password = $request->request->get(key: 'password'); |
39 | $csrfToken = $request->request->get(key: '_csrf_token'); |
40 | |
41 | $request->getSession()->set(name: Security::LAST_USERNAME, value: $username); |
42 | |
43 | return new Passport( |
44 | userBadge: new UserBadge(userIdentifier: $username, userLoader: function ($username) { |
45 | $user = $this->userRepository->findOneBy(['username' => $username]); |
46 | |
47 | if (!$user) { |
48 | $user = $this->userRepository->findOneBy(['email' => $username]); |
49 | } |
50 | |
51 | if (!$user) { |
52 | throw new UserNotFoundException(); |
53 | } |
54 | |
55 | return $user; |
56 | }), |
57 | |
58 | credentials: new PasswordCredentials(password: $password), |
59 | badges: [ |
60 | new CsrfTokenBadge(csrfTokenId: 'authenticate', csrfToken: $csrfToken), |
61 | new RememberMeBadge(), |
62 | ] |
63 | ); |
64 | } |
65 | |
66 | public function onAuthenticationSuccess(Request $request, TokenInterface $token, string $firewallName): ?Response |
67 | { |
68 | if ($target = $this->getTargetPath(session: $request->getSession(), firewallName: $firewallName)) { |
69 | return new RedirectResponse(url: $target); |
70 | } |
71 | |
72 | return new RedirectResponse( |
73 | url: $this->router->generate(name: 'app_main') |
74 | ); |
75 | } |
76 | |
77 | protected function getLoginUrl(Request $request): string |
78 | { |
79 | return $this->router->generate(name: 'security_login'); |
80 | } |
81 | |
82 | } |