Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
53.85% |
7 / 13 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
| AuthenticationResultTrait | |
53.85% |
7 / 13 |
|
50.00% |
1 / 2 |
3.88 | |
0.00% |
0 / 1 |
| onAuthenticationSuccess | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
6 | |||
| onAuthenticationFailure | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
1 | |||
| getSuccessRedirectUrl | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| getFailureRedirectUrl | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace App\Security; |
| 6 | |
| 7 | use Symfony\Component\HttpFoundation\RedirectResponse; |
| 8 | use Symfony\Component\HttpFoundation\Request; |
| 9 | use Symfony\Component\HttpFoundation\Session\Session; |
| 10 | use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; |
| 11 | use Symfony\Component\Security\Core\Exception\AuthenticationException; |
| 12 | |
| 13 | trait AuthenticationResultTrait |
| 14 | { |
| 15 | public function onAuthenticationSuccess( |
| 16 | Request $request, |
| 17 | TokenInterface $token, |
| 18 | string $firewallName |
| 19 | ): RedirectResponse |
| 20 | { |
| 21 | if ($targetPath = $this->getTargetPath( |
| 22 | $request->getSession(), |
| 23 | $firewallName |
| 24 | ) |
| 25 | ) { |
| 26 | return new RedirectResponse($targetPath); |
| 27 | } |
| 28 | |
| 29 | return new RedirectResponse($this->getSuccessRedirectUrl()); |
| 30 | } |
| 31 | |
| 32 | public function onAuthenticationFailure( |
| 33 | Request $request, |
| 34 | AuthenticationException $exception, |
| 35 | ): RedirectResponse |
| 36 | { |
| 37 | $message = strtr( |
| 38 | $exception->getMessageKey(), |
| 39 | $exception->getMessageData() |
| 40 | ); |
| 41 | |
| 42 | /** |
| 43 | * @var Session $session |
| 44 | */ |
| 45 | $session = $request->getSession(); |
| 46 | $session->getFlashBag()->add('danger', $message); |
| 47 | |
| 48 | return new RedirectResponse($this->getFailureRedirectUrl()); |
| 49 | } |
| 50 | |
| 51 | abstract private function getSuccessRedirectUrl(): string; |
| 52 | |
| 53 | abstract private function getFailureRedirectUrl(): string; |
| 54 | } |