|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Sylius package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Paweł Jędrzejewski |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Sylius\Bundle\UserBundle\Authentication; |
|
13
|
|
|
|
|
14
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
|
15
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
16
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; |
|
17
|
|
|
use Symfony\Component\Security\Http\Authentication\DefaultAuthenticationSuccessHandler; |
|
18
|
|
|
use Symfony\Component\Security\Http\ParameterBagUtils; |
|
19
|
|
|
use Symfony\Component\Security\Http\Util\TargetPathTrait; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @author Arkadiusz Krakowiak <[email protected]> |
|
23
|
|
|
*/ |
|
24
|
|
|
final class AuthenticationSuccessHandler extends DefaultAuthenticationSuccessHandler |
|
25
|
|
|
{ |
|
26
|
|
|
use TargetPathTrait; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* {@inheritdoc} |
|
30
|
|
|
*/ |
|
31
|
|
|
public function onAuthenticationSuccess(Request $request, TokenInterface $token) |
|
32
|
|
|
{ |
|
33
|
|
|
if ($request->isXmlHttpRequest()) { |
|
34
|
|
|
return new JsonResponse(['success' => true, 'username' => $token->getUsername()]); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
return parent::onAuthenticationSuccess($request, $token); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* TODO: Workaround for regression introduced in Symfony 3.3. To be deleted when fixed. |
|
|
|
|
|
|
43
|
|
|
* |
|
44
|
|
|
* @see https://github.com/symfony/symfony/pull/23411 |
|
45
|
|
|
* @see https://github.com/symfony/symfony/pull/23061 |
|
46
|
|
|
* |
|
47
|
|
|
* @internal |
|
48
|
|
|
* |
|
49
|
|
|
* {@inheritdoc} |
|
50
|
|
|
*/ |
|
51
|
|
|
protected function determineTargetUrl(Request $request) |
|
52
|
|
|
{ |
|
53
|
|
|
if ($this->options['always_use_default_target_path']) { |
|
54
|
|
|
return $this->options['default_target_path']; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
if ($targetUrl = ParameterBagUtils::getRequestParameterValue($request, $this->options['target_path_parameter'])) { |
|
58
|
|
|
return $targetUrl; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
if (null !== $this->providerKey && $targetUrl = $this->getTargetPath($request->getSession(), $this->providerKey)) { |
|
|
|
|
|
|
62
|
|
|
$this->removeTargetPath($request->getSession(), $this->providerKey); |
|
|
|
|
|
|
63
|
|
|
|
|
64
|
|
|
return $targetUrl; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
if ($this->options['use_referer'] && ($targetUrl = $request->headers->get('Referer')) && parse_url($targetUrl, PHP_URL_PATH) !== parse_url($this->httpUtils->generateUri($request, $this->options['login_path']), PHP_URL_PATH)) { |
|
68
|
|
|
return $targetUrl; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
return $this->options['default_target_path']; |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|