You are here

class ServicesEntityCreateAccessCheck in Services 9.0.x

Same name and namespace in other branches
  1. 8.4 src/Entity/ServicesEntityCreateAccessCheck.php \Drupal\services\Entity\ServicesEntityCreateAccessCheck

Defines an access checker for entities in services endpoint.

Hierarchy

Expanded class hierarchy of ServicesEntityCreateAccessCheck

1 string reference to 'ServicesEntityCreateAccessCheck'
services.services.yml in ./services.services.yml
services.services.yml
1 service uses ServicesEntityCreateAccessCheck
services.entity_access in ./services.services.yml
Drupal\services\Entity\ServicesEntityCreateAccessCheck

File

src/Entity/ServicesEntityCreateAccessCheck.php, line 21
Contains Drupal\services\Entity\EntityAccessCheck

Namespace

Drupal\services\Entity
View source
class ServicesEntityCreateAccessCheck implements AccessInterface {

  /**
   * The entity manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityManager;
  protected $request;
  protected $serializer;

  /**
   * The key used by the routing requirement.
   *
   * @var string
   */
  protected $requirementsKey = '_services_entity_access_create';

  /**
   * Constructs a EntityCreateAccessCheck object.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_manager
   *   The entity manager.
   */
  public function __construct(EntityTypeManagerInterface $entity_manager, SerializerInterface $serializer) {
    $this->entityManager = $entity_manager;
    $this->request = \Drupal::request();
    $this->serializer = $serializer;
  }

  /**
   * Checks access to create the entity type and bundle for the given route.
   *
   * @param \Symfony\Component\Routing\Route $route
   *   The route to check against.
   * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
   *   The parametrized route.
   * @param \Drupal\Core\Session\AccountInterface $account
   *   The currently logged in account.
   *
   * @return \Drupal\Core\Access\AccessResultInterface
   *   The access result.
   */
  public function access(Route $route, RouteMatchInterface $route_match, AccountInterface $account) {
    $entity_type_id = $route
      ->getRequirement($this->requirementsKey);
    $entity_type = $this->entityManager
      ->getDefinition($entity_type_id);
    $format = $this->request
      ->getContentType();
    $content = $this->request
      ->getContent();
    $content_decoded = $this->serializer
      ->decode($content, $format);
    $entity = \Drupal::entityTypeManager()
      ->getStorage($entity_type_id)
      ->create($content_decoded);
    $bundle_value = $entity
      ->bundle();
    $bundle = is_array($bundle_value) ? reset(call_user_func_array('array_merge', $bundle_value)) : $bundle_value;

    // The bundle argument can contain request argument placeholders like
    // {name}, loop over the raw variables and attempt to replace them in the
    // bundle name. If a placeholder does not exist, it won't get replaced.
    if ($bundle && strpos($bundle, '{') !== FALSE) {
      foreach ($route_match
        ->getRawParameters()
        ->all() as $name => $value) {
        $bundle = str_replace('{' . $name . '}', $value, $bundle);
      }

      // If we were unable to replace all placeholders, deny access.
      if (strpos($bundle, '{') !== FALSE) {
        return AccessResult::neutral();
      }
    }
    return $this->entityManager
      ->getAccessControlHandler($entity_type_id)
      ->createAccess($bundle, $account, [], TRUE);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ServicesEntityCreateAccessCheck::$entityManager protected property The entity manager.
ServicesEntityCreateAccessCheck::$request protected property
ServicesEntityCreateAccessCheck::$requirementsKey protected property The key used by the routing requirement.
ServicesEntityCreateAccessCheck::$serializer protected property
ServicesEntityCreateAccessCheck::access public function Checks access to create the entity type and bundle for the given route.
ServicesEntityCreateAccessCheck::__construct public function Constructs a EntityCreateAccessCheck object.