You are here

class M4032404EventSubscriber in 403 to 404 8

Provides a subscriber to set the properly exception.

Hierarchy

  • class \Drupal\m4032404\EventSubscriber\M4032404EventSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of M4032404EventSubscriber

1 file declares its use of M4032404EventSubscriber
M4032404EventSubscriberTest.php in tests/src/Unit/EventSubscriber/M4032404EventSubscriberTest.php
1 string reference to 'M4032404EventSubscriber'
m4032404.services.yml in ./m4032404.services.yml
m4032404.services.yml
1 service uses M4032404EventSubscriber
m4032404_exception_listener in ./m4032404.services.yml
Drupal\m4032404\EventSubscriber\M4032404EventSubscriber

File

src/EventSubscriber/M4032404EventSubscriber.php, line 17

Namespace

Drupal\m4032404\EventSubscriber
View source
class M4032404EventSubscriber implements EventSubscriberInterface {

  /**
   * The settings config.
   *
   * @var \Drupal\Core\Config\Config
   */
  protected $config;

  /**
   * The admin context.
   *
   * @var \Drupal\Core\Routing\AdminContext
   */
  protected $adminContext;

  /**
   * The current user.
   *
   * @var \Drupal\Core\Session\AccountProxyInterface
   */
  protected $currentUser;

  /**
   * Constructor for m4032404EventSubscriber.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The settings config.
   * @param \Drupal\Core\Routing\AdminContext $admin_context
   *   The admin context.
   * @param \Drupal\Core\Session\AccountProxyInterface $current_user
   *   The current user.
   */
  public function __construct(ConfigFactoryInterface $config_factory, AdminContext $admin_context, AccountProxyInterface $current_user) {
    $this->config = $config_factory
      ->get('m4032404.settings');
    $this->adminContext = $admin_context;
    $this->currentUser = $current_user;
  }

  /**
   * Set the properly exception for event.
   *
   * @param \Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event
   *   The response for exception event.
   */
  public function onAccessDeniedException(GetResponseForExceptionEvent $event) {
    if ($event
      ->getException() instanceof AccessDeniedHttpException) {
      $admin_only = $this->config
        ->get('admin_only');
      $is_admin = $this->adminContext
        ->isAdminRoute();
      if ((!$admin_only || $is_admin) && !$this->currentUser
        ->hasPermission('access 403 page')) {
        $event
          ->setException(new NotFoundHttpException());
      }
    }
  }

  /**
   * Registers the methods in this class that should be listeners.
   *
   * @return array
   *   An array of event listener definitions.
   */
  public static function getSubscribedEvents() {
    $events[KernelEvents::EXCEPTION][] = array(
      'onAccessDeniedException',
      50,
    );
    return $events;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
M4032404EventSubscriber::$adminContext protected property The admin context.
M4032404EventSubscriber::$config protected property The settings config.
M4032404EventSubscriber::$currentUser protected property The current user.
M4032404EventSubscriber::getSubscribedEvents public static function Registers the methods in this class that should be listeners.
M4032404EventSubscriber::onAccessDeniedException public function Set the properly exception for event.
M4032404EventSubscriber::__construct public function Constructor for m4032404EventSubscriber.