1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace spec\Knp\RadBundle\Security\Voter; |
4
|
|
|
|
5
|
|
|
use PhpSpec\ObjectBehavior; |
6
|
|
|
use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface; |
7
|
|
|
|
8
|
|
|
class IsOwnerVoterSpec extends ObjectBehavior |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @param \Symfony\Component\Security\Core\Authentication\Token\TokenInterface $token |
|
|
|
|
12
|
|
|
**/ |
13
|
|
|
function let() |
14
|
|
|
{ |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
function it_should_be_initializable() |
|
|
|
|
18
|
|
|
{ |
19
|
|
|
$this->shouldHaveType('Knp\RadBundle\Security\Voter\IsOwnerVoter'); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
function it_should_only_support_IS_OWNER_attribute() |
|
|
|
|
23
|
|
|
{ |
24
|
|
|
$this->supportsAttribute('IS_OWNER')->shouldReturn(true); |
25
|
|
|
$this->supportsAttribute('IS_SOMETHING_ELSE')->shouldReturn(false); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param Knp\RadBundle\Security\OwnerInterface $user |
30
|
|
|
* @param Knp\RadBundle\Security\OwnableInterface $object |
31
|
|
|
**/ |
32
|
|
|
function it_should_vote_yes_for_owned_object($token, $user, $object) |
|
|
|
|
33
|
|
|
{ |
34
|
|
|
$token->getUser()->willReturn($user); |
35
|
|
|
$object->getOwner()->willReturn($user); |
36
|
|
|
$this->vote($token, $object, array('IS_OWNER'))->shouldReturn(VoterInterface::ACCESS_GRANTED); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param Knp\RadBundle\Security\OwnerInterface $user |
41
|
|
|
* @param Knp\RadBundle\Security\OwnerInterface $otherUser |
42
|
|
|
* @param Knp\RadBundle\Security\OwnableInterface $object |
43
|
|
|
**/ |
44
|
|
|
function it_should_vote_no_for_not_owned_object($token, $user, $otherUser, $object) |
|
|
|
|
45
|
|
|
{ |
46
|
|
|
$token->getUser()->willReturn($user); |
47
|
|
|
$object->getOwner()->willReturn($otherUser); |
48
|
|
|
$this->vote($token, $object, array('IS_OWNER'))->shouldReturn(VoterInterface::ACCESS_DENIED); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param Knp\RadBundle\Security\OwnerInterface $user |
53
|
|
|
* @param stdClass $object |
54
|
|
|
**/ |
55
|
|
|
function it_should_abstain_to_vote_for_not_ownable_object($token, $user, $object) |
|
|
|
|
56
|
|
|
{ |
57
|
|
|
$token->getUser()->willReturn($user); |
58
|
|
|
$this->vote($token, $object, array('IS_OWNER'))->shouldReturn(VoterInterface::ACCESS_ABSTAIN); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param stdClass $user |
63
|
|
|
* @param Knp\RadBundle\Security\OwnableInterface $object |
64
|
|
|
**/ |
65
|
|
|
function it_should_abstain_to_vote_for_not_owner_token_user($token, $user, $object) |
|
|
|
|
66
|
|
|
{ |
67
|
|
|
$token->getUser()->willReturn($user); |
68
|
|
|
$object->getOwner()->willReturn($user); |
69
|
|
|
$this->vote($token, $object, array('IS_OWNER'))->shouldReturn(VoterInterface::ACCESS_ABSTAIN); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param Knp\RadBundle\Security\OwnerInterface $user |
74
|
|
|
* @param Knp\RadBundle\Security\OwnableInterface $object |
75
|
|
|
**/ |
76
|
|
|
function it_should_abstain_to_vote_for_unknown_attribute($token, $user, $object) |
|
|
|
|
77
|
|
|
{ |
78
|
|
|
$token->getUser()->willReturn($user); |
79
|
|
|
$object->getOwner()->willReturn($user); |
80
|
|
|
$this->vote($token, $object, array('IS_TEST'))->shouldReturn(VoterInterface::ACCESS_ABSTAIN); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param Knp\RadBundle\Security\OwnableInterface $object |
85
|
|
|
* @param Symfony\Component\Security\Core\User\UserInterface $equatableUser |
86
|
|
|
* @param stdClass $user |
87
|
|
|
**/ |
88
|
|
|
function it_should_vote_yes_for_equal_owners($token, $user, $object, $equatableUser) |
|
|
|
|
89
|
|
|
{ |
90
|
|
|
$user->implement('Knp\RadBundle\Security\OwnerInterface'); |
91
|
|
|
$user->implement('Symfony\Component\Security\Core\User\EquatableInterface'); |
92
|
|
|
|
93
|
|
|
$token->getUser()->willReturn($user); |
94
|
|
|
$object->getOwner()->willReturn($equatableUser); |
95
|
|
|
$user->isEqualTo($equatableUser->getWrappedObject())->willReturn(true); |
96
|
|
|
|
97
|
|
|
$this->vote($token, $object, array('IS_OWNER'))->shouldReturn(VoterInterface::ACCESS_GRANTED); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param Knp\RadBundle\Security\OwnableInterface $object |
102
|
|
|
* @param Symfony\Component\Security\Core\User\UserInterface $equatableUser |
103
|
|
|
* @param stdClass $user |
104
|
|
|
**/ |
105
|
|
|
function it_should_vote_no_for_non_equal_owners($token, $user, $object, $equatableUser) |
|
|
|
|
106
|
|
|
{ |
107
|
|
|
$user->implement('Knp\RadBundle\Security\OwnerInterface'); |
108
|
|
|
$user->implement('Symfony\Component\Security\Core\User\EquatableInterface'); |
109
|
|
|
|
110
|
|
|
$token->getUser()->willReturn($user); |
111
|
|
|
$object->getOwner()->willReturn($equatableUser); |
112
|
|
|
$user->isEqualTo($equatableUser->getWrappedObject())->willReturn(false); |
113
|
|
|
|
114
|
|
|
$this->vote($token, $object, array('IS_OWNER'))->shouldReturn(VoterInterface::ACCESS_DENIED); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param Knp\RadBundle\Security\OwnableInterface $object |
119
|
|
|
* @param Symfony\Component\Security\Core\User\UserInterface $equatableUser |
120
|
|
|
* @param stdClass $user |
121
|
|
|
**/ |
122
|
|
|
function it_should_use_isEqualTo_if_possible($token, $user, $object, $equatableUser) |
|
|
|
|
123
|
|
|
{ |
124
|
|
|
$user->implement('Knp\RadBundle\Security\OwnerInterface'); |
125
|
|
|
$user->implement('Symfony\Component\Security\Core\User\EquatableInterface'); |
126
|
|
|
|
127
|
|
|
$token->getUser()->willReturn($user); |
128
|
|
|
$object->getOwner()->willReturn($equatableUser); |
129
|
|
|
$user->isEqualTo($equatableUser->getWrappedObject())->shouldBeCalled(); |
130
|
|
|
|
131
|
|
|
$this->vote($token, $object, array('IS_OWNER'))->shouldReturn(VoterInterface::ACCESS_DENIED); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @param Knp\RadBundle\Security\OwnableInterface $object |
136
|
|
|
* @param Knp\RadBundle\Security\OwnerInterface $nonEquatableUser |
137
|
|
|
* @param stdClass $user |
138
|
|
|
**/ |
139
|
|
|
function it_should_not_use_isEqualTo_if_no_UserInterface($token, $user, $object, $nonEquatableUser) |
|
|
|
|
140
|
|
|
{ |
141
|
|
|
$user->implement('Knp\RadBundle\Security\OwnerInterface'); |
142
|
|
|
$user->implement('Symfony\Component\Security\Core\User\EquatableInterface'); |
143
|
|
|
|
144
|
|
|
$token->getUser()->willReturn($user); |
145
|
|
|
$object->getOwner()->willReturn($nonEquatableUser); |
146
|
|
|
$user->isEqualTo($nonEquatableUser)->shouldNotBeCalled(); |
147
|
|
|
|
148
|
|
|
$this->vote($token, $object, array('IS_OWNER'))->shouldReturn(VoterInterface::ACCESS_DENIED); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @param Knp\RadBundle\Security\OwnableInterface $object |
153
|
|
|
* @param Symfony\Component\Security\Core\User\UserInterface $equatableUser |
154
|
|
|
* @param Knp\RadBundle\Security\OwnerInterface $user |
155
|
|
|
**/ |
156
|
|
|
function it_should_not_use_isEqualTo_if_no_EquatableInterface($token, $user, $object, $equatableUser) |
|
|
|
|
157
|
|
|
{ |
158
|
|
|
$token->getUser()->willReturn($user); |
159
|
|
|
$object->getOwner()->willReturn($equatableUser); |
160
|
|
|
|
161
|
|
|
$this->vote($token, $object, array('IS_OWNER'))->shouldReturn(VoterInterface::ACCESS_DENIED); |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.