Skip to content

Commit b6ef994

Browse files
committed
[HttpFoundation] added a way to override the Request class
0 parents  commit b6ef994

File tree

49 files changed

+4312
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+4312
-0
lines changed

AccessMap.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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 Symfony\Component\Security\Http;
13+
14+
use Symfony\Component\HttpFoundation\RequestMatcherInterface;
15+
use Symfony\Component\HttpFoundation\Request;
16+
17+
/**
18+
* AccessMap allows configuration of different access control rules for
19+
* specific parts of the website.
20+
*
21+
* @author Fabien Potencier <[email protected]>
22+
*/
23+
class AccessMap implements AccessMapInterface
24+
{
25+
private $map = array();
26+
27+
/**
28+
* Constructor.
29+
*
30+
* @param RequestMatcherInterface $requestMatcher A RequestMatcherInterface instance
31+
* @param array $roles An array of roles needed to access the resource
32+
* @param string|null $channel The channel to enforce (http, https, or null)
33+
*/
34+
public function add(RequestMatcherInterface $requestMatcher, array $roles = array(), $channel = null)
35+
{
36+
$this->map[] = array($requestMatcher, $roles, $channel);
37+
}
38+
39+
public function getPatterns(Request $request)
40+
{
41+
foreach ($this->map as $elements) {
42+
if (null === $elements[0] || $elements[0]->matches($request)) {
43+
return array($elements[1], $elements[2]);
44+
}
45+
}
46+
47+
return array(null, null);
48+
}
49+
}

AccessMapInterface.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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 Symfony\Component\Security\Http;
13+
14+
use Symfony\Component\HttpFoundation\Request;
15+
16+
/**
17+
* AccessMap allows configuration of different access control rules for
18+
* specific parts of the website.
19+
*
20+
* @author Fabien Potencier <[email protected]>
21+
* @author Kris Wallsmith <[email protected]>
22+
*/
23+
interface AccessMapInterface
24+
{
25+
/**
26+
* Returns security attributes and required channel for the supplied request.
27+
*
28+
* @param Request $request The current request
29+
*
30+
* @return array A tuple of security attributes and the required channel
31+
*/
32+
public function getPatterns(Request $request);
33+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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 Symfony\Component\Security\Http\Authentication;
13+
14+
use Symfony\Component\Security\Core\Exception\AuthenticationException;
15+
use Symfony\Component\HttpFoundation\Request;
16+
17+
/**
18+
* Interface for custom authentication failure handlers.
19+
*
20+
* If you want to customize the failure handling process, instead of
21+
* overwriting the respective listener globally, you can set a custom failure
22+
* handler which implements this interface.
23+
*
24+
* @author Johannes M. Schmitt <[email protected]>
25+
*/
26+
interface AuthenticationFailureHandlerInterface
27+
{
28+
/**
29+
* This is called when an interactive authentication attempt fails. This is
30+
* called by authentication listeners inheriting from
31+
* AbstractAuthenticationListener.
32+
*
33+
* @param Request $request
34+
* @param AuthenticationException $exception
35+
*
36+
* @return Response The response to return, never null
37+
*/
38+
public function onAuthenticationFailure(Request $request, AuthenticationException $exception);
39+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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 Symfony\Component\Security\Http\Authentication;
13+
14+
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
15+
use Symfony\Component\HttpFoundation\Request;
16+
17+
/**
18+
* Interface for a custom authentication success handler
19+
*
20+
* If you want to customize the success handling process, instead of
21+
* overwriting the respective listener globally, you can set a custom success
22+
* handler which implements this interface.
23+
*
24+
* @author Johannes M. Schmitt <[email protected]>
25+
*/
26+
interface AuthenticationSuccessHandlerInterface
27+
{
28+
/**
29+
* This is called when an interactive authentication attempt succeeds. This
30+
* is called by authentication listeners inheriting from
31+
* AbstractAuthenticationListener.
32+
*
33+
* @param Request $request
34+
* @param TokenInterface $token
35+
*
36+
* @return Response never null
37+
*/
38+
public function onAuthenticationSuccess(Request $request, TokenInterface $token);
39+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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 Symfony\Component\Security\Http\Authentication;
13+
14+
use Symfony\Component\HttpFoundation\Request;
15+
use Symfony\Component\HttpKernel\HttpKernelInterface;
16+
use Psr\Log\LoggerInterface;
17+
use Symfony\Component\Security\Core\Exception\AuthenticationException;
18+
use Symfony\Component\Security\Core\SecurityContextInterface;
19+
use Symfony\Component\Security\Http\HttpUtils;
20+
21+
/**
22+
* Class with the default authentication failure handling logic.
23+
*
24+
* Can be optionally be extended from by the developer to alter the behaviour
25+
* while keeping the default behaviour.
26+
*
27+
* @author Fabien Potencier <[email protected]>
28+
* @author Johannes M. Schmitt <[email protected]>
29+
* @author Alexander <[email protected]>
30+
*/
31+
class DefaultAuthenticationFailureHandler implements AuthenticationFailureHandlerInterface
32+
{
33+
protected $httpKernel;
34+
protected $httpUtils;
35+
protected $logger;
36+
protected $options;
37+
38+
/**
39+
* Constructor.
40+
*
41+
* @param HttpKernelInterface $httpKernel
42+
* @param HttpUtils $httpUtils
43+
* @param array $options Options for processing a failed authentication attempt.
44+
* @param LoggerInterface $logger Optional logger
45+
*/
46+
public function __construct(HttpKernelInterface $httpKernel, HttpUtils $httpUtils, array $options, LoggerInterface $logger = null)
47+
{
48+
$this->httpKernel = $httpKernel;
49+
$this->httpUtils = $httpUtils;
50+
$this->logger = $logger;
51+
52+
$this->options = array_merge(array(
53+
'failure_path' => null,
54+
'failure_forward' => false,
55+
'login_path' => '/login',
56+
'failure_path_parameter' => '_failure_path'
57+
), $options);
58+
}
59+
60+
/**
61+
* {@inheritDoc}
62+
*/
63+
public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
64+
{
65+
if ($failureUrl = $request->get($this->options['failure_path_parameter'], null, true)) {
66+
$this->options['failure_path'] = $failureUrl;
67+
}
68+
69+
if (null === $this->options['failure_path']) {
70+
$this->options['failure_path'] = $this->options['login_path'];
71+
}
72+
73+
if ($this->options['failure_forward']) {
74+
if (null !== $this->logger) {
75+
$this->logger->debug(sprintf('Forwarding to %s', $this->options['failure_path']));
76+
}
77+
78+
$subRequest = $this->httpUtils->createRequest($request, $this->options['failure_path']);
79+
$subRequest->attributes->set(SecurityContextInterface::AUTHENTICATION_ERROR, $exception);
80+
81+
return $this->httpKernel->handle($subRequest, HttpKernelInterface::SUB_REQUEST);
82+
}
83+
84+
if (null !== $this->logger) {
85+
$this->logger->debug(sprintf('Redirecting to %s', $this->options['failure_path']));
86+
}
87+
88+
$request->getSession()->set(SecurityContextInterface::AUTHENTICATION_ERROR, $exception);
89+
90+
return $this->httpUtils->createRedirectResponse($request, $this->options['failure_path']);
91+
}
92+
}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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 Symfony\Component\Security\Http\Authentication;
13+
14+
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
15+
use Symfony\Component\HttpFoundation\Request;
16+
use Symfony\Component\Security\Http\HttpUtils;
17+
18+
/**
19+
* Class with the default authentication success handling logic.
20+
*
21+
* Can be optionally be extended from by the developer to alter the behaviour
22+
* while keeping the default behaviour.
23+
*
24+
* @author Fabien Potencier <[email protected]>
25+
* @author Johannes M. Schmitt <[email protected]>
26+
* @author Alexander <[email protected]>
27+
*/
28+
class DefaultAuthenticationSuccessHandler implements AuthenticationSuccessHandlerInterface
29+
{
30+
protected $httpUtils;
31+
protected $options;
32+
protected $providerKey;
33+
34+
/**
35+
* Constructor.
36+
*
37+
* @param HttpUtils $httpUtils
38+
* @param array $options Options for processing a successful authentication attempt.
39+
*/
40+
public function __construct(HttpUtils $httpUtils, array $options)
41+
{
42+
$this->httpUtils = $httpUtils;
43+
44+
$this->options = array_merge(array(
45+
'always_use_default_target_path' => false,
46+
'default_target_path' => '/',
47+
'login_path' => '/login',
48+
'target_path_parameter' => '_target_path',
49+
'use_referer' => false,
50+
), $options);
51+
}
52+
53+
/**
54+
* {@inheritDoc}
55+
*/
56+
public function onAuthenticationSuccess(Request $request, TokenInterface $token)
57+
{
58+
return $this->httpUtils->createRedirectResponse($request, $this->determineTargetUrl($request));
59+
}
60+
61+
/**
62+
* Get the provider key.
63+
*
64+
* @return string
65+
*/
66+
public function getProviderKey()
67+
{
68+
return $this->providerKey;
69+
}
70+
71+
/**
72+
* Set the provider key.
73+
*
74+
* @param string $providerKey
75+
*/
76+
public function setProviderKey($providerKey)
77+
{
78+
$this->providerKey = $providerKey;
79+
}
80+
81+
/**
82+
* Builds the target URL according to the defined options.
83+
*
84+
* @param Request $request
85+
*
86+
* @return string
87+
*/
88+
protected function determineTargetUrl(Request $request)
89+
{
90+
if ($this->options['always_use_default_target_path']) {
91+
return $this->options['default_target_path'];
92+
}
93+
94+
if ($targetUrl = $request->get($this->options['target_path_parameter'], null, true)) {
95+
return $targetUrl;
96+
}
97+
98+
if (null !== $this->providerKey && $targetUrl = $request->getSession()->get('_security.'.$this->providerKey.'.target_path')) {
99+
$request->getSession()->remove('_security.'.$this->providerKey.'.target_path');
100+
101+
return $targetUrl;
102+
}
103+
104+
if ($this->options['use_referer'] && ($targetUrl = $request->headers->get('Referer')) && $targetUrl !== $this->httpUtils->generateUri($request, $this->options['login_path'])) {
105+
return $targetUrl;
106+
}
107+
108+
return $this->options['default_target_path'];
109+
}
110+
}

0 commit comments

Comments
 (0)