Skip to content

Commit 7f9a64b

Browse files
pawaclawczykfabpot
authored andcommitted
[Security] Fixed problem with losing ROLE_PREVIOUS_ADMIN role.
0 parents  commit 7f9a64b

File tree

79 files changed

+5103
-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.

79 files changed

+5103
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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\Core\Authentication;
13+
14+
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
15+
use Symfony\Component\Security\Core\Exception\AuthenticationException;
16+
17+
/**
18+
* AuthenticationManagerInterface is the interface for authentication managers,
19+
* which process Token authentication.
20+
*
21+
* @author Fabien Potencier <[email protected]>
22+
*/
23+
interface AuthenticationManagerInterface
24+
{
25+
/**
26+
* Attempts to authenticate a TokenInterface object.
27+
*
28+
* @param TokenInterface $token The TokenInterface instance to authenticate
29+
*
30+
* @return TokenInterface An authenticated TokenInterface instance, never null
31+
*
32+
* @throws AuthenticationException if the authentication fails
33+
*/
34+
public function authenticate(TokenInterface $token);
35+
}
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
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\Core\Authentication;
13+
14+
use Symfony\Component\Security\Core\Event\AuthenticationFailureEvent;
15+
use Symfony\Component\Security\Core\Event\AuthenticationEvent;
16+
use Symfony\Component\Security\Core\AuthenticationEvents;
17+
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
18+
use Symfony\Component\Security\Core\Exception\AccountStatusException;
19+
use Symfony\Component\Security\Core\Exception\AuthenticationException;
20+
use Symfony\Component\Security\Core\Exception\ProviderNotFoundException;
21+
use Symfony\Component\Security\Core\Authentication\Provider\AuthenticationProviderInterface;
22+
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
23+
24+
/**
25+
* AuthenticationProviderManager uses a list of AuthenticationProviderInterface
26+
* instances to authenticate a Token.
27+
*
28+
* @author Fabien Potencier <[email protected]>
29+
* @author Johannes M. Schmitt <[email protected]>
30+
*/
31+
class AuthenticationProviderManager implements AuthenticationManagerInterface
32+
{
33+
private $providers;
34+
private $eraseCredentials;
35+
private $eventDispatcher;
36+
37+
/**
38+
* Constructor.
39+
*
40+
* @param AuthenticationProviderInterface[] $providers An array of AuthenticationProviderInterface instances
41+
* @param Boolean $eraseCredentials Whether to erase credentials after authentication or not
42+
*
43+
* @throws \InvalidArgumentException
44+
*/
45+
public function __construct(array $providers, $eraseCredentials = true)
46+
{
47+
if (!$providers) {
48+
throw new \InvalidArgumentException('You must at least add one authentication provider.');
49+
}
50+
51+
$this->providers = $providers;
52+
$this->eraseCredentials = (Boolean) $eraseCredentials;
53+
}
54+
55+
public function setEventDispatcher(EventDispatcherInterface $dispatcher)
56+
{
57+
$this->eventDispatcher = $dispatcher;
58+
}
59+
60+
/**
61+
* {@inheritdoc}
62+
*/
63+
public function authenticate(TokenInterface $token)
64+
{
65+
$lastException = null;
66+
$result = null;
67+
68+
foreach ($this->providers as $provider) {
69+
if (!$provider->supports($token)) {
70+
continue;
71+
}
72+
73+
try {
74+
$result = $provider->authenticate($token);
75+
76+
if (null !== $result) {
77+
break;
78+
}
79+
} catch (AccountStatusException $e) {
80+
$e->setToken($token);
81+
82+
throw $e;
83+
} catch (AuthenticationException $e) {
84+
$lastException = $e;
85+
}
86+
}
87+
88+
if (null !== $result) {
89+
if (true === $this->eraseCredentials) {
90+
$result->eraseCredentials();
91+
}
92+
93+
if (null !== $this->eventDispatcher) {
94+
$this->eventDispatcher->dispatch(AuthenticationEvents::AUTHENTICATION_SUCCESS, new AuthenticationEvent($result));
95+
}
96+
97+
return $result;
98+
}
99+
100+
if (null === $lastException) {
101+
$lastException = new ProviderNotFoundException(sprintf('No Authentication Provider found for token of class "%s".', get_class($token)));
102+
}
103+
104+
if (null !== $this->eventDispatcher) {
105+
$this->eventDispatcher->dispatch(AuthenticationEvents::AUTHENTICATION_FAILURE, new AuthenticationFailureEvent($token, $lastException));
106+
}
107+
108+
$lastException->setToken($token);
109+
110+
throw $lastException;
111+
}
112+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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\Core\Authentication;
13+
14+
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
15+
16+
/**
17+
* The default implementation of the authentication trust resolver.
18+
*
19+
* @author Johannes M. Schmitt <[email protected]>
20+
*/
21+
class AuthenticationTrustResolver implements AuthenticationTrustResolverInterface
22+
{
23+
private $anonymousClass;
24+
private $rememberMeClass;
25+
26+
/**
27+
* Constructor
28+
*
29+
* @param string $anonymousClass
30+
* @param string $rememberMeClass
31+
*/
32+
public function __construct($anonymousClass, $rememberMeClass)
33+
{
34+
$this->anonymousClass = $anonymousClass;
35+
$this->rememberMeClass = $rememberMeClass;
36+
}
37+
38+
/**
39+
* {@inheritDoc}
40+
*/
41+
public function isAnonymous(TokenInterface $token = null)
42+
{
43+
if (null === $token) {
44+
return false;
45+
}
46+
47+
return $token instanceof $this->anonymousClass;
48+
}
49+
50+
/**
51+
* {@inheritDoc}
52+
*/
53+
public function isRememberMe(TokenInterface $token = null)
54+
{
55+
if (null === $token) {
56+
return false;
57+
}
58+
59+
return $token instanceof $this->rememberMeClass;
60+
}
61+
62+
/**
63+
* {@inheritDoc}
64+
*/
65+
public function isFullFledged(TokenInterface $token = null)
66+
{
67+
if (null === $token) {
68+
return false;
69+
}
70+
71+
return !$this->isAnonymous($token) && !$this->isRememberMe($token);
72+
}
73+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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\Core\Authentication;
13+
14+
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
15+
16+
/**
17+
* Interface for resolving the authentication status of a given token.
18+
*
19+
* @author Johannes M. Schmitt <[email protected]>
20+
*/
21+
interface AuthenticationTrustResolverInterface
22+
{
23+
/**
24+
* Resolves whether the passed token implementation is authenticated
25+
* anonymously.
26+
*
27+
* If null is passed, the method must return false.
28+
*
29+
* @param TokenInterface $token
30+
*
31+
* @return Boolean
32+
*/
33+
public function isAnonymous(TokenInterface $token = null);
34+
35+
/**
36+
* Resolves whether the passed token implementation is authenticated
37+
* using remember-me capabilities.
38+
*
39+
* @param TokenInterface $token
40+
*
41+
* @return Boolean
42+
*/
43+
public function isRememberMe(TokenInterface $token = null);
44+
45+
/**
46+
* Resolves whether the passed token implementation is fully authenticated.
47+
*
48+
* @param TokenInterface $token
49+
*
50+
* @return Boolean
51+
*/
52+
public function isFullFledged(TokenInterface $token = null);
53+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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\Core\Authentication\Provider;
13+
14+
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
15+
use Symfony\Component\Security\Core\Exception\BadCredentialsException;
16+
use Symfony\Component\Security\Core\Authentication\Token\AnonymousToken;
17+
18+
/**
19+
* AnonymousAuthenticationProvider validates AnonymousToken instances.
20+
*
21+
* @author Fabien Potencier <[email protected]>
22+
*/
23+
class AnonymousAuthenticationProvider implements AuthenticationProviderInterface
24+
{
25+
private $key;
26+
27+
/**
28+
* Constructor.
29+
*
30+
* @param string $key The key shared with the authentication token
31+
*/
32+
public function __construct($key)
33+
{
34+
$this->key = $key;
35+
}
36+
37+
/**
38+
* {@inheritdoc}
39+
*/
40+
public function authenticate(TokenInterface $token)
41+
{
42+
if (!$this->supports($token)) {
43+
return null;
44+
}
45+
46+
if ($this->key !== $token->getKey()) {
47+
throw new BadCredentialsException('The Token does not contain the expected key.');
48+
}
49+
50+
return $token;
51+
}
52+
53+
/**
54+
* {@inheritdoc}
55+
*/
56+
public function supports(TokenInterface $token)
57+
{
58+
return $token instanceof AnonymousToken;
59+
}
60+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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\Core\Authentication\Provider;
13+
14+
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
15+
use Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface;
16+
17+
/**
18+
* AuthenticationProviderInterface is the interface for all authentication
19+
* providers.
20+
*
21+
* Concrete implementations processes specific Token instances.
22+
*
23+
* @author Fabien Potencier <[email protected]>
24+
*/
25+
interface AuthenticationProviderInterface extends AuthenticationManagerInterface
26+
{
27+
/**
28+
* Checks whether this provider supports the given token.
29+
*
30+
* @param TokenInterface $token A TokenInterface instance
31+
*
32+
* @return Boolean true if the implementation supports the Token, false otherwise
33+
*/
34+
public function supports(TokenInterface $token);
35+
}

0 commit comments

Comments
 (0)