24
24
use ApiPlatform \Core \Metadata \Property \Factory \PropertyNameCollectionFactoryInterface ;
25
25
use ApiPlatform \Core \Metadata \Property \PropertyMetadata ;
26
26
use ApiPlatform \Core \Metadata \Resource \Factory \ResourceMetadataFactoryInterface ;
27
+ use ApiPlatform \Core \Security \ResourceAccessCheckerInterface ;
27
28
use ApiPlatform \Core \Util \ClassInfoTrait ;
28
29
use Symfony \Component \PropertyAccess \Exception \NoSuchPropertyException ;
29
30
use Symfony \Component \PropertyAccess \PropertyAccess ;
@@ -55,13 +56,14 @@ abstract class AbstractItemNormalizer extends AbstractObjectNormalizer
55
56
protected $ propertyMetadataFactory ;
56
57
protected $ iriConverter ;
57
58
protected $ resourceClassResolver ;
59
+ protected $ resourceAccessChecker ;
58
60
protected $ propertyAccessor ;
59
61
protected $ itemDataProvider ;
60
62
protected $ allowPlainIdentifiers ;
61
63
protected $ dataTransformers = [];
62
64
protected $ localCache = [];
63
65
64
- public function __construct (PropertyNameCollectionFactoryInterface $ propertyNameCollectionFactory , PropertyMetadataFactoryInterface $ propertyMetadataFactory , IriConverterInterface $ iriConverter , ResourceClassResolverInterface $ resourceClassResolver , PropertyAccessorInterface $ propertyAccessor = null , NameConverterInterface $ nameConverter = null , ClassMetadataFactoryInterface $ classMetadataFactory = null , ItemDataProviderInterface $ itemDataProvider = null , bool $ allowPlainIdentifiers = false , array $ defaultContext = [], iterable $ dataTransformers = [], ResourceMetadataFactoryInterface $ resourceMetadataFactory = null )
66
+ public function __construct (PropertyNameCollectionFactoryInterface $ propertyNameCollectionFactory , PropertyMetadataFactoryInterface $ propertyMetadataFactory , IriConverterInterface $ iriConverter , ResourceClassResolverInterface $ resourceClassResolver , PropertyAccessorInterface $ propertyAccessor = null , NameConverterInterface $ nameConverter = null , ClassMetadataFactoryInterface $ classMetadataFactory = null , ItemDataProviderInterface $ itemDataProvider = null , bool $ allowPlainIdentifiers = false , array $ defaultContext = [], iterable $ dataTransformers = [], ResourceMetadataFactoryInterface $ resourceMetadataFactory = null , ResourceAccessCheckerInterface $ resourceAccessChecker = null )
65
67
{
66
68
if (!isset ($ defaultContext ['circular_reference_handler ' ])) {
67
69
$ defaultContext ['circular_reference_handler ' ] = function ($ object ) {
@@ -83,6 +85,7 @@ public function __construct(PropertyNameCollectionFactoryInterface $propertyName
83
85
$ this ->allowPlainIdentifiers = $ allowPlainIdentifiers ;
84
86
$ this ->dataTransformers = $ dataTransformers ;
85
87
$ this ->resourceMetadataFactory = $ resourceMetadataFactory ;
88
+ $ this ->resourceAccessChecker = $ resourceAccessChecker ;
86
89
}
87
90
88
91
/**
@@ -349,6 +352,25 @@ protected function getAllowedAttributes($classOrObject, array $context, $attribu
349
352
return $ allowedAttributes ;
350
353
}
351
354
355
+ /**
356
+ * {@inheritdoc}
357
+ */
358
+ protected function isAllowedAttribute ($ classOrObject , $ attribute , $ format = null , array $ context = [])
359
+ {
360
+ if (!parent ::isAllowedAttribute ($ classOrObject , $ attribute , $ format , $ context )) {
361
+ return false ;
362
+ }
363
+
364
+ $ options = $ this ->getFactoryOptions ($ context );
365
+ $ propertyMetadata = $ this ->propertyMetadataFactory ->create ($ context ['resource_class ' ], $ attribute , $ options );
366
+ $ security = $ propertyMetadata ->getAttribute ('security ' );
367
+ if ($ this ->resourceAccessChecker && $ security ) {
368
+ return $ this ->resourceAccessChecker ->isGranted ($ attribute , $ security );
369
+ }
370
+
371
+ return true ;
372
+ }
373
+
352
374
/**
353
375
* {@inheritdoc}
354
376
*/
0 commit comments