You are here

class ComposeAccessCheck 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

Checks if user can send to a channel.

Hierarchy

Expanded class hierarchy of ComposeAccessCheck

1 string reference to 'ComposeAccessCheck'
courier_message_composer.services.yml in courier_message_composer/courier_message_composer.services.yml
courier_message_composer/courier_message_composer.services.yml
1 service uses ComposeAccessCheck
access_check.courier_message_composer.compose in courier_message_composer/courier_message_composer.services.yml
Drupal\courier_message_composer\Access\ComposeAccessCheck

File

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

Namespace

Drupal\courier_message_composer\Access
View source
class ComposeAccessCheck implements AccessInterface {

  /**
   * The identity channel manager.
   *
   * @var \Drupal\courier\Service\IdentityChannelManagerInterface
   */
  protected $identityChannelManager;

  /**
   * Constructs a ComposeAccessCheck object.
   *
   * @param \Drupal\courier\Service\IdentityChannelManagerInterface $identity_channel_manager
   *   The identity channel manager.
   */
  public function __construct(IdentityChannelManagerInterface $identity_channel_manager) {
    $this->identityChannelManager = $identity_channel_manager;
  }

  /**
   * Checks if user can send to a channel.
   */
  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();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ComposeAccessCheck::$identityChannelManager protected property The identity channel manager.
ComposeAccessCheck::access public function Checks if user can send to a channel.
ComposeAccessCheck::__construct public function Constructs a ComposeAccessCheck object.