You are here

public function ComposeAccessCheck::access in Courier 8

Same name and namespace in other branches
  1. 2.x courier_message_composer/src/Access/ComposeAccessCheck.php \Drupal\courier_message_composer\Access\ComposeAccessCheck::access()

Checks if user can send to a channel.

File

courier_message_composer/src/Access/ComposeAccessCheck.php, line 37

Class

ComposeAccessCheck
Checks if user can send to a channel.

Namespace

Drupal\courier_message_composer\Access

Code

public function access(Route $route, RouteMatchInterface $route_match, AccountInterface $account) {
  $requirement = $route
    ->getRequirement('_courier_compose');
  $channels_all = $this->identityChannelManager
    ->getChannels();
  $channels = [];
  if ($requirement == '*') {

    // Check if user can send to *any* channel.
    $channels = array_keys($channels_all);
  }
  elseif ($courier_channel = $route_match
    ->getParameter('courier_channel')) {
    $channels = array_key_exists($courier_channel
      ->id(), $channels_all) ? [
      $courier_channel
        ->id(),
    ] : [];
  }
  foreach ($channels as $channel) {
    foreach ($channels_all[$channel] as $identity) {
      if ($account
        ->hasPermission("courier_message_composer compose {$channel} to {$identity}")) {
        return AccessResult::allowed()
          ->cachePerPermissions();
      }
    }
  }
  return AccessResult::forbidden()
    ->cachePerPermissions();
}