You are here

public function RedirectSubscriber::checkForRedirection in Data Policy 8

This method is called when the KernelEvents::REQUEST event is dispatched.

Parameters

\Symfony\Component\HttpKernel\Event\GetResponseEvent $event: The event.

File

src/RedirectSubscriber.php, line 152

Class

RedirectSubscriber
Class RedirectSubscriber.

Namespace

Drupal\data_policy

Code

public function checkForRedirection(GetResponseEvent $event) : void {

  // Check if a data policy is set.
  if (!$this->dataPolicyConsentManager
    ->isDataPolicy()) {
    return;
  }

  // Check if the current route is the data policy agreement page.
  if (($route_name = $this->routeMatch
    ->getRouteName()) === 'data_policy.data_policy.agreement') {

    // The current route is the data policy agreement page. We don't need
    // a redirect response.
    return;
  }
  $route_names = [
    'entity.user.cancel_form',
    'data_policy.data_policy',
    'system.403',
    'system.404',
    'system.batch_page.html',
    'system.batch_page.json',
    'user.cancel_confirm',
    'user.logout',
    'entity_sanitizer_image_fallback.generator',
  ];
  if (in_array($route_name, $route_names, TRUE)) {
    return;
  }
  if ($this->currentUser
    ->hasPermission('without consent')) {
    return;
  }

  // Check if entity tokens exist in the consent text in the settings form.
  $entity_ids = $this->dataPolicyConsentManager
    ->getEntityIdsFromConsentText();
  if (empty($entity_ids)) {
    return;
  }

  // At least one data policy entity should exist.
  if (empty($this->entityTypeManager
    ->getStorage('data_policy')
    ->getQuery()
    ->execute())) {
    return;
  }
  $existing_user_consents = $this->dataPolicyConsentManager
    ->getExistingUserConsents($this->currentUser
    ->id());
  $is_required_id = $this->dataPolicyConsentManager
    ->isRequiredEntityInEntities($entity_ids);

  // Do redirect if the user did not submit any consent and there is some
  // required consent.
  if (empty($existing_user_consents) && $is_required_id) {
    $this
      ->doRedirect($event);
    return;
  }
  elseif (empty($existing_user_consents) && !$is_required_id) {
    $this
      ->addStatusLink();
    return;
  }
  $revisions = $this->dataPolicyConsentManager
    ->getRevisionsByEntityIds($entity_ids);
  foreach ($revisions as $revision) {

    /** @var \Drupal\data_policy\Entity\DataPolicy $revision */
    $saved_revision_ids[] = $revision
      ->getRevisionId();
  }

  // If a new data policy was created then we should display a link or do
  // redirect to the agreement page.
  $user_revisions = $this
    ->getActiveUserRevisionData();
  $do_redirect = FALSE;
  $add_status_link = FALSE;
  foreach ($user_revisions as $item) {
    if (!in_array($item['data_policy_revision_id_value'], $saved_revision_ids)) {
      if ($item['required'] === '1') {
        $do_redirect = TRUE;
      }
      else {
        $add_status_link = TRUE;
      }
    }
  }
  if ($do_redirect) {
    $this
      ->doRedirect($event);
    return;
  }
  if ($add_status_link) {
    $this
      ->addStatusLink();
    return;
  }
  $existing_revisions = array_column($user_revisions, 'data_policy_revision_id_value');
  $revision_ids_from_consent_text = array_map(function (DataPolicyInterface $revision) {
    return $revision
      ->getRevisionId();
  }, $revisions);
  $diff = array_diff($existing_revisions, $revision_ids_from_consent_text);
  $is_new_consents = array_diff($revision_ids_from_consent_text, $existing_revisions);
  if (empty($diff) && empty($this
    ->getActiveUserRevisionData(TRUE)
    ->condition('state', 0)
    ->execute()
    ->fetchAll()) && empty($is_new_consents)) {
    return;
  }

  // If new consent is created then if this consent is required redirect
  // to the agreement page if not then appear status link.
  if (empty($diff) && !empty($is_new_consents)) {
    $is_new_required = $this->dataPolicyConsentManager
      ->isRequiredEntityInEntities($is_new_consents);
    if ($is_new_required) {
      $this
        ->doRedirect($event);
      return;
    }
    else {
      $this
        ->addStatusLink();
      return;
    }
  }
  if ($is_required_id === FALSE) {
    $this
      ->addStatusLink();
    return;
  }
  $this
    ->doRedirect($event);
}