22
33namespace Drupal \graphql \Plugin \GraphQL \DataProducer \Entity ;
44
5+ use Drupal \Core \Access \AccessManagerInterface ;
6+ use Drupal \Core \DependencyInjection \DependencySerializationTrait ;
57use Drupal \Core \Entity \EntityInterface ;
8+ use Drupal \Core \Plugin \ContainerFactoryPluginInterface ;
9+ use Drupal \Core \Session \AccountInterface ;
10+ use Drupal \graphql \GraphQL \Execution \FieldContext ;
611use Drupal \graphql \Plugin \GraphQL \DataProducer \DataProducerPluginBase ;
12+ use Symfony \Component \DependencyInjection \ContainerInterface ;
713
814/**
915 * Returns the URL of an entity.
2834 * label = @Translation("URL Options"),
2935 * description = @Translation("Options to pass to the toUrl call"),
3036 * required = FALSE
31- * )
37+ * ),
38+ * "access_user" = @ContextDefinition("entity:user",
39+ * label = @Translation("User"),
40+ * required = FALSE,
41+ * default_value = NULL
42+ * ),
3243 * }
3344 * )
3445 */
35- class EntityUrl extends DataProducerPluginBase {
46+ class EntityUrl extends DataProducerPluginBase implements ContainerFactoryPluginInterface {
47+ use DependencySerializationTrait;
48+
49+ /**
50+ * The access manager.
51+ *
52+ * @var \Drupal\Core\Access\AccessManagerInterface
53+ */
54+ protected $ accessManager ;
55+
56+ /**
57+ * {@inheritdoc}
58+ *
59+ * @codeCoverageIgnore
60+ */
61+ public static function create (ContainerInterface $ container , array $ configuration , $ pluginId , $ pluginDefinition ) {
62+ return new static (
63+ $ configuration ,
64+ $ pluginId ,
65+ $ pluginDefinition ,
66+ $ container ->get ('access_manager ' )
67+ );
68+ }
69+
70+ /**
71+ * EntityTranslation constructor.
72+ *
73+ * @param array $configuration
74+ * The plugin configuration array.
75+ * @param string $pluginId
76+ * The plugin id.
77+ * @param mixed $pluginDefinition
78+ * The plugin definition.
79+ * @param \Drupal\Core\Access\AccessManagerInterface $accessManager
80+ * The access manager service.
81+ *
82+ * @codeCoverageIgnore
83+ */
84+ public function __construct (array $ configuration , $ pluginId , $ pluginDefinition , AccessManagerInterface $ accessManager ) {
85+ parent ::__construct ($ configuration , $ pluginId , $ pluginDefinition );
86+ $ this ->accessManager = $ accessManager ;
87+ }
3688
3789 /**
3890 * Resolver.
@@ -43,13 +95,24 @@ class EntityUrl extends DataProducerPluginBase {
4395 * The link relationship type, for example: canonical or edit-form.
4496 * @param array|null $options
4597 * The options to provided to the URL generator.
98+ * @param \Drupal\Core\Session\AccountInterface|null $accessUser
99+ * @param \Drupal\graphql\GraphQL\Execution\FieldContext $context
46100 *
47101 * @return \Drupal\Core\Url
48102 *
49103 * @throws \Drupal\Core\Entity\EntityMalformedException
50104 */
51- public function resolve (EntityInterface $ entity , ?string $ rel , ?array $ options ) {
52- return $ entity ->toUrl ($ rel ?? 'canonical ' , $ options ?? []);
105+ public function resolve (EntityInterface $ entity , ?string $ rel , ?array $ options , ?AccountInterface $ accessUser , FieldContext $ context ) {
106+ $ url = $ entity ->toUrl ($ rel ?? 'canonical ' , $ options ?? []);
107+
108+ // @see https://www.drupal.org/project/drupal/issues/2677902
109+ $ access = $ this ->accessManager ->checkNamedRoute ($ url ->getRouteName (), $ url ->getRouteParameters (), $ accessUser , TRUE );
110+ $ context ->addCacheableDependency ($ access );
111+ if ($ access ->isAllowed ()) {
112+ return $ url ;
113+ }
114+
115+ return NULL ;
53116 }
54117
55118}
0 commit comments