You are here

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

Class KernelEventListener.

@package Drupal\permissions_by_term

Hierarchy

  • class \Drupal\permissions_by_term\Listener\KernelEventListener implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of KernelEventListener

1 string reference to 'KernelEventListener'
permissions_by_term.services.yml in ./permissions_by_term.services.yml
permissions_by_term.services.yml
1 service uses KernelEventListener
permissions_by_term.kernel_event_listener in ./permissions_by_term.services.yml
Drupal\permissions_by_term\Listener\KernelEventListener

File

src/Listener/KernelEventListener.php, line 23

Namespace

Drupal\permissions_by_term\Listener
View source
class KernelEventListener implements EventSubscriberInterface {

  /**
   * @var AccessCheck
   */
  private $accessCheckService;

  /**
   * @var TermHandler
   */
  private $term;

  /**
   * @var ContainerAwareEventDispatcher
   */
  private $eventDispatcher;

  /**
   * @var AccessStorage
   */
  private $accessStorageService;

  /**
   * Instantiating of objects on class construction.
   */
  public function __construct() {
    $this->accessCheckService = \Drupal::service('permissions_by_term.access_check');
    $this->accessStorageService = \Drupal::service('permissions_by_term.access_storage');
    $this->term = \Drupal::service('permissions_by_term.term_handler');
    $this->eventDispatcher = \Drupal::service('event_dispatcher');
  }

  /**
   * Access restriction on kernel request.
   */
  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();
      }
    }
  }

  /**
   * Restricts access on kernel response.
   */
  public function onKernelResponse(FilterResponseEvent $event) {
    $this
      ->restrictTermAccessAtAutoCompletion($event);
  }

  /**
   * Restricts access to terms on AJAX auto completion.
   */
  private function restrictTermAccessAtAutoCompletion(FilterResponseEvent $event) {
    if ($event
      ->getRequest()->attributes
      ->get('target_type') == 'taxonomy_term' && $event
      ->getRequest()->attributes
      ->get('_route') == 'system.entity_autocomplete') {
      $json_suggested_terms = $event
        ->getResponse()
        ->getContent();
      $suggested_terms = json_decode($json_suggested_terms);
      $allowed_terms = [];
      foreach ($suggested_terms as $term) {
        $tid = $this->term
          ->getTermIdByName($term->label);
        $termLangcode = \Drupal::languageManager()
          ->getCurrentLanguage()
          ->getId();
        if ($this->term
          ->getTerm() instanceof \Drupal\taxonomy\Entity\Term) {
          $termLangcode = $this->term
            ->getTerm()
            ->language()
            ->getId();
        }
        if ($this->accessCheckService
          ->isAccessAllowedByDatabase($tid, \Drupal::currentUser()
          ->id(), $termLangcode)) {
          $allowed_terms[] = [
            'value' => $term->value,
            'label' => $term->label,
          ];
        }
      }
      $json_response = new JsonResponse($allowed_terms);
      $event
        ->setResponse($json_response);
    }
  }

  /**
   * The subscribed events.
   */
  public static function getSubscribedEvents() {
    return [
      KernelEvents::REQUEST => 'onKernelRequest',
      KernelEvents::RESPONSE => 'onKernelResponse',
    ];
  }
  private function canRequestGetNode(Request $request) {
    if (method_exists($request->attributes, 'get') && !empty($request->attributes
      ->get('node'))) {
      if (method_exists($request->attributes
        ->get('node'), 'get')) {
        return TRUE;
      }
    }
    return FALSE;
  }
  private function sendUserToAccessDeniedPage() {
    $redirect_url = new \Drupal\Core\Url('system.403');
    $response = new RedirectResponse($redirect_url
      ->toString());
    $response
      ->send();
    return $response;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
KernelEventListener::$accessCheckService private property
KernelEventListener::$accessStorageService private property
KernelEventListener::$eventDispatcher private property
KernelEventListener::$term private property
KernelEventListener::canRequestGetNode private function
KernelEventListener::getSubscribedEvents public static function The subscribed events.
KernelEventListener::onKernelRequest public function Access restriction on kernel request.
KernelEventListener::onKernelResponse public function Restricts access on kernel response.
KernelEventListener::restrictTermAccessAtAutoCompletion private function Restricts access to terms on AJAX auto completion.
KernelEventListener::sendUserToAccessDeniedPage private function
KernelEventListener::__construct public function Instantiating of objects on class construction.