|
0
|
1 |
<?php |
|
|
2 |
|
|
|
3 |
/* |
|
|
4 |
* Copyright 2010 Johannes M. Schmitt <schmittjoh@gmail.com> |
|
|
5 |
* |
|
|
6 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
|
7 |
* you may not use this file except in compliance with the License. |
|
|
8 |
* You may obtain a copy of the License at |
|
|
9 |
* |
|
|
10 |
* http://www.apache.org/licenses/LICENSE-2.0 |
|
|
11 |
* |
|
|
12 |
* Unless required by applicable law or agreed to in writing, software |
|
|
13 |
* distributed under the License is distributed on an "AS IS" BASIS, |
|
|
14 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
|
15 |
* See the License for the specific language governing permissions and |
|
|
16 |
* limitations under the License. |
|
|
17 |
*/ |
|
|
18 |
|
|
|
19 |
namespace JMS\SecurityExtraBundle\Security\Authorization\AfterInvocation; |
|
|
20 |
|
|
|
21 |
use Symfony\Component\HttpKernel\Log\LoggerInterface; |
|
|
22 |
use Symfony\Component\Security\Acl\Exception\AclNotFoundException; |
|
|
23 |
use Symfony\Component\Security\Acl\Exception\NoAceFoundException; |
|
|
24 |
use Symfony\Component\Security\Acl\Model\SecurityIdentityRetrievalStrategyInterface; |
|
|
25 |
use Symfony\Component\Security\Acl\Model\ObjectIdentityRetrievalStrategyInterface; |
|
|
26 |
use Symfony\Component\Security\Acl\Model\AclProviderInterface; |
|
|
27 |
use Symfony\Component\Security\Acl\Permission\PermissionMapInterface; |
|
|
28 |
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; |
|
|
29 |
use Symfony\Component\Security\Core\Exception\AccessDeniedException; |
|
|
30 |
|
|
|
31 |
/** |
|
|
32 |
* This after invocation provider filters returned objects based on ACLs. |
|
|
33 |
* |
|
|
34 |
* @author Johannes M. Schmitt <schmittjoh@gmail.com> |
|
|
35 |
*/ |
|
|
36 |
class AclAfterInvocationProvider implements AfterInvocationProviderInterface |
|
|
37 |
{ |
|
|
38 |
private $aclProvider; |
|
|
39 |
private $oidRetrievalStrategy; |
|
|
40 |
private $sidRetrievalStrategy; |
|
|
41 |
private $permissionMap; |
|
|
42 |
private $logger; |
|
|
43 |
|
|
|
44 |
public function __construct(AclProviderInterface $aclProvider, ObjectIdentityRetrievalStrategyInterface $oidRetrievalStrategy, SecurityIdentityRetrievalStrategyInterface $sidRetrievalStrategy, PermissionMapInterface $permissionMap, LoggerInterface $logger = null) |
|
|
45 |
{ |
|
|
46 |
$this->aclProvider = $aclProvider; |
|
|
47 |
$this->oidRetrievalStrategy = $oidRetrievalStrategy; |
|
|
48 |
$this->sidRetrievalStrategy = $sidRetrievalStrategy; |
|
|
49 |
$this->permissionMap = $permissionMap; |
|
|
50 |
$this->logger = $logger; |
|
|
51 |
} |
|
|
52 |
|
|
|
53 |
public function decide(TokenInterface $token, $secureObject, array $attributes, $returnedObject) |
|
|
54 |
{ |
|
|
55 |
if (null === $returnedObject) { |
|
|
56 |
if (null !== $this->logger) { |
|
|
57 |
$this->logger->debug('Returned object was null, skipping security check.'); |
|
|
58 |
} |
|
|
59 |
|
|
|
60 |
return null; |
|
|
61 |
} |
|
|
62 |
|
|
|
63 |
foreach ($attributes as $attribute) { |
|
|
64 |
if (!$this->supportsAttribute($attribute)) { |
|
|
65 |
continue; |
|
|
66 |
} |
|
|
67 |
|
|
|
68 |
if (null === $oid = $this->oidRetrievalStrategy->getObjectIdentity($returnedObject)) { |
|
|
69 |
if (null !== $this->logger) { |
|
|
70 |
$this->logger->debug('Returned object was no domain object, skipping security check.'); |
|
|
71 |
} |
|
|
72 |
|
|
|
73 |
return $returnedObject; |
|
|
74 |
} |
|
|
75 |
|
|
|
76 |
$sids = $this->sidRetrievalStrategy->getSecurityIdentities($token); |
|
|
77 |
|
|
|
78 |
try { |
|
|
79 |
$acl = $this->aclProvider->findAcl($oid, $sids); |
|
|
80 |
if ($acl->isGranted($this->permissionMap->getMasks($attribute, $returnedObject), $sids, false)) { |
|
|
81 |
return $returnedObject; |
|
|
82 |
} |
|
|
83 |
|
|
|
84 |
if (null !== $this->logger) { |
|
|
85 |
$this->logger->debug('Token has been denied access for returned object.'); |
|
|
86 |
} |
|
|
87 |
} catch (AclNotFoundException $noAcl) { |
|
|
88 |
throw new AccessDeniedException('No applicable ACL found for domain object.'); |
|
|
89 |
} catch (NoAceFoundException $noAce) { |
|
|
90 |
if (null !== $this->logger) { |
|
|
91 |
$this->logger->debug('No applicable ACE found for the given Token, denying access.'); |
|
|
92 |
} |
|
|
93 |
} |
|
|
94 |
|
|
|
95 |
throw new AccessDeniedException('ACL has denied access for attribute: '.$attribute); |
|
|
96 |
} |
|
|
97 |
|
|
|
98 |
// no attribute was supported |
|
|
99 |
return $returnedObject; |
|
|
100 |
} |
|
|
101 |
|
|
|
102 |
public function supportsAttribute($attribute) |
|
|
103 |
{ |
|
|
104 |
return $this->permissionMap->contains($attribute); |
|
|
105 |
} |
|
|
106 |
|
|
|
107 |
public function supportsClass($className) |
|
|
108 |
{ |
|
|
109 |
return true; |
|
|
110 |
} |
|
|
111 |
} |