|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Knp\RadBundle\Security\Voter; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface; |
|
6
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; |
|
7
|
|
|
use Symfony\Component\Security\Core\User\EquatableInterface; |
|
8
|
|
|
use Knp\RadBundle\Security\OwnerInterface; |
|
9
|
|
|
use Knp\RadBundle\Security\OwnableInterface; |
|
10
|
|
|
use Symfony\Component\Security\Core\User\UserInterface; |
|
11
|
|
|
|
|
12
|
|
|
class IsOwnerVoter implements VoterInterface |
|
13
|
|
|
{ |
|
14
|
|
|
const IS_OWNER = 'IS_OWNER'; |
|
15
|
|
|
|
|
16
|
|
|
public function supportsAttribute($attribute) |
|
17
|
|
|
{ |
|
18
|
|
|
return self::IS_OWNER === $attribute; |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
public function supportsClass($class) |
|
22
|
|
|
{ |
|
23
|
|
|
if (is_object($class)) { |
|
24
|
|
|
$refl = new \ReflectionObject($class); |
|
25
|
|
|
|
|
26
|
|
|
return $refl->implementsInterface('Knp\RadBundle\Security\OwnableInterface'); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
return false; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function vote(TokenInterface $token, $object, array $attributes) |
|
33
|
|
|
{ |
|
34
|
|
|
foreach ($attributes as $attribute) { |
|
35
|
|
|
if (!$this->supportsAttribute($attribute)) { |
|
36
|
|
|
continue; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
if (!$this->supportsClass($object)) { |
|
|
|
|
|
|
40
|
|
|
return self::ACCESS_ABSTAIN; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
if (!$token->getUser() instanceof OwnerInterface) { |
|
44
|
|
|
return self::ACCESS_ABSTAIN; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
if ($this->isOwner($token->getUser(), $object)) { |
|
|
|
|
|
|
48
|
|
|
return self::ACCESS_GRANTED; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
return self::ACCESS_DENIED; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
return self::ACCESS_ABSTAIN; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
private function isOwner(OwnerInterface $owner, OwnableInterface $ownable) |
|
58
|
|
|
{ |
|
59
|
|
|
if ($ownable->getOwner() instanceof UserInterface && $owner instanceof EquatableInterface) { |
|
60
|
|
|
return $owner->isEqualTo($ownable->getOwner()); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
return $ownable->getOwner() === $owner; |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: