|
15 | 15 |
|
16 | 16 | use ApiPlatform\Metadata\ApiResource;
|
17 | 17 | use ApiPlatform\Metadata\Get;
|
| 18 | +use ApiPlatform\Metadata\GetCollection; |
| 19 | +use ApiPlatform\Metadata\Link; |
18 | 20 | use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
|
19 | 21 | use ApiPlatform\Metadata\Resource\ResourceMetadataCollection;
|
20 | 22 | use ApiPlatform\Symfony\EventListener\DenyAccessListener;
|
@@ -155,6 +157,92 @@ public function testSecurityComponentNotAvailable(): void
|
155 | 157 | $listener->onSecurity($event);
|
156 | 158 | }
|
157 | 159 |
|
| 160 | + public function testIsGrantedLink(): void |
| 161 | + { |
| 162 | + $request = new Request([], [], ['_api_resource_class' => 'Foo', '_api_operation_name' => 'get_collection']); |
| 163 | + |
| 164 | + $eventProphecy = $this->prophesize(RequestEvent::class); |
| 165 | + $eventProphecy->getRequest()->willReturn($request)->shouldBeCalled(); |
| 166 | + $event = $eventProphecy->reveal(); |
| 167 | + |
| 168 | + $resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataCollectionFactoryInterface::class); |
| 169 | + $resourceMetadataFactoryProphecy->create('Foo')->shouldBeCalled()->willReturn(new ResourceMetadataCollection('Foo', [ |
| 170 | + new ApiResource( |
| 171 | + uriTemplate: '/bars/{barId}/foos', |
| 172 | + operations: [ |
| 173 | + 'get_collection' => new GetCollection(uriVariables: [ |
| 174 | + 'barId' => new Link(toProperty: 'bar', fromClass: 'Bar', security: 'is_granted("some_voter", "bar")'), |
| 175 | + ], ), |
| 176 | + ], |
| 177 | + ), |
| 178 | + ])); |
| 179 | + |
| 180 | + $resourceAccessCheckerProphecy = $this->prophesize(ResourceAccessCheckerInterface::class); |
| 181 | + $resourceAccessCheckerProphecy->isGranted('Bar', 'is_granted("some_voter", "bar")', Argument::type('array'))->willReturn(true)->shouldBeCalled(); |
| 182 | + |
| 183 | + $listener = $this->getListener($resourceMetadataFactoryProphecy->reveal(), $resourceAccessCheckerProphecy->reveal()); |
| 184 | + $listener->onSecurity($event); |
| 185 | + } |
| 186 | + |
| 187 | + public function testIsNotGrantedLink(): void |
| 188 | + { |
| 189 | + $this->expectException(AccessDeniedException::class); |
| 190 | + |
| 191 | + $request = new Request([], [], ['_api_resource_class' => 'Foo', '_api_operation_name' => 'get_collection']); |
| 192 | + |
| 193 | + $eventProphecy = $this->prophesize(RequestEvent::class); |
| 194 | + $eventProphecy->getRequest()->willReturn($request)->shouldBeCalled(); |
| 195 | + $event = $eventProphecy->reveal(); |
| 196 | + |
| 197 | + $resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataCollectionFactoryInterface::class); |
| 198 | + $resourceMetadataFactoryProphecy->create('Foo')->shouldBeCalled()->willReturn(new ResourceMetadataCollection('Foo', [ |
| 199 | + new ApiResource( |
| 200 | + uriTemplate: '/bars/{barId}/foos', |
| 201 | + operations: [ |
| 202 | + 'get_collection' => new GetCollection(uriVariables: [ |
| 203 | + 'barId' => new Link(toProperty: 'bar', fromClass: 'Bar', security: 'is_granted("some_voter", "bar")'), |
| 204 | + ], ), |
| 205 | + ], |
| 206 | + ), |
| 207 | + ])); |
| 208 | + |
| 209 | + $resourceAccessCheckerProphecy = $this->prophesize(ResourceAccessCheckerInterface::class); |
| 210 | + $resourceAccessCheckerProphecy->isGranted('Bar', 'is_granted("some_voter", "bar")', Argument::type('array'))->willReturn(false)->shouldBeCalled(); |
| 211 | + |
| 212 | + $listener = $this->getListener($resourceMetadataFactoryProphecy->reveal(), $resourceAccessCheckerProphecy->reveal()); |
| 213 | + $listener->onSecurity($event); |
| 214 | + } |
| 215 | + |
| 216 | + public function testSecurityMessageLink(): void |
| 217 | + { |
| 218 | + $this->expectException(AccessDeniedException::class); |
| 219 | + $this->expectExceptionMessage('You are not admin.'); |
| 220 | + |
| 221 | + $request = new Request([], [], ['_api_resource_class' => 'Foo', '_api_operation_name' => 'get_collection']); |
| 222 | + |
| 223 | + $eventProphecy = $this->prophesize(RequestEvent::class); |
| 224 | + $eventProphecy->getRequest()->willReturn($request)->shouldBeCalled(); |
| 225 | + $event = $eventProphecy->reveal(); |
| 226 | + |
| 227 | + $resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataCollectionFactoryInterface::class); |
| 228 | + $resourceMetadataFactoryProphecy->create('Foo')->shouldBeCalled()->willReturn(new ResourceMetadataCollection('Foo', [ |
| 229 | + new ApiResource( |
| 230 | + uriTemplate: '/bars/{barId}/foos', |
| 231 | + operations: [ |
| 232 | + 'get_collection' => new GetCollection(uriVariables: [ |
| 233 | + 'barId' => new Link(toProperty: 'bar', fromClass: 'Bar', security: 'is_granted("some_voter", "bar")', securityMessage: 'You are not admin.'), |
| 234 | + ], ), |
| 235 | + ], |
| 236 | + ), |
| 237 | + ])); |
| 238 | + |
| 239 | + $resourceAccessCheckerProphecy = $this->prophesize(ResourceAccessCheckerInterface::class); |
| 240 | + $resourceAccessCheckerProphecy->isGranted('Bar', 'is_granted("some_voter", "bar")', Argument::type('array'))->willReturn(false)->shouldBeCalled(); |
| 241 | + |
| 242 | + $listener = $this->getListener($resourceMetadataFactoryProphecy->reveal(), $resourceAccessCheckerProphecy->reveal()); |
| 243 | + $listener->onSecurity($event); |
| 244 | + } |
| 245 | + |
158 | 246 | private function getListener(ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory, ResourceAccessCheckerInterface $resourceAccessChecker = null): DenyAccessListener
|
159 | 247 | {
|
160 | 248 | if (null === $resourceAccessChecker) {
|
|
0 commit comments