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\BadCredentialsException; |
9
|
|
|
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface; |
10
|
|
|
use Symfony\Component\Security\Guard\Authenticator\AbstractFormLoginAuthenticator; |
11
|
|
|
use Symfony\Component\Routing\RouterInterface; |
12
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; |
13
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
14
|
|
|
|
15
|
|
|
class FormAuthenticator extends AbstractFormLoginAuthenticator |
16
|
|
|
{ |
17
|
|
|
/** @var UserPasswordEncoderInterface */ |
18
|
|
|
private $passwordEncoder; |
19
|
|
|
|
20
|
|
|
/** RouterInterface */ |
21
|
|
|
private $router; |
22
|
|
|
|
23
|
|
|
/** array */ |
24
|
|
|
private $redirectRoutes = []; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param UserPasswordEncoderInterface $passwordEncoder |
28
|
|
|
* @param RouterInterface $router |
29
|
|
|
* @param array $redirectRoutes |
30
|
|
|
*/ |
31
|
|
|
public function __construct( |
32
|
|
|
UserPasswordEncoderInterface $passwordEncoder, |
33
|
|
|
RouterInterface $router, |
34
|
|
|
array $redirectRoutes = [] |
35
|
|
|
) { |
36
|
|
|
$this->passwordEncoder = $passwordEncoder; |
37
|
|
|
$this->router = $router; |
38
|
|
|
$this->redirectRoutes = $redirectRoutes; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* {@inheritDoc} |
43
|
|
|
* |
44
|
|
|
* @return FormCredentials |
45
|
|
|
*/ |
46
|
|
|
public function getCredentials(Request $request) |
47
|
|
|
{ |
48
|
|
|
if ($request->getPathInfo() !== $this->getLoginUrl() |
49
|
|
|
|| !$request->isMethod(Request::METHOD_POST)) { |
50
|
|
|
return; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
return new FormCredentials( |
54
|
|
|
$request->request->get('_username'), |
55
|
|
|
$request->request->get('_password') |
56
|
|
|
); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* {@inheritDoc} |
61
|
|
|
*/ |
62
|
|
|
public function getUser($credentials, UserProviderInterface $userProvider) |
63
|
|
|
{ |
64
|
|
|
return $userProvider->loadUserByUsername($credentials->getUsername()); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* {@inheritDoc} |
69
|
|
|
*/ |
70
|
|
|
public function checkCredentials($credentials, UserInterface $user) |
71
|
|
|
{ |
72
|
|
|
$plainPassword = $credentials->getPlainPassword(); |
73
|
|
|
$encoder = $this->passwordEncoder; |
74
|
|
|
|
75
|
|
|
if (!$encoder->isPasswordValid($user, $plainPassword)) { |
76
|
|
|
throw new BadCredentialsException(); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return true; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* {@inheritDoc} |
84
|
|
|
*/ |
85
|
|
|
protected function getLoginUrl() |
86
|
|
|
{ |
87
|
|
|
return $this->router->generate('multi_user_login'); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* {@inheritDoc} |
92
|
|
|
*/ |
93
|
|
|
public function onAuthenticationSuccess(Request $request, TokenInterface $token, $providerKey) |
94
|
|
|
{ |
95
|
|
|
// if the user hit a secure page and start() was called, this was |
96
|
|
|
// the URL they were on, and probably where you want to redirect to |
97
|
|
|
$targetPath = $request->getSession()->get('_security.'.$providerKey.'.target_path'); |
98
|
|
|
|
99
|
|
|
if (!$targetPath) { |
100
|
|
|
$targetPath = $this->getSuccessRedirectUrl($token); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return new RedirectResponse($targetPath); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
protected function getSuccessRedirectUrl(TokenInterface $token) |
107
|
|
|
{ |
108
|
|
|
foreach ($this->redirectRoutes as $class => $route) { |
109
|
|
|
if (get_class($token->getUser()) === $class) { |
110
|
|
|
return $this->router->generate($route['route']); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return $this->getDefaultSuccessRedirectURL(); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
protected function getDefaultSuccessRedirectURL() |
118
|
|
|
{ |
119
|
|
|
return '/'; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|