|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SumoCoders\FrameworkMultiUserBundle\Controller; |
|
4
|
|
|
|
|
5
|
|
|
use SumoCoders\FrameworkMultiUserBundle\Security\FormAuthenticator; |
|
6
|
|
|
use SumoCoders\FrameworkMultiUserBundle\User\Interfaces\User; |
|
7
|
|
|
use Symfony\Bundle\FrameworkBundle\Translation\Translator; |
|
8
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
|
9
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
10
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage; |
|
11
|
|
|
use Symfony\Component\Security\Core\Exception\AuthenticationException; |
|
12
|
|
|
use Symfony\Component\Security\Core\Exception\BadCredentialsException; |
|
13
|
|
|
use Symfony\Component\Security\Core\Exception\UnsupportedUserException; |
|
14
|
|
|
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException; |
|
15
|
|
|
use Symfony\Component\Templating\EngineInterface; |
|
16
|
|
|
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils; |
|
17
|
|
|
|
|
18
|
|
|
final class LoginController |
|
19
|
|
|
{ |
|
20
|
|
|
/** @var EngineInterface */ |
|
21
|
|
|
private $templating; |
|
22
|
|
|
|
|
23
|
|
|
/** @var AuthenticationUtils */ |
|
24
|
|
|
private $authenticationUtils; |
|
25
|
|
|
|
|
26
|
|
|
/** @var FormAuthenticator */ |
|
27
|
|
|
private $formAuthenticator; |
|
28
|
|
|
|
|
29
|
|
|
/** @var TokenStorage */ |
|
30
|
|
|
private $tokenStorage; |
|
31
|
|
|
|
|
32
|
|
|
/** @var Translator */ |
|
33
|
|
|
private $translator; |
|
34
|
|
|
|
|
35
|
|
|
public function __construct( |
|
36
|
|
|
EngineInterface $templating, |
|
37
|
|
|
AuthenticationUtils $authenticationUtils, |
|
38
|
|
|
FormAuthenticator $formAuthenticator, |
|
39
|
|
|
TokenStorage $tokenStorage, |
|
40
|
|
|
Translator $translator |
|
41
|
|
|
) { |
|
42
|
|
|
$this->templating = $templating; |
|
43
|
|
|
$this->authenticationUtils = $authenticationUtils; |
|
44
|
|
|
$this->formAuthenticator = $formAuthenticator; |
|
45
|
|
|
$this->tokenStorage = $tokenStorage; |
|
46
|
|
|
$this->translator = $translator; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function loginAction(): Response |
|
50
|
|
|
{ |
|
51
|
|
|
if ($this->tokenStorage->getToken()->getUser() instanceof User) { |
|
52
|
|
|
return new RedirectResponse( |
|
53
|
|
|
$this->formAuthenticator->getSuccessRedirectUrl($this->tokenStorage->getToken()) |
|
|
|
|
|
|
54
|
|
|
); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
$exception = $this->authenticationUtils->getLastAuthenticationError(); |
|
58
|
|
|
|
|
59
|
|
|
return $this->templating->renderResponse( |
|
60
|
|
|
'SumoCodersFrameworkMultiUserBundle:Login:login.html.twig', |
|
61
|
|
|
[ |
|
62
|
|
|
'error' => $this->getTranslatedErrorMessageFromAuthenticationException($exception), |
|
63
|
|
|
] |
|
64
|
|
|
); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
private function getTranslatedErrorMessageFromAuthenticationException( |
|
68
|
|
|
?AuthenticationException $exception = null |
|
69
|
|
|
): ?string { |
|
70
|
|
|
if ($exception === null) { |
|
71
|
|
|
return null; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
switch (true) { |
|
75
|
|
|
case $exception instanceof UsernameNotFoundException: |
|
76
|
|
|
return $this->translateValidatorMessage('sumocoders.multiuserbundle.login.username_not_found'); |
|
77
|
|
|
case $exception instanceof BadCredentialsException: |
|
78
|
|
|
return $this->translateValidatorMessage('sumocoders.multiuserbundle.login.bad_credentials'); |
|
79
|
|
|
case $exception instanceof UnsupportedUserException: |
|
80
|
|
|
return $this->translateValidatorMessage('sumocoders.multiuserbundle.login.unsupported_user'); |
|
81
|
|
|
default: |
|
82
|
|
|
return $this->translateValidatorMessage('sumocoders.multiuserbundle.something_went_wrong'); |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
private function translateValidatorMessage(string $messageString): string |
|
87
|
|
|
{ |
|
88
|
|
|
return $this->translator->trans( |
|
89
|
|
|
$messageString, |
|
90
|
|
|
[], |
|
91
|
|
|
'validators' |
|
92
|
|
|
); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: