|
0
|
1 |
<?php |
|
|
2 |
|
|
|
3 |
/* |
|
|
4 |
* This file is part of the Symfony package. |
|
|
5 |
* |
|
|
6 |
* (c) Fabien Potencier <fabien@symfony.com> |
|
|
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\Acl\Domain; |
|
|
13 |
|
|
|
14 |
use Symfony\Component\Security\Core\Authentication\Token\AnonymousToken; |
|
|
15 |
|
|
|
16 |
use Symfony\Component\Security\Core\User\UserInterface; |
|
|
17 |
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; |
|
|
18 |
use Symfony\Component\Security\Acl\Model\SecurityIdentityRetrievalStrategyInterface; |
|
|
19 |
use Symfony\Component\Security\Core\Authentication\AuthenticationTrustResolver; |
|
|
20 |
use Symfony\Component\Security\Core\Role\RoleHierarchyInterface; |
|
|
21 |
use Symfony\Component\Security\Core\Authorization\Voter\AuthenticatedVoter; |
|
|
22 |
|
|
|
23 |
/** |
|
|
24 |
* Strategy for retrieving security identities |
|
|
25 |
* |
|
|
26 |
* @author Johannes M. Schmitt <schmittjoh@gmail.com> |
|
|
27 |
*/ |
|
|
28 |
class SecurityIdentityRetrievalStrategy implements SecurityIdentityRetrievalStrategyInterface |
|
|
29 |
{ |
|
|
30 |
private $roleHierarchy; |
|
|
31 |
private $authenticationTrustResolver; |
|
|
32 |
|
|
|
33 |
/** |
|
|
34 |
* Constructor |
|
|
35 |
* |
|
|
36 |
* @param RoleHierarchyInterface $roleHierarchy |
|
|
37 |
* @param AuthenticationTrustResolver $authenticationTrustResolver |
|
|
38 |
* |
|
|
39 |
* @return void |
|
|
40 |
*/ |
|
|
41 |
public function __construct(RoleHierarchyInterface $roleHierarchy, AuthenticationTrustResolver $authenticationTrustResolver) |
|
|
42 |
{ |
|
|
43 |
$this->roleHierarchy = $roleHierarchy; |
|
|
44 |
$this->authenticationTrustResolver = $authenticationTrustResolver; |
|
|
45 |
} |
|
|
46 |
|
|
|
47 |
/** |
|
|
48 |
* {@inheritDoc} |
|
|
49 |
*/ |
|
|
50 |
public function getSecurityIdentities(TokenInterface $token) |
|
|
51 |
{ |
|
|
52 |
$sids = array(); |
|
|
53 |
|
|
|
54 |
// add user security identity |
|
|
55 |
if (!$token instanceof AnonymousToken) { |
|
|
56 |
try { |
|
|
57 |
$sids[] = UserSecurityIdentity::fromToken($token); |
|
|
58 |
} catch (\InvalidArgumentException $invalid) { |
|
|
59 |
// ignore, user has no user security identity |
|
|
60 |
} |
|
|
61 |
} |
|
|
62 |
|
|
|
63 |
// add all reachable roles |
|
|
64 |
foreach ($this->roleHierarchy->getReachableRoles($token->getRoles()) as $role) { |
|
|
65 |
$sids[] = new RoleSecurityIdentity($role); |
|
|
66 |
} |
|
|
67 |
|
|
|
68 |
// add built-in special roles |
|
|
69 |
if ($this->authenticationTrustResolver->isFullFledged($token)) { |
|
|
70 |
$sids[] = new RoleSecurityIdentity(AuthenticatedVoter::IS_AUTHENTICATED_FULLY); |
|
|
71 |
$sids[] = new RoleSecurityIdentity(AuthenticatedVoter::IS_AUTHENTICATED_REMEMBERED); |
|
|
72 |
$sids[] = new RoleSecurityIdentity(AuthenticatedVoter::IS_AUTHENTICATED_ANONYMOUSLY); |
|
|
73 |
} else if ($this->authenticationTrustResolver->isRememberMe($token)) { |
|
|
74 |
$sids[] = new RoleSecurityIdentity(AuthenticatedVoter::IS_AUTHENTICATED_REMEMBERED); |
|
|
75 |
$sids[] = new RoleSecurityIdentity(AuthenticatedVoter::IS_AUTHENTICATED_ANONYMOUSLY); |
|
|
76 |
} else if ($this->authenticationTrustResolver->isAnonymous($token)) { |
|
|
77 |
$sids[] = new RoleSecurityIdentity(AuthenticatedVoter::IS_AUTHENTICATED_ANONYMOUSLY); |
|
|
78 |
} |
|
|
79 |
|
|
|
80 |
return $sids; |
|
|
81 |
} |
|
|
82 |
} |