66use Drupal \Core \Entity \EntityTypeManagerInterface ;
77use Drupal \Core \Entity \TranslatableInterface ;
88use Drupal \Core \Plugin \ContainerFactoryPluginInterface ;
9+ use Drupal \Core \Session \AccountInterface ;
910use Drupal \graphql \GraphQL \Buffers \EntityBuffer ;
1011use Drupal \graphql \GraphQL \Execution \FieldContext ;
1112use Drupal \graphql \Plugin \GraphQL \DataProducer \DataProducerPluginBase ;
3637 * label = @Translation("Entity bundle(s)"),
3738 * multiple = TRUE,
3839 * required = FALSE
40+ * ),
41+ * "access" = @ContextDefinition("boolean",
42+ * label = @Translation("Check access"),
43+ * required = FALSE
44+ * ),
45+ * "access_user" = @ContextDefinition("entity:user",
46+ * label = @Translation("User"),
47+ * required = FALSE
48+ * ),
49+ * "access_operation" = @ContextDefinition("string",
50+ * label = @Translation("Operation"),
51+ * required = FALSE
3952 * )
4053 * }
4154 * )
@@ -116,14 +129,17 @@ public function __construct(
116129 * @param $id
117130 * @param null $language
118131 * @param null $bundles
132+ * @param bool $access
133+ * @param \Drupal\Core\Session\AccountInterface|NULL $accessUser
134+ * @param string $accessOperation
119135 * @param \Drupal\graphql\GraphQL\Execution\FieldContext $context
120136 *
121137 * @return \GraphQL\Deferred
122138 */
123- public function resolve ($ type , $ id = NULL , $ language = NULL , $ bundles = NULL , FieldContext $ context ) {
139+ public function resolve ($ type , $ id = NULL , $ language = NULL , $ bundles = NULL , $ access = TRUE , AccountInterface $ accessUser = NULL , string $ accessOperation = ' view ' , FieldContext $ context ) {
124140 $ resolver = $ this ->entityBuffer ->add ($ type , $ id );
125141
126- return new Deferred (function () use ($ type , $ id , $ language , $ bundles , $ resolver , $ context ) {
142+ return new Deferred (function () use ($ type , $ id , $ language , $ bundles , $ resolver , $ context, $ access , $ accessUser , $ accessOperation ) {
127143 if (!$ entity = $ resolver ()) {
128144 // If there is no entity with this id, add the list cache tags so that the
129145 // cache entry is purged whenever a new entity of this type is saved.
@@ -134,6 +150,17 @@ public function resolve($type, $id = NULL, $language = NULL, $bundles = NULL, Fi
134150 return NULL ;
135151 }
136152
153+ // Check if the passed user (or current user if none is passed) has access
154+ // to the entity, if not return NULL.
155+ if ($ access ) {
156+ /* @var $accessResult \Drupal\Core\Access\AccessResultInterface */
157+ $ accessResult = $ entity ->access ($ accessOperation , $ accessUser , TRUE );
158+ $ context ->addCacheableDependency ($ accessResult );
159+ if ($ accessResult ->isForbidden ()) {
160+ return NULL ;
161+ }
162+ }
163+
137164 if (isset ($ bundles ) && !in_array ($ entity ->bundle (), $ bundles )) {
138165 // If the entity is not among the allowed bundles, don't return it.
139166 $ context ->addCacheableDependency ($ entity );
0 commit comments