You are here

class HiddenLanguageSubscriber in Hidden Language 8

Same name and namespace in other branches
  1. 2.x src/EventSubscriber/HiddenLanguageSubscriber.php \Drupal\hidden_language\EventSubscriber\HiddenLanguageSubscriber

Hierarchy

  • class \Drupal\hidden_language\EventSubscriber\HiddenLanguageSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of HiddenLanguageSubscriber

1 string reference to 'HiddenLanguageSubscriber'
hidden_language.services.yml in ./hidden_language.services.yml
hidden_language.services.yml
1 service uses HiddenLanguageSubscriber
hidden_language_subscriber in ./hidden_language.services.yml
Drupal\hidden_language\EventSubscriber\HiddenLanguageSubscriber

File

src/EventSubscriber/HiddenLanguageSubscriber.php, line 14

Namespace

Drupal\hidden_language\EventSubscriber
View source
class HiddenLanguageSubscriber implements EventSubscriberInterface {

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

  /**
   * Constructs a new hidden language subscriber.
   *
   * @param \Drupal\Core\Session\AccountInterface $account
   *   The current user.
   */
  public function __construct(AccountInterface $account) {
    $this->account = $account;
  }

  /**
   * Disallow access to hidden language.
   *
   * @param \Symfony\Component\HttpKernel\Event\GetResponseEvent $event
   */
  public function onKernelRequestCheckLanguageAccess(GetResponseEvent $event) {
    if ($event
      ->getRequestType() === HttpKernelInterface::MASTER_REQUEST) {
      $route_name = RouteMatch::createFromRequest($event
        ->getRequest())
        ->getRouteName();

      // Don't check access if user is trying to log in, register or reset password.
      if ($this->account
        ->isAuthenticated() || !in_array($route_name, array(
        'user.login',
        'user.pass',
        'user.register',
      ))) {
        $currentLanguage = \Drupal::languageManager()
          ->getCurrentLanguage()
          ->getId();

        // Check if user has access to hidden languages.
        if (!$this->account
          ->hasPermission('access all hidden languages') && !$this->account
          ->hasPermission("access hidden language {$currentLanguage}")) {

          /** @var ConfigurableLanguage $language */
          $language = ConfigurableLanguage::load($currentLanguage);
          if ($language
            ->getThirdPartySetting('hidden_language', 'hidden', FALSE)) {
            throw new AccessDeniedHttpException();
          }
        }
      }
    }
  }

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

}

Members

Namesort descending Modifiers Type Description Overrides
HiddenLanguageSubscriber::$account protected property The current user.
HiddenLanguageSubscriber::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
HiddenLanguageSubscriber::onKernelRequestCheckLanguageAccess public function Disallow access to hidden language.
HiddenLanguageSubscriber::__construct public function Constructs a new hidden language subscriber.