You are here

public function RedirectOnAccessDeniedSubscriber::onKernelRequest in Opigno dashboard 3.x

Return value

\Drupal\Core\Session\AccountInterface

File

src/EventSubscriber/RedirectOnAccessDeniedSubscriber.php, line 52

Class

RedirectOnAccessDeniedSubscriber
Class RedirectOnAccessDeniedSubscriber.

Namespace

Drupal\opigno_dashboard\EventSubscriber

Code

public function onKernelRequest(GetResponseEvent $event) {
  $is_anonymous = $this->user
    ->isAnonymous();

  // Add the route name as an extra class to body.
  $route = (string) \Drupal::routeMatch()
    ->getRouteName();
  if ($is_anonymous && !in_array($route, [
    'user.login',
    'user.register',
    'user.pass',
    'view.frontpage.page_1',
    'view.opigno_training_catalog.training_catalogue',
    'system.403',
  ])) {
    $request = $event
      ->getRequest();
    $access_result = AccessResult::neutral();
    if (!$access_result
      ->isAllowed()) {
      if ($access_result instanceof CacheableDependencyInterface && $request
        ->isMethodCacheable()) {
        throw new CacheableAccessDeniedHttpException($access_result, $access_result instanceof AccessResultReasonInterface ? $access_result
          ->getReason() : NULL);
      }
      else {
        throw new AccessDeniedHttpException($access_result instanceof AccessResultReasonInterface ? $access_result
          ->getReason() : NULL);
      }
    }
  }
}