You are here

class SimpleMegaMenuAccessCanonicalPageSubscriber in Simple Mega Menu 2.0.x

Same name and namespace in other branches
  1. 8 src/EventSubscriber/SimpleMegaMenuAccessCanonicalPageSubscriber.php \Drupal\simple_megamenu\EventSubscriber\SimpleMegaMenuAccessCanonicalPageSubscriber

Class SimpleMegaMenuAccessCanonicalPageSubscriber.

@package Drupal\simple_megamenu

Hierarchy

Expanded class hierarchy of SimpleMegaMenuAccessCanonicalPageSubscriber

1 string reference to 'SimpleMegaMenuAccessCanonicalPageSubscriber'
simple_megamenu.services.yml in ./simple_megamenu.services.yml
simple_megamenu.services.yml
1 service uses SimpleMegaMenuAccessCanonicalPageSubscriber
simple_megamenu.access_canonical_page in ./simple_megamenu.services.yml
Drupal\simple_megamenu\EventSubscriber\SimpleMegaMenuAccessCanonicalPageSubscriber

File

src/EventSubscriber/SimpleMegaMenuAccessCanonicalPageSubscriber.php, line 18

Namespace

Drupal\simple_megamenu\EventSubscriber
View source
class SimpleMegaMenuAccessCanonicalPageSubscriber implements EventSubscriberInterface {

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

  /**
   * Constructor.
   *
   * @param \Drupal\Core\Session\AccountInterface $current_user
   *   The current user.
   * @param \Drupal\Core\Routing\UrlGeneratorInterface $url_generator
   *   The url generator.
   */
  public function __construct(AccountInterface $current_user, UrlGeneratorInterface $url_generator) {
    $this->currentUser = $current_user;
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events[KernelEvents::REQUEST] = [
      'onRequestCheckAccess',
    ];
    return $events;
  }

  /**
   * This method is called whenever the kernel.request event is dispatched.
   *
   * @param \Symfony\Component\HttpKernel\Event\GetResponseEvent $event
   *   The event object.
   */
  public function onRequestCheckAccess(GetResponseEvent $event) {
    $request = $event
      ->getRequest();

    // If we've got an exception, nothing to do here.
    if ($request
      ->get('exception') != NULL) {
      return;
    }

    /** @var \Drupal\simple_megamenu\Entity\SimpleMegaMenu $simple_mega_menu */
    $simple_mega_menu = $request
      ->get('simple_mega_menu');
    if ($simple_mega_menu && $simple_mega_menu instanceof SimpleMegaMenuInterface) {
      if (!$this->currentUser
        ->hasPermission('access simple mega menu entities canonical page')) {
        throw new NotFoundHttpException();
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
SimpleMegaMenuAccessCanonicalPageSubscriber::$currentUser protected property The current user.
SimpleMegaMenuAccessCanonicalPageSubscriber::getSubscribedEvents public static function
SimpleMegaMenuAccessCanonicalPageSubscriber::onRequestCheckAccess public function This method is called whenever the kernel.request event is dispatched.
SimpleMegaMenuAccessCanonicalPageSubscriber::__construct public function Constructor.