Newer
Older
Dries Buytaert
committed
<?php
/**
* @file
* Definition of Drupal\rest\Plugin\rest\resource\EntityResource.
*/
namespace Drupal\rest\Plugin\rest\resource;
use Drupal\Core\Entity\EntityInterface;
Dries Buytaert
committed
use Drupal\Core\Entity\EntityStorageException;
use Drupal\rest\Plugin\ResourceBase;
Dries Buytaert
committed
use Drupal\rest\ResourceResponse;
Alex Pott
committed
use Drupal\Component\Utility\String;
Dries Buytaert
committed
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
Dries Buytaert
committed
use Symfony\Component\HttpKernel\Exception\HttpException;
/**
* Represents entities as resources.
*
* @RestResource(
Dries Buytaert
committed
* id = "entity",
* label = @Translation("Entity"),
* serialization_class = "Drupal\Core\Entity\Entity",
Dries Buytaert
committed
* deriver = "Drupal\rest\Plugin\Derivative\EntityDerivative",
catch
committed
* uri_paths = {
* "canonical" = "/entity/{entity_type}/{entity}",
* "http://drupal.org/link-relations/create" = "/entity/{entity_type}"
* }
Dries Buytaert
committed
* )
*
* @see \Drupal\rest\Plugin\Derivative\EntityDerivative
Dries Buytaert
committed
*/
class EntityResource extends ResourceBase {
Dries Buytaert
committed
/**
* Responds to entity GET requests.
*
catch
committed
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity object.
Dries Buytaert
committed
*
* @return \Drupal\rest\ResourceResponse
catch
committed
* The response containing the entity with its accessible fields.
Dries Buytaert
committed
*
* @throws \Symfony\Component\HttpKernel\Exception\HttpException
*/
catch
committed
public function get(EntityInterface $entity) {
if (!$entity->access('view')) {
throw new AccessDeniedHttpException();
}
foreach ($entity as $field_name => $field) {
if (!$field->access('view')) {
unset($entity->{$field_name});
Dries Buytaert
committed
}
Dries Buytaert
committed
}
catch
committed
return new ResourceResponse($entity);
Dries Buytaert
committed
}
/**
* Responds to entity POST requests and saves the new entity.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity.
*
* @return \Drupal\rest\ResourceResponse
* The HTTP response object.
*
* @throws \Symfony\Component\HttpKernel\Exception\HttpException
*/
catch
committed
public function post(EntityInterface $entity = NULL) {
Alex Pott
committed
if ($entity == NULL) {
Alex Pott
committed
throw new BadRequestHttpException('No entity content received.');
Alex Pott
committed
}
Dries Buytaert
committed
if (!$entity->access('create')) {
throw new AccessDeniedHttpException();
}
Alex Pott
committed
$definition = $this->getPluginDefinition();
// Verify that the deserialized entity is of the type that we expect to
// prevent security issues.
if ($entity->getEntityTypeId() != $definition['entity_type']) {
Alex Pott
committed
throw new BadRequestHttpException('Invalid entity type');
}
// POSTed entities must not have an ID set, because we always want to create
// new entities here.
if (!$entity->isNew()) {
Alex Pott
committed
throw new BadRequestHttpException('Only new entities can be created');
}
Dries Buytaert
committed
foreach ($entity as $field_name => $field) {
if (!$field->access('create')) {
Alex Pott
committed
throw new AccessDeniedHttpException(String::format('Access denied on creating field ', array('@field' => $field_name)));
Dries Buytaert
committed
}
}
Alex Pott
committed
// Validate the received data before saving.
$this->validate($entity);
try {
$entity->save();
$this->logger->notice('Created entity %type with ID %id.', array('%type' => $entity->getEntityTypeId(), '%id' => $entity->id()));
Angie Byron
committed
$url = _url(strtr($this->pluginId, ':', '/') . '/' . $entity->id(), array('absolute' => TRUE));
// 201 Created responses have an empty body.
return new ResourceResponse(NULL, 201, array('Location' => $url));
}
catch (EntityStorageException $e) {
Alex Pott
committed
throw new HttpException(500, 'Internal Server Error', $e);
}
}
/**
* Responds to entity PATCH requests.
*
catch
committed
* @param \Drupal\Core\Entity\EntityInterface $original_entity
* The original entity object.
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity.
*
* @return \Drupal\rest\ResourceResponse
* The HTTP response object.
*
* @throws \Symfony\Component\HttpKernel\Exception\HttpException
*/
catch
committed
public function patch(EntityInterface $original_entity, EntityInterface $entity = NULL) {
Alex Pott
committed
if ($entity == NULL) {
Alex Pott
committed
throw new BadRequestHttpException('No entity content received.');
Alex Pott
committed
}
Alex Pott
committed
$definition = $this->getPluginDefinition();
if ($entity->getEntityTypeId() != $definition['entity_type']) {
Alex Pott
committed
throw new BadRequestHttpException('Invalid entity type');
}
Dries Buytaert
committed
if (!$original_entity->access('update')) {
throw new AccessDeniedHttpException();
}
// Overwrite the received properties.
foreach ($entity->_restPatchFields as $field_name) {
$field = $entity->get($field_name);
// It is not possible to set the language to NULL as it is automatically
// re-initialized. As it must not be empty, skip it if it is.
// @todo: Use the langcode entity key when available. See
// https://drupal.org/node/2143729.
if ($field_name == 'langcode' && $field->isEmpty()) {
continue;
}
if ($field->isEmpty() && !$original_entity->get($field_name)->access('delete')) {
throw new AccessDeniedHttpException(String::format('Access denied on deleting field @field.', array('@field' => $field_name)));
}
$original_entity->set($field_name, $field->getValue());
if (!$original_entity->get($field_name)->access('update')) {
throw new AccessDeniedHttpException(String::format('Access denied on updating field @field.', array('@field' => $field_name)));
}
}
Alex Pott
committed
// Validate the received data before saving.
$this->validate($original_entity);
try {
$original_entity->save();
$this->logger->notice('Updated entity %type with ID %id.', array('%type' => $entity->getEntityTypeId(), '%id' => $entity->id()));
Angie Byron
committed
// Update responses have an empty body.
return new ResourceResponse(NULL, 204);
}
catch (EntityStorageException $e) {
Alex Pott
committed
throw new HttpException(500, 'Internal Server Error', $e);
}
}
Dries Buytaert
committed
/**
* Responds to entity DELETE requests.
*
catch
committed
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity object.
Dries Buytaert
committed
*
Dries Buytaert
committed
* @return \Drupal\rest\ResourceResponse
* The HTTP response object.
Dries Buytaert
committed
*
* @throws \Symfony\Component\HttpKernel\Exception\HttpException
*/
catch
committed
public function delete(EntityInterface $entity) {
if (!$entity->access('delete')) {
throw new AccessDeniedHttpException();
}
try {
$entity->delete();
$this->logger->notice('Deleted entity %type with ID %id.', array('%type' => $entity->getEntityTypeId(), '%id' => $entity->id()));
Angie Byron
committed
catch
committed
// Delete responses have an empty body.
return new ResourceResponse(NULL, 204);
}
catch (EntityStorageException $e) {
Alex Pott
committed
throw new HttpException(500, 'Internal Server Error', $e);
Dries Buytaert
committed
}
}
Alex Pott
committed
/**
* Verifies that the whole entity does not violate any validation constraints.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity object.
*
* @throws \Symfony\Component\HttpKernel\Exception\HttpException
* If validation errors are found.
*/
protected function validate(EntityInterface $entity) {
$violations = $entity->validate();
if (count($violations) > 0) {
$message = "Unprocessable Entity: validation failed.\n";
foreach ($violations as $violation) {
$message .= $violation->getPropertyPath() . ': ' . $violation->getMessage() . "\n";
}
// Instead of returning a generic 400 response we use the more specific
// 422 Unprocessable Entity code from RFC 4918. That way clients can
// distinguish between general syntax errors in bad serializations (code
// 400) and semantic errors in well-formed requests (code 422).
throw new HttpException(422, $message);
}
}
/**
* {@inheritdoc}
*/
protected function getBaseRoute($canonical_path, $method) {
$route = parent::getBaseRoute($canonical_path, $method);
$definition = $this->getPluginDefinition();
$parameters = $route->getOption('parameters') ?: array();
$parameters[$definition['entity_type']]['type'] = 'entity:' . $definition['entity_type'];
$route->setOption('parameters', $parameters);
return $route;
}