|
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\Voter; |
|
|
13 |
|
|
|
14 |
use Symfony\Component\HttpKernel\Log\LoggerInterface; |
|
|
15 |
use Symfony\Component\Security\Acl\Domain\ObjectIdentity; |
|
|
16 |
use Symfony\Component\Security\Acl\Domain\RoleSecurityIdentity; |
|
|
17 |
use Symfony\Component\Security\Acl\Domain\UserSecurityIdentity; |
|
|
18 |
use Symfony\Component\Security\Acl\Exception\NoAceFoundException; |
|
|
19 |
use Symfony\Component\Security\Acl\Exception\AclNotFoundException; |
|
|
20 |
use Symfony\Component\Security\Acl\Model\AclProviderInterface; |
|
|
21 |
use Symfony\Component\Security\Acl\Model\ObjectIdentityInterface; |
|
|
22 |
use Symfony\Component\Security\Acl\Permission\PermissionMapInterface; |
|
|
23 |
use Symfony\Component\Security\Acl\Model\SecurityIdentityRetrievalStrategyInterface; |
|
|
24 |
use Symfony\Component\Security\Acl\Model\ObjectIdentityRetrievalStrategyInterface; |
|
|
25 |
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; |
|
|
26 |
use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface; |
|
|
27 |
use Symfony\Component\Security\Core\Role\RoleHierarchyInterface; |
|
|
28 |
|
|
|
29 |
/** |
|
|
30 |
* This voter can be used as a base class for implementing your own permissions. |
|
|
31 |
* |
|
|
32 |
* @author Johannes M. Schmitt <schmittjoh@gmail.com> |
|
|
33 |
*/ |
|
|
34 |
class AclVoter implements VoterInterface |
|
|
35 |
{ |
|
|
36 |
private $aclProvider; |
|
|
37 |
private $permissionMap; |
|
|
38 |
private $objectIdentityRetrievalStrategy; |
|
|
39 |
private $securityIdentityRetrievalStrategy; |
|
|
40 |
private $allowIfObjectIdentityUnavailable; |
|
|
41 |
private $logger; |
|
|
42 |
|
|
|
43 |
public function __construct(AclProviderInterface $aclProvider, ObjectIdentityRetrievalStrategyInterface $oidRetrievalStrategy, SecurityIdentityRetrievalStrategyInterface $sidRetrievalStrategy, PermissionMapInterface $permissionMap, LoggerInterface $logger = null, $allowIfObjectIdentityUnavailable = true) |
|
|
44 |
{ |
|
|
45 |
$this->aclProvider = $aclProvider; |
|
|
46 |
$this->permissionMap = $permissionMap; |
|
|
47 |
$this->objectIdentityRetrievalStrategy = $oidRetrievalStrategy; |
|
|
48 |
$this->securityIdentityRetrievalStrategy = $sidRetrievalStrategy; |
|
|
49 |
$this->logger = $logger; |
|
|
50 |
$this->allowIfObjectIdentityUnavailable = $allowIfObjectIdentityUnavailable; |
|
|
51 |
} |
|
|
52 |
|
|
|
53 |
public function supportsAttribute($attribute) |
|
|
54 |
{ |
|
|
55 |
return $this->permissionMap->contains($attribute); |
|
|
56 |
} |
|
|
57 |
|
|
|
58 |
public function vote(TokenInterface $token, $object, array $attributes) |
|
|
59 |
{ |
|
|
60 |
foreach ($attributes as $attribute) { |
|
|
61 |
if (null === $masks = $this->permissionMap->getMasks($attribute, $object)) { |
|
|
62 |
continue; |
|
|
63 |
} |
|
|
64 |
|
|
|
65 |
if (null === $object) { |
|
|
66 |
if (null !== $this->logger) { |
|
|
67 |
$this->logger->debug(sprintf('Object identity unavailable. Voting to %s', $this->allowIfObjectIdentityUnavailable? 'grant access' : 'abstain')); |
|
|
68 |
} |
|
|
69 |
|
|
|
70 |
return $this->allowIfObjectIdentityUnavailable ? self::ACCESS_GRANTED : self::ACCESS_ABSTAIN; |
|
|
71 |
} else if ($object instanceof FieldVote) { |
|
|
72 |
$field = $object->getField(); |
|
|
73 |
$object = $object->getDomainObject(); |
|
|
74 |
} else { |
|
|
75 |
$field = null; |
|
|
76 |
} |
|
|
77 |
|
|
|
78 |
if ($object instanceof ObjectIdentityInterface) { |
|
|
79 |
$oid = $object; |
|
|
80 |
} else if (null === $oid = $this->objectIdentityRetrievalStrategy->getObjectIdentity($object)) { |
|
|
81 |
if (null !== $this->logger) { |
|
|
82 |
$this->logger->debug(sprintf('Object identity unavailable. Voting to %s', $this->allowIfObjectIdentityUnavailable? 'grant access' : 'abstain')); |
|
|
83 |
} |
|
|
84 |
|
|
|
85 |
return $this->allowIfObjectIdentityUnavailable ? self::ACCESS_GRANTED : self::ACCESS_ABSTAIN; |
|
|
86 |
} |
|
|
87 |
|
|
|
88 |
if (!$this->supportsClass($oid->getType())) { |
|
|
89 |
return self::ACCESS_ABSTAIN; |
|
|
90 |
} |
|
|
91 |
|
|
|
92 |
$sids = $this->securityIdentityRetrievalStrategy->getSecurityIdentities($token); |
|
|
93 |
|
|
|
94 |
try { |
|
|
95 |
$acl = $this->aclProvider->findAcl($oid, $sids); |
|
|
96 |
|
|
|
97 |
if (null === $field && $acl->isGranted($masks, $sids, false)) { |
|
|
98 |
if (null !== $this->logger) { |
|
|
99 |
$this->logger->debug('ACL found, permission granted. Voting to grant access'); |
|
|
100 |
} |
|
|
101 |
|
|
|
102 |
return self::ACCESS_GRANTED; |
|
|
103 |
} else if (null !== $field && $acl->isFieldGranted($field, $masks, $sids, false)) { |
|
|
104 |
if (null !== $this->logger) { |
|
|
105 |
$this->logger->debug('ACL found, permission granted. Voting to grant access'); |
|
|
106 |
} |
|
|
107 |
|
|
|
108 |
return self::ACCESS_GRANTED; |
|
|
109 |
} |
|
|
110 |
|
|
|
111 |
if (null !== $this->logger) { |
|
|
112 |
$this->logger->debug('ACL found, insufficient permissions. Voting to deny access.'); |
|
|
113 |
} |
|
|
114 |
|
|
|
115 |
return self::ACCESS_DENIED; |
|
|
116 |
} catch (AclNotFoundException $noAcl) { |
|
|
117 |
if (null !== $this->logger) { |
|
|
118 |
$this->logger->debug('No ACL found for the object identity. Voting to deny access.'); |
|
|
119 |
} |
|
|
120 |
|
|
|
121 |
return self::ACCESS_DENIED; |
|
|
122 |
} catch (NoAceFoundException $noAce) { |
|
|
123 |
if (null !== $this->logger) { |
|
|
124 |
$this->logger->debug('ACL found, no ACE applicable. Voting to deny access.'); |
|
|
125 |
} |
|
|
126 |
|
|
|
127 |
return self::ACCESS_DENIED; |
|
|
128 |
} |
|
|
129 |
} |
|
|
130 |
|
|
|
131 |
// no attribute was supported |
|
|
132 |
return self::ACCESS_ABSTAIN; |
|
|
133 |
} |
|
|
134 |
|
|
|
135 |
/** |
|
|
136 |
* You can override this method when writing a voter for a specific domain |
|
|
137 |
* class. |
|
|
138 |
* |
|
|
139 |
* @param string $class The class name |
|
|
140 |
* |
|
|
141 |
* @return Boolean |
|
|
142 |
*/ |
|
|
143 |
public function supportsClass($class) |
|
|
144 |
{ |
|
|
145 |
return true; |
|
|
146 |
} |
|
|
147 |
} |