You are here

class KeycloakRouteSubscriber in Keycloak OpenID Connect 8

Listens to dynamic route events.

Hierarchy

Expanded class hierarchy of KeycloakRouteSubscriber

1 string reference to 'KeycloakRouteSubscriber'
keycloak.services.yml in ./keycloak.services.yml
keycloak.services.yml
1 service uses KeycloakRouteSubscriber
keycloak.route_subscriber in ./keycloak.services.yml
Drupal\keycloak\Routing\KeycloakRouteSubscriber

File

src/Routing/KeycloakRouteSubscriber.php, line 13

Namespace

Drupal\keycloak\Routing
View source
class KeycloakRouteSubscriber extends RouteSubscriberBase {

  /**
   * The Keycloak service.
   *
   * @var \Drupal\keycloak\Service\KeycloakServiceInterface
   */
  protected $keycloak;

  /**
   * Construct a KeycloakRouteSubscriber object.
   *
   * @param \Drupal\keycloak\Service\KeycloakServiceInterface $keycloak
   *   A Keycloak service instance.
   */
  public function __construct(KeycloakServiceInterface $keycloak) {
    $this->keycloak = $keycloak;
  }

  /**
   * {@inheritdoc}
   */
  protected function alterRoutes(RouteCollection $collection) {

    // Whether the Keycloak client is disabled.
    if (!$this->keycloak
      ->isEnabled()) {
      return;
    }

    // Whether Keycloak single sign-on is enabled.
    if ($this->keycloak
      ->isSsoEnabled() && ($route = $collection
      ->get('user.login'))) {
      $route
        ->setDefaults([
        '_controller' => '\\Drupal\\keycloak\\Controller\\KeycloakController::login',
      ])
        ->setOptions([
        '_maintenance_access' => TRUE,
        'no_cache' => TRUE,
      ]);
    }

    // Always grant access to '/user/logout' and delegate its
    // handling to our own controller.
    if (($this->keycloak
      ->isKeycloakSignOutEnabled() || $this->keycloak
      ->isCheckSessionEnabled()) && ($route = $collection
      ->get('user.logout'))) {
      $route
        ->setDefaults([
        '_controller' => '\\Drupal\\keycloak\\Controller\\KeycloakController::logout',
      ])
        ->setRequirements([
        '_access' => 'TRUE',
      ])
        ->setOptions([
        'no_cache' => TRUE,
      ]);
    }
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {

    // Come after field_ui.
    $events[RoutingEvents::ALTER] = [
      'onAlterRoutes',
      -200,
    ];
    return $events;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
KeycloakRouteSubscriber::$keycloak protected property The Keycloak service.
KeycloakRouteSubscriber::alterRoutes protected function Alters existing routes for a specific collection. Overrides RouteSubscriberBase::alterRoutes
KeycloakRouteSubscriber::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to. Overrides RouteSubscriberBase::getSubscribedEvents
KeycloakRouteSubscriber::__construct public function Construct a KeycloakRouteSubscriber object.
RouteSubscriberBase::onAlterRoutes public function Delegates the route altering to self::alterRoutes(). 1