You are here

function domain_entity_entity_create_access in Domain Access Entity 8

File

./domain_entity.module, line 508
Enables domain access for entities, and access query alter.

Code

function domain_entity_entity_create_access(AccountInterface $account, array $context, $entity_bundle) {

  // Check to see that we have a valid active domain.
  // Without one, we cannot assert an opinion about access.

  /** @var \Drupal\domain\DomainInterface $active */
  if ($active = \Drupal::service('domain.negotiator')
    ->getActiveDomain()) {
    $id = $active
      ->getDomainId();
  }
  else {
    return AccessResult::neutral();
  }

  // Load the full user record.
  $user = \Drupal::entityTypeManager()
    ->getStorage('user')
    ->load($account
    ->id());
  $user_domains = \Drupal::service('domain_access.manager')
    ->getAccessValues($user);
  if (($account
    ->hasPermission('create ' . $entity_bundle . ' ' . $context['entity_type_id'] . ' content on assigned domains') || $account
    ->hasPermission('create domain content')) && in_array($id, $user_domains)) {

    // Note the cache context here!
    return AccessResult::allowed()
      ->addCacheContexts([
      'user.permissions',
      'url.site',
    ]);
  }

  // No opinion.
  return AccessResult::neutral();
}