|
22 | 22 | use ApiPlatform\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
|
23 | 23 | use ApiPlatform\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
|
24 | 24 | use ApiPlatform\Metadata\Property\PropertyNameCollection;
|
| 25 | +use ApiPlatform\Symfony\Security\ResourceAccessCheckerInterface; |
25 | 26 | use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Dummy;
|
| 27 | +use ApiPlatform\Tests\Fixtures\TestBundle\Entity\SecuredDummy; |
26 | 28 | use PHPUnit\Framework\TestCase;
|
27 | 29 | use Prophecy\Argument;
|
28 | 30 | use Prophecy\PhpUnit\ProphecyTrait;
|
@@ -115,6 +117,95 @@ public function testNormalize(): void
|
115 | 117 | ];
|
116 | 118 | $this->assertEquals($expected, $normalizer->normalize($dummy, ItemNormalizer::FORMAT, [
|
117 | 119 | 'resources' => [],
|
| 120 | + 'resource_class' => Dummy::class, |
| 121 | + ])); |
| 122 | + } |
| 123 | + |
| 124 | + public function testNormalizeWithUnsafeCacheProperty(): void |
| 125 | + { |
| 126 | + $securedDummyWithOwnerOnlyPropertyAllowed = new SecuredDummy(); |
| 127 | + $securedDummyWithOwnerOnlyPropertyAllowed->setTitle('hello'); |
| 128 | + $securedDummyWithOwnerOnlyPropertyAllowed->setOwnerOnlyProperty('ownerOnly'); |
| 129 | + $securedDummyWithoutOwnerOnlyPropertyAllowed = clone $securedDummyWithOwnerOnlyPropertyAllowed; |
| 130 | + $securedDummyWithoutOwnerOnlyPropertyAllowed->setTitle('hello from secured dummy'); |
| 131 | + |
| 132 | + $propertyNameCollection = new PropertyNameCollection(['title', 'ownerOnlyProperty']); |
| 133 | + $propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class); |
| 134 | + $propertyNameCollectionFactoryProphecy->create(SecuredDummy::class, [])->willReturn($propertyNameCollection); |
| 135 | + |
| 136 | + $unsecuredPropertyMetadata = (new ApiProperty())->withReadable(true); |
| 137 | + $securedPropertyMetadata = (new ApiProperty())->withReadable(true)->withSecurity('object == null or object.getOwner() == user'); |
| 138 | + $propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class); |
| 139 | + $propertyMetadataFactoryProphecy->create(SecuredDummy::class, 'title', [])->willReturn($unsecuredPropertyMetadata); |
| 140 | + $propertyMetadataFactoryProphecy->create(SecuredDummy::class, 'ownerOnlyProperty', [])->willReturn($securedPropertyMetadata); |
| 141 | + |
| 142 | + $iriConverterProphecy = $this->prophesize(IriConverterInterface::class); |
| 143 | + $iriConverterProphecy->getIriFromResource($securedDummyWithOwnerOnlyPropertyAllowed, UrlGeneratorInterface::ABS_URL, Argument::any(), Argument::type('array'))->willReturn('/dummies/1'); |
| 144 | + $iriConverterProphecy->getIriFromResource($securedDummyWithoutOwnerOnlyPropertyAllowed, UrlGeneratorInterface::ABS_URL, Argument::any(), Argument::type('array'))->willReturn('/dummies/2'); |
| 145 | + |
| 146 | + $identifiersExtractorProphecy = $this->prophesize(IdentifiersExtractorInterface::class); |
| 147 | + $identifiersExtractorProphecy->getIdentifiersFromItem($securedDummyWithOwnerOnlyPropertyAllowed, Argument::any())->willReturn(['id' => 1])->shouldBeCalled(); |
| 148 | + $identifiersExtractorProphecy->getIdentifiersFromItem($securedDummyWithoutOwnerOnlyPropertyAllowed, Argument::any())->willReturn(['id' => 2])->shouldBeCalled(); |
| 149 | + |
| 150 | + $resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class); |
| 151 | + $resourceClassResolverProphecy->getResourceClass($securedDummyWithOwnerOnlyPropertyAllowed, null)->willReturn(SecuredDummy::class); |
| 152 | + $resourceClassResolverProphecy->getResourceClass($securedDummyWithoutOwnerOnlyPropertyAllowed, null)->willReturn(SecuredDummy::class); |
| 153 | + $resourceClassResolverProphecy->getResourceClass(null, SecuredDummy::class)->willReturn(SecuredDummy::class); |
| 154 | + $resourceClassResolverProphecy->isResourceClass(SecuredDummy::class)->willReturn(true); |
| 155 | + |
| 156 | + $serializerProphecy = $this->prophesize(SerializerInterface::class); |
| 157 | + $serializerProphecy->willImplement(NormalizerInterface::class); |
| 158 | + $serializerProphecy->normalize('hello', ItemNormalizer::FORMAT, Argument::type('array'))->willReturn('hello'); |
| 159 | + $serializerProphecy->normalize('hello from secured dummy', ItemNormalizer::FORMAT, Argument::type('array'))->willReturn('hello from secured dummy'); |
| 160 | + $serializerProphecy->normalize('ownerOnly', ItemNormalizer::FORMAT, Argument::type('array'))->willReturn('ownerOnly'); |
| 161 | + |
| 162 | + $resourceAccessCheckerProphecy = $this->prophesize(ResourceAccessCheckerInterface::class); |
| 163 | + $resourceAccessCheckerProphecy->isGranted( |
| 164 | + SecuredDummy::class, |
| 165 | + 'object == null or object.getOwner() == user', |
| 166 | + Argument::type('array') |
| 167 | + )->will(function (array $args) { |
| 168 | + return 'hello' === $args[2]['object']->getTitle(); // Allow access only for securedDummyWithOwnerOnlyPropertyAllowed |
| 169 | + }); |
| 170 | + |
| 171 | + $normalizer = new ItemNormalizer( |
| 172 | + $propertyNameCollectionFactoryProphecy->reveal(), |
| 173 | + $propertyMetadataFactoryProphecy->reveal(), |
| 174 | + $iriConverterProphecy->reveal(), |
| 175 | + $identifiersExtractorProphecy->reveal(), |
| 176 | + $resourceClassResolverProphecy->reveal(), |
| 177 | + null, |
| 178 | + null, |
| 179 | + null, |
| 180 | + null, |
| 181 | + null, |
| 182 | + $resourceAccessCheckerProphecy->reveal() |
| 183 | + ); |
| 184 | + $normalizer->setSerializer($serializerProphecy->reveal()); |
| 185 | + |
| 186 | + $expected = [ |
| 187 | + 'title' => 'hello', |
| 188 | + 'ownerOnlyProperty' => 'ownerOnly', |
| 189 | + ItemNormalizer::ITEM_RESOURCE_CLASS_KEY => SecuredDummy::class, |
| 190 | + ItemNormalizer::ITEM_IDENTIFIERS_KEY => [ |
| 191 | + 'id' => 1, |
| 192 | + ], |
| 193 | + ]; |
| 194 | + $this->assertEquals($expected, $normalizer->normalize($securedDummyWithOwnerOnlyPropertyAllowed, ItemNormalizer::FORMAT, [ |
| 195 | + 'resources' => [], |
| 196 | + 'resource_class' => SecuredDummy::class, |
| 197 | + ])); |
| 198 | + |
| 199 | + $expected = [ |
| 200 | + 'title' => 'hello from secured dummy', |
| 201 | + ItemNormalizer::ITEM_RESOURCE_CLASS_KEY => SecuredDummy::class, |
| 202 | + ItemNormalizer::ITEM_IDENTIFIERS_KEY => [ |
| 203 | + 'id' => 2, |
| 204 | + ], |
| 205 | + ]; |
| 206 | + $this->assertEquals($expected, $normalizer->normalize($securedDummyWithoutOwnerOnlyPropertyAllowed, ItemNormalizer::FORMAT, [ |
| 207 | + 'resources' => [], |
| 208 | + 'resource_class' => SecuredDummy::class, |
118 | 209 | ]));
|
119 | 210 | }
|
120 | 211 |
|
|
0 commit comments