You are here

public function KernelEventListener::onKernelRequest in Permissions by Term 8

Same name and namespace in other branches
  1. 8.2 src/Listener/KernelEventListener.php \Drupal\permissions_by_term\Listener\KernelEventListener::onKernelRequest()

Access restriction on kernel request.

File

src/Listener/KernelEventListener.php, line 60

Class

KernelEventListener
Class KernelEventListener.

Namespace

Drupal\permissions_by_term\Listener

Code

public function onKernelRequest(GetResponseEvent $event) {

  // Restricts access to nodes (views/edit).
  if ($this
    ->canRequestGetNode($event
    ->getRequest())) {
    $nid = $event
      ->getRequest()->attributes
      ->get('node')
      ->get('nid')
      ->getValue()['0']['value'];
    if (!$this->accessCheckService
      ->canUserAccessByNodeId($nid, false, $this->accessStorageService
      ->getLangCode($nid))) {
      $accessDeniedEvent = new PermissionsByTermDeniedEvent($nid);
      $this->eventDispatcher
        ->dispatch(PermissionsByTermDeniedEvent::NAME, $accessDeniedEvent);
      $this
        ->sendUserToAccessDeniedPage();
    }
  }

  // Restrict access to taxonomy terms by autocomplete list.
  if ($event
    ->getRequest()->attributes
    ->get('target_type') == 'taxonomy_term' && $event
    ->getRequest()->attributes
    ->get('_route') == 'system.entity_autocomplete') {
    $query_string = $event
      ->getRequest()
      ->get('q');
    $query_string = trim($query_string);
    $tid = $this->term
      ->getTermIdByName($query_string);
    $term = $this->term
      ->getTerm();
    $termLangcode = \Drupal::languageManager()
      ->getCurrentLanguage()
      ->getId();
    if ($term instanceof \Drupal\taxonomy\Entity\Term) {
      $termLangcode = $term
        ->language()
        ->getId();
    }
    if (!$this->accessCheckService
      ->isAccessAllowedByDatabase($tid, \Drupal::currentUser()
      ->id(), $termLangcode)) {
      $this
        ->sendUserToAccessDeniedPage();
    }
  }
}