You are here

function domain_access_node_create_access in Domain Access 8

Implements hook_node_create_access().

@link https://www.drupal.org/node/2348203

File

domain_access/domain_access.module, line 411
Domain-based access control for content.

Code

function domain_access_node_create_access(AccountInterface $account, $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 . ' 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();
}