You are here

class EntityAccessDeniedHttpException in JSON:API 8

Same name and namespace in other branches
  1. 8.2 src/Exception/EntityAccessDeniedHttpException.php \Drupal\jsonapi\Exception\EntityAccessDeniedHttpException

Enhances the access denied exception with information about the entity.

@internal

Hierarchy

Expanded class hierarchy of EntityAccessDeniedHttpException

4 files declare their use of EntityAccessDeniedHttpException
EntityAccessDeniedHttpExceptionNormalizer.php in src/Normalizer/EntityAccessDeniedHttpExceptionNormalizer.php
EntityCollection.php in src/Resource/EntityCollection.php
EntityResource.php in src/Controller/EntityResource.php
EntityResourceTest.php in tests/src/Kernel/Controller/EntityResourceTest.php

File

src/Exception/EntityAccessDeniedHttpException.php, line 16

Namespace

Drupal\jsonapi\Exception
View source
class EntityAccessDeniedHttpException extends HttpException {
  use DependencySerializationTrait;

  /**
   * The error which caused the 403.
   *
   * The error contains:
   *   - entity: The entity which the current user doens't have access to.
   *   - pointer: A path in the JSON API response structure pointing to the
   *     entity.
   *   - reason: (Optional) An optional reason for this failure.
   *
   * @var array
   */
  protected $error = [];

  /**
   * EntityAccessDeniedHttpException constructor.
   *
   * @param \Drupal\Core\Entity\EntityInterface|null $entity
   *   The entity, or NULL when an entity is being created.
   * @param \Drupal\Core\Access\AccessResultInterface $entity_access
   *   The access result.
   * @param string $pointer
   *   (optional) The pointer.
   * @param string $messsage
   *   (Optional) The display to display.
   * @param \Exception|null $previous
   *   The previous exception.
   * @param array $headers
   *   The headers.
   * @param int $code
   *   The code.
   */
  public function __construct($entity, AccessResultInterface $entity_access, $pointer, $messsage = 'The current user is not allowed to GET the selected resource.', \Exception $previous = NULL, array $headers = [], $code = 0) {
    assert(is_null($entity) || $entity instanceof EntityInterface);
    parent::__construct(403, $messsage, $previous, $headers, $code);
    $error = [
      'entity' => $entity,
      'pointer' => $pointer,
      'reason' => NULL,
    ];
    if ($entity_access instanceof AccessResultReasonInterface) {
      $error['reason'] = $entity_access
        ->getReason();
    }
    $this->error = $error;
  }

  /**
   * Returns the error.
   *
   * @return array
   *   The error.
   */
  public function getError() {
    return $this->error;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
EntityAccessDeniedHttpException::$error protected property The error which caused the 403.
EntityAccessDeniedHttpException::getError public function Returns the error.
EntityAccessDeniedHttpException::__construct public function EntityAccessDeniedHttpException constructor.