1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SumoCoders\FrameworkMultiUserBundle\Security; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\HttpFoundation\Request; |
6
|
|
|
use Symfony\Component\Security\Core\User\UserProviderInterface; |
7
|
|
|
use Symfony\Component\Security\Core\User\UserInterface; |
8
|
|
|
use Symfony\Component\Security\Core\Exception\AuthenticationException; |
9
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; |
10
|
|
|
use Symfony\Component\Security\Core\Exception\BadCredentialsException; |
11
|
|
|
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface; |
12
|
|
|
use Symfony\Component\Security\Guard\Authenticator\AbstractFormLoginAuthenticator; |
13
|
|
|
|
14
|
|
|
class FormAuthenticator extends AbstractFormLoginAuthenticator |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var UserPasswordEncoderInterface |
18
|
|
|
*/ |
19
|
|
|
private $passwordDecoder; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @param UserPasswordEncoderInterface $passwordDecoder |
23
|
|
|
*/ |
24
|
|
|
public function __construct(UserPasswordEncoderInterface $passwordDecoder) |
25
|
|
|
{ |
26
|
|
|
$this->passwordDecoder = $passwordDecoder; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* {@inheritDoc} |
31
|
|
|
* |
32
|
|
|
* @return FormCredentials |
33
|
|
|
*/ |
34
|
|
|
public function getCredentials(Request $request) |
35
|
|
|
{ |
36
|
|
|
// @todo: there will probably be a better way te determine if the login |
37
|
|
|
// form has been submitted |
38
|
|
|
if (!$request->request->has('_username') |
39
|
|
|
|| !$request->request->has('_password')) { |
40
|
|
|
return; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
return new FormCredentials( |
44
|
|
|
$request->request->get('_username'), |
45
|
|
|
$request->request->get('_password') |
46
|
|
|
); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* {@inheritDoc} |
51
|
|
|
*/ |
52
|
|
|
public function getUser($credentials, UserProviderInterface $userProvider) |
53
|
|
|
{ |
54
|
|
|
return $userProvider->loadUserByUsername($credentials->getUsername()); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* {@inheritDoc} |
59
|
|
|
*/ |
60
|
|
|
public function checkCredentials($credentials, UserInterface $user) |
61
|
|
|
{ |
62
|
|
|
$plainPassword = $credentials->getPlainPassword(); |
63
|
|
|
$encoder = $this->passwordEncoder; |
64
|
|
|
|
65
|
|
|
if (!$encoder->isPasswordValid($user, $plainPassword)) { |
66
|
|
|
throw new BadCredentialsException(); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
protected function getLoginUrl() |
71
|
|
|
{ |
72
|
|
|
return '/login'; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
protected function getDefaultSuccessRedirectURL() |
76
|
|
|
{ |
77
|
|
|
return '/success'; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|