forked from alxp/islandora
-
Notifications
You must be signed in to change notification settings - Fork 119
/
LinkHeaderSubscriber.php
313 lines (268 loc) · 8.98 KB
/
LinkHeaderSubscriber.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
<?php
namespace Drupal\islandora\EventSubscriber;
use Drupal\Core\Access\AccessManagerInterface;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\islandora\IslandoraUtils;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
/**
* Class LinkHeaderSubscriber.
*
* @package Drupal\islandora\EventSubscriber
*/
abstract class LinkHeaderSubscriber implements EventSubscriberInterface {
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* The entity field manager.
*
* @var \Drupal\Core\Entity\EntityFieldManagerInterface
*/
protected $entityFieldManager;
/**
* The access manager.
*
* @var \Drupal\Core\Access\AccessManagerInterface
*/
protected $accessManager;
/**
* Current user.
*
* @var \Drupal\Core\Session\AccountInterface
*/
protected $account;
/**
* The route match object.
*
* @var \Drupal\Core\Routing\RouteMatchInterface
*/
protected $routeMatch;
/**
* Request stack (for current request).
*
* @var Symfony\Component\HttpFoundation\RequestStack
*/
protected $requestStack;
/**
* Islandora utils.
*
* @var \Drupal\islandora\IslandoraUtils
*/
protected $utils;
/**
* Constructor.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
* @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager
* The entity field manager.
* @param \Drupal\Core\Access\AccessManagerInterface $access_manager
* The access manager.
* @param \Drupal\Core\Session\AccountInterface $account
* The current user.
* @param \Drupal\Core\Routing\RouteMatchInterface $route_match
* The route match object.
* @param Symfony\Component\HttpFoundation\RequestStack $request_stack
* Request stack (for current request).
* @param \Drupal\islandora\IslandoraUtils $utils
* Islandora utils.
*/
public function __construct(
EntityTypeManagerInterface $entity_type_manager,
EntityFieldManagerInterface $entity_field_manager,
AccessManagerInterface $access_manager,
AccountInterface $account,
RouteMatchInterface $route_match,
RequestStack $request_stack,
IslandoraUtils $utils
) {
$this->entityTypeManager = $entity_type_manager;
$this->entityFieldManager = $entity_field_manager;
$this->accessManager = $access_manager;
$this->account = $account;
$this->routeMatch = $route_match;
$this->accessManager = $access_manager;
$this->account = $account;
$this->requestStack = $request_stack;
$this->utils = $utils;
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
// Run this early so the headers get cached.
$events[KernelEvents::RESPONSE][] = ['onResponse', 129];
return $events;
}
/**
* Get the Node | Media | File.
*
* @param \Symfony\Component\HttpFoundation\Response $response
* The current response object.
* @param string $object_type
* The type of entity to look for.
*
* @return Drupal\Core\Entity\ContentEntityBase|bool
* A node or media entity or FALSE if we should skip out.
*/
protected function getObject(Response $response, $object_type) {
if ($object_type != 'node'
&& $object_type != 'media'
) {
return FALSE;
}
// Exit early if the response is already cached.
if ($response->headers->get('X-Drupal-Dynamic-Cache') == 'HIT') {
return FALSE;
}
if (!$response->isOk()) {
return FALSE;
}
// Hack the node out of the route.
$route_object = $this->routeMatch->getRouteObject();
if (!$route_object) {
return FALSE;
}
$methods = $route_object->getMethods();
$is_get = in_array('GET', $methods);
$is_head = in_array('HEAD', $methods);
if (!($is_get || $is_head)) {
return FALSE;
}
$route_contexts = $route_object->getOption('parameters');
if (!$route_contexts) {
return FALSE;
}
if (!isset($route_contexts[$object_type])) {
return FALSE;
}
$object = $this->routeMatch->getParameter($object_type);
if (!$object) {
return FALSE;
}
return $object;
}
/**
* Generates link headers for each referenced entity.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* Entity that has reference fields.
*
* @return string[]
* Array of link headers
*/
protected function generateEntityReferenceLinks(EntityInterface $entity) {
// Use the node to add link headers for each entity reference.
$entity_type = $entity->getEntityType()->id();
$bundle = $entity->bundle();
// Get all fields for the entity.
$fields = $this->entityFieldManager->getFieldDefinitions($entity_type, $bundle);
// Strip out everything but entity references that are not base fields.
$entity_reference_fields = array_filter($fields, function ($field) {
return $field->getFieldStorageDefinition()->isBaseField() == FALSE && $field->getType() == "entity_reference";
});
// Collect links for referenced entities.
$links = [];
foreach ($entity_reference_fields as $field_name => $field_definition) {
foreach ($entity->get($field_name)->referencedEntities() as $referencedEntity) {
// Headers are subject to an access check.
if ($referencedEntity->access('view')) {
$entity_url = $this->utils->getEntityUrl($referencedEntity);
// Taxonomy terms are written out as
// <url>; rel="tag"; title="Tag Name"
// where url is defined in field_same_as.
// If field_same_as doesn't exist or is empty,
// it becomes the taxonomy term's local uri.
if ($referencedEntity->getEntityTypeId() == 'taxonomy_term') {
$rel = "tag";
if ($referencedEntity->hasField(IslandoraUtils::EXTERNAL_URI_FIELD)) {
$external_uri = $referencedEntity->get(IslandoraUtils::EXTERNAL_URI_FIELD)->getValue();
if (!empty($external_uri) && isset($external_uri[0]['uri'])) {
$entity_url = $external_uri[0]['uri'];
}
}
$title = $referencedEntity->label();
}
else {
// If it's not a taxonomy term, referenced entity link
// headers take the form
// <url>; rel="related"; title="Field Label"
// and the url is the local uri.
$rel = "related";
$title = $field_definition->label();
}
$links[] = "<$entity_url>; rel=\"$rel\"; title=\"$title\"";
}
}
}
return $links;
}
/**
* Generates link headers for REST endpoints.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* Entity that has reference fields.
*
* @return string[]
* Array of link headers
*/
protected function generateRestLinks(EntityInterface $entity) {
$rest_resource_config_storage = $this->entityTypeManager->getStorage('rest_resource_config');
$entity_type = $entity->getEntityType()->id();
$rest_resource_config = $rest_resource_config_storage->load("entity.$entity_type");
$current_format = $this->requestStack->getCurrentRequest()->query->get('_format');
$links = [];
$route_name = $this->routeMatch->getRouteName();
if ($rest_resource_config) {
$formats = $rest_resource_config->getFormats("GET");
foreach ($formats as $format) {
if ($format == $current_format) {
continue;
}
switch ($format) {
case 'json':
$mime = 'application/json';
break;
case 'jsonld':
$mime = 'application/ld+json';
break;
case 'hal_json':
$mime = 'application/hal+json';
break;
case 'xml':
$mime = 'application/xml';
break;
default:
continue;
}
// Skip route if the user doesn't have access.
$meta_route_name = "rest.entity.$entity_type.GET";
$route_params = [$entity_type => $entity->id()];
if (!$this->accessManager->checkNamedRoute($meta_route_name, $route_params, $this->account)) {
continue;
}
$meta_url = $this->utils->getRestUrl($entity, $format);
$links[] = "<$meta_url>; rel=\"alternate\"; type=\"$mime\"";
}
}
return $links;
}
/**
* Adds resource-specific link headers to appropriate responses.
*
* @param \Symfony\Component\HttpKernel\Event\FilterResponseEvent $event
* Event containing the response.
*/
abstract public function onResponse(FilterResponseEvent $event);
}