2424use ApiPlatform \Core \Metadata \Property \Factory \PropertyNameCollectionFactoryInterface ;
2525use ApiPlatform \Core \Metadata \Property \PropertyMetadata ;
2626use ApiPlatform \Core \Metadata \Resource \Factory \ResourceMetadataFactoryInterface ;
27+ use ApiPlatform \Core \Security \ResourceAccessCheckerInterface ;
2728use ApiPlatform \Core \Util \ClassInfoTrait ;
2829use Symfony \Component \PropertyAccess \Exception \NoSuchPropertyException ;
2930use Symfony \Component \PropertyAccess \PropertyAccess ;
@@ -55,13 +56,14 @@ abstract class AbstractItemNormalizer extends AbstractObjectNormalizer
5556 protected $ propertyMetadataFactory ;
5657 protected $ iriConverter ;
5758 protected $ resourceClassResolver ;
59+ protected $ resourceAccessChecker ;
5860 protected $ propertyAccessor ;
5961 protected $ itemDataProvider ;
6062 protected $ allowPlainIdentifiers ;
6163 protected $ dataTransformers = [];
6264 protected $ localCache = [];
6365
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 )
6567 {
6668 if (!isset ($ defaultContext ['circular_reference_handler ' ])) {
6769 $ defaultContext ['circular_reference_handler ' ] = function ($ object ) {
@@ -83,6 +85,7 @@ public function __construct(PropertyNameCollectionFactoryInterface $propertyName
8385 $ this ->allowPlainIdentifiers = $ allowPlainIdentifiers ;
8486 $ this ->dataTransformers = $ dataTransformers ;
8587 $ this ->resourceMetadataFactory = $ resourceMetadataFactory ;
88+ $ this ->resourceAccessChecker = $ resourceAccessChecker ;
8689 }
8790
8891 /**
@@ -349,6 +352,25 @@ protected function getAllowedAttributes($classOrObject, array $context, $attribu
349352 return $ allowedAttributes ;
350353 }
351354
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+
352374 /**
353375 * {@inheritdoc}
354376 */
0 commit comments