You are here

protected function MessageAccessControlHandler::checkCreateAccess in Message UI 8

Separate from the checkAccess because the entity does not yet exist, it will be created during the 'add' process.

Overrides EntityAccessControlHandler::checkCreateAccess

File

src/MessageAccessControlHandler.php, line 52

Class

MessageAccessControlHandler
Access controller for the comment entity.

Namespace

Drupal\message_ui

Code

protected function checkCreateAccess(AccountInterface $account, array $context, $entity_bundle = NULL) {

  // Return early if we have bypass or create any template permissions.
  if ($account
    ->hasPermission('bypass message access control') || $account
    ->hasPermission('create any message template')) {
    return AccessResult::allowed()
      ->cachePerPermissions();
  }

  /** @var \Drupal\Core\Access\AccessResult[] $results */
  $results = $this
    ->moduleHandler()
    ->invokeAll('message_message_ui_create_access_control', [
    $entity_bundle,
    $account,
  ]);
  foreach ($results as $result) {
    if ($result
      ->isNeutral()) {
      continue;
    }
    return $result;
  }

  // When we have a bundle, check access on that bundle.
  if ($entity_bundle) {
    return AccessResult::allowedIfHasPermission($account, 'create ' . $entity_bundle . ' message')
      ->cachePerPermissions();
  }

  // With no bundle, e.g. on message/add, check access to any message bundle.
  // @todo: perhaps change this method to a service as in NodeAddAccessCheck.
  foreach (\Drupal::entityTypeManager()
    ->getStorage('message_template')
    ->loadMultiple() as $template) {
    $access = AccessResult::allowedIfHasPermission($account, 'create ' . $template
      ->id() . ' message');

    // If access is allowed to any of the existing bundles return allowed.
    if ($access
      ->isAllowed()) {
      return $access
        ->cachePerPermissions();
    }
  }
  return AccessResult::neutral()
    ->cachePerPermissions();
}