You are here

public function ServicesEntityCreateAccessCheck::access in Services 8.4

Same name and namespace in other branches
  1. 9.0.x src/Entity/ServicesEntityCreateAccessCheck.php \Drupal\services\Entity\ServicesEntityCreateAccessCheck::access()

Checks access to create the entity type and bundle for the given route.

Parameters

\Symfony\Component\Routing\Route $route: The route to check against.

\Drupal\Core\Routing\RouteMatchInterface $route_match: The parametrized route.

\Drupal\Core\Session\AccountInterface $account: The currently logged in account.

Return value

\Drupal\Core\Access\AccessResultInterface The access result.

File

src/Entity/ServicesEntityCreateAccessCheck.php, line 61

Class

ServicesEntityCreateAccessCheck
Defines an access checker for entities in services endpoint.

Namespace

Drupal\services\Entity

Code

public function access(Route $route, RouteMatchInterface $route_match, AccountInterface $account) {
  $entity_type_id = $route
    ->getRequirement($this->requirementsKey);
  $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);
}