You are here

public function MailingListController::accessSubscription in Mailing List 8

Process access link to a subscription.

Parameters

int $sid: The subscription ID what want to be accessed.

string $hash: Access hash.

string $rel: Destination entity link. Defaults to canonical.

1 string reference to 'MailingListController::accessSubscription'
mailing_list.routing.yml in ./mailing_list.routing.yml
mailing_list.routing.yml

File

src/Controller/MailingListController.php, line 111

Class

MailingListController
Returns responses for Mailing list routes.

Namespace

Drupal\mailing_list\Controller

Code

public function accessSubscription($sid, $hash, $rel = 'canonical') {
  $subscription_storage = $this
    ->entityTypeManager()
    ->getStorage('mailing_list_subscription');

  /** @var \Drupal\mailing_list\SubscriptionInterface $subscription */
  if (!($subscription = $subscription_storage
    ->load($sid))) {
    throw new NotFoundHttpException();
  }
  $url = $subscription
    ->toUrl($rel);

  // Go to subscription if access already granted.
  if (!($go = $url
    ->access())) {

    // Go if valid hash received and owner or cross access allowed.
    if ($subscription
      ->getAccessHash() == $hash && ($subscription
      ->getList()
      ->isCrossAccessAllowed() || $subscription
      ->getOwnerId() == $this
      ->currentUser()
      ->id())) {

      // Grant session access to the current user.
      $this->mailingListManager
        ->grantSessionAccess($subscription);
      $go = TRUE;
    }
  }

  // Go.
  if ($go) {

    // The access link is the only way that anonymous users have to manage
    // all their subscriptions. We will grant session access to any
    // additional anonymous subscription with the same email.
    if ($this
      ->currentUser()
      ->isAnonymous()) {
      foreach ($subscription_storage
        ->loadMultiple($subscription_storage
        ->getQuery()
        ->condition('uid', 0)
        ->condition('email', $subscription
        ->getEmail())
        ->condition('status', SubscriptionInterface::ACTIVE)
        ->execute()) as $additional_subscription) {
        if (!$additional_subscription
          ->access('view')) {
          $this->mailingListManager
            ->grantSessionAccess($additional_subscription);
        }
      }
    }

    // Redirect to subscription.
    return $this
      ->redirect($url
      ->getRouteName(), $url
      ->getRouteParameters());
  }
  else {
    throw new AccessDeniedHttpException();
  }
}