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\Response; |
10
|
|
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
11
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; |
12
|
|
|
use Symfony\Component\Security\Core\Exception\AuthenticationException; |
13
|
|
|
use Symfony\Component\Security\Core\Security; |
14
|
|
|
use Symfony\Component\Security\Http\Authenticator\AbstractAuthenticator; |
15
|
|
|
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\CsrfTokenBadge; |
16
|
|
|
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge; |
17
|
|
|
use Symfony\Component\Security\Http\Authenticator\Passport\Credentials\PasswordCredentials; |
18
|
|
|
use Symfony\Component\Security\Http\Authenticator\Passport\Passport; |
19
|
|
|
|
20
|
|
|
final class RegistrationFormAuthenticator extends AbstractAuthenticator |
21
|
|
|
{ |
22
|
|
|
private UrlGeneratorInterface $urlGenerator; |
23
|
|
|
|
24
|
|
|
public function __construct(UrlGeneratorInterface $urlGenerator) |
25
|
|
|
{ |
26
|
|
|
$this->urlGenerator = $urlGenerator; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function authenticate(Request $request): Passport |
30
|
|
|
{ |
31
|
|
|
$formData = $request->request->all('registration_form'); |
32
|
|
|
$request->getSession()->set(Security::LAST_USERNAME, $formData['username']); |
33
|
|
|
|
34
|
|
|
return new Passport( |
35
|
|
|
new UserBadge($formData['username']), |
36
|
|
|
new PasswordCredentials($formData['password']), |
37
|
|
|
[ |
38
|
|
|
new CsrfTokenBadge('authenticate', $formData['_token']), |
39
|
|
|
] |
40
|
|
|
); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function onAuthenticationSuccess(Request $request, TokenInterface $token, string $firewallName): ?Response |
44
|
|
|
{ |
45
|
|
|
return new RedirectResponse($this->urlGenerator->generate('user_property')); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function supports(Request $request): ?bool |
49
|
|
|
{ |
50
|
|
|
return false; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function onAuthenticationFailure(Request $request, AuthenticationException $exception): ?Response |
54
|
|
|
{ |
55
|
|
|
return new RedirectResponse($this->urlGenerator->generate('security_login')); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|