You are here

class LanguageAccessSubscriber in Language access 8

Redirect .html pages to corresponding Node page.

Hierarchy

  • class \Drupal\language_access\EventSubscriber\LanguageAccessSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of LanguageAccessSubscriber

1 string reference to 'LanguageAccessSubscriber'
language_access.services.yml in ./language_access.services.yml
language_access.services.yml
1 service uses LanguageAccessSubscriber
language_access_event_subscriber in ./language_access.services.yml
Drupal\language_access\EventSubscriber\LanguageAccessSubscriber

File

src/EventSubscriber/LanguageAccessSubscriber.php, line 18

Namespace

Drupal\language_access\EventSubscriber
View source
class LanguageAccessSubscriber implements EventSubscriberInterface {

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

  /**
   * The language manager.
   *
   * @var \Drupal\Core\Language\LanguageManagerInterface
   */
  protected $languageManager;

  /**
   * The request stack.
   *
   * @var \Symfony\Component\HttpFoundation\RequestStack
   */
  protected $requestStack;

  /**
   * Constructs LanguageAccess Subscriber.
   *
   * @param \Drupal\Core\Session\AccountInterface $current_user
   *   The current user.
   * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
   *   The language manager.
   * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
   *   The request stack.
   */
  public function __construct(AccountInterface $current_user, LanguageManagerInterface $language_manager, RequestStack $request_stack) {
    $this->currentUser = $current_user;
    $this->languageManager = $language_manager;
    $this->requestStack = $request_stack;
  }

  /**
   * Redirect pattern based url.
   *
   * @param \Symfony\Component\HttpKernel\Event\GetResponseEvent $event
   *   A GetResponseEvent instance.
   */
  public function customLanguageAccess(GetResponseEvent $event) : void {
    $request = $this->requestStack
      ->getCurrentRequest();
    $request_url = $request->server
      ->get('REQUEST_URI');
    $language = $this->languageManager
      ->getCurrentLanguage();

    // Do not execute on drush.
    if (PHP_SAPI === 'cli') {
      return;
    }

    // Do nothing on a sub request.
    if ($event
      ->getRequestType() !== HttpKernelInterface::MASTER_REQUEST) {
      return;
    }
    if (!$this
      ->pathIsWhitelisted($request_url)) {
      return;
    }

    // Check access to language.
    if ($this->currentUser
      ->hasPermission('access language ' . $language
      ->getId())) {
      return;
    }
    throw new AccessDeniedHttpException();
  }

  /**
   * Checks if the path is whitelisted.
   *
   * @param string $request_url
   *   The request URL.
   *
   * @return bool
   *   TRUE when the user can access the given URL.
   */
  public function pathIsWhitelisted(string $request_url) : bool {
    $allowed_urls = [
      '/user/',
      PublicStream::basePath(),
      'sitemap.xml',
    ];
    foreach ($allowed_urls as $allowed_url) {
      if (strpos($request_url, $allowed_url) !== FALSE) {
        return FALSE;
      }
    }
    return TRUE;
  }

  /**
   * Listen to kernel.request events and call customRedirection.
   *
   * @return array
   *   Event names to listen to (key) and methods to call (value).
   */
  public static function getSubscribedEvents() {
    $events[KernelEvents::REQUEST][] = [
      'customLanguageAccess',
    ];
    return $events;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
LanguageAccessSubscriber::$currentUser protected property The current active user.
LanguageAccessSubscriber::$languageManager protected property The language manager.
LanguageAccessSubscriber::$requestStack protected property The request stack.
LanguageAccessSubscriber::customLanguageAccess public function Redirect pattern based url.
LanguageAccessSubscriber::getSubscribedEvents public static function Listen to kernel.request events and call customRedirection.
LanguageAccessSubscriber::pathIsWhitelisted public function Checks if the path is whitelisted.
LanguageAccessSubscriber::__construct public function Constructs LanguageAccess Subscriber.