You are here

public function OpenIDConnectRedirectController::redirectLogout in OpenID Connect / OAuth client 2.x

Redirect after logout.

1 string reference to 'OpenIDConnectRedirectController::redirectLogout'
openid_connect.routing.yml in ./openid_connect.routing.yml
openid_connect.routing.yml

File

src/Controller/OpenIDConnectRedirectController.php, line 280

Class

OpenIDConnectRedirectController
Redirect controller.

Namespace

Drupal\openid_connect\Controller

Code

public function redirectLogout() {

  // Set default URL.
  $language = $this->languageManager
    ->getCurrentLanguage();
  $default_url = Url::fromRoute('<front>', [], [
    'language' => $language,
  ])
    ->toString(TRUE);
  $response = new RedirectResponse($default_url
    ->getGeneratedUrl());

  // @todo The fact that the user has a connected account doesn't necessarily
  //   mean that it was used for the login. This info should probably be kept
  //   in the session.
  // Get client names for this user based on its username.
  $mapped_users = $this->authmap
    ->getAll($this->currentUser
    ->id());
  if (is_array($mapped_users) & !empty($mapped_users)) {
    foreach (array_keys($mapped_users) as $key) {

      // strlen('openid_connect.') = 15.
      $client_name = substr($key, 15);

      // Perform log out.
      if (!empty($client_name)) {

        /** @var \Drupal\openid_connect\Entity\OpenIDConnectClientEntity $entity */
        $entity = current($this->entityTypeManager
          ->getStorage('openid_connect_client')
          ->loadByProperties([
          'id' => $client_name,
        ]));
        if ($entity) {
          $endpoints = $entity
            ->getPlugin()
            ->getEndpoints();
          $config = $this->configFactory
            ->get('openid_connect.settings');
          $redirect_logout = $config
            ->get('redirect_logout');
          $redirect_logout_url = empty($redirect_logout) ? FALSE : Url::fromUri('internal:/' . ltrim($redirect_logout, '/'), [
            'language' => $language,
          ]);

          // Destroy session if provider supports it.
          $end_session_enabled = $config
            ->get('end_session_enabled') ?? FALSE;
          if ($end_session_enabled && !empty($endpoints['end_session'])) {
            $url_options = [
              'query' => [
                'id_token_hint' => $this->session
                  ->retrieveIdToken(),
              ],
            ];
            if ($redirect_logout_url) {
              $url_options['query']['post_logout_redirect_uri'] = $redirect_logout_url
                ->setAbsolute()
                ->toString(TRUE)
                ->getGeneratedUrl();
            }
            $redirect = Url::fromUri($endpoints['end_session'], $url_options)
              ->toString(TRUE);
            $response = new TrustedRedirectResponse($redirect
              ->getGeneratedUrl());
            $response
              ->addCacheableDependency($redirect);
          }
          else {
            if (!$end_session_enabled) {
              $this
                ->messenger()
                ->addWarning($this
                ->t('@provider does not support log out. You are logged out of this site but not out of the OpenID Connect provider.', [
                '@provider' => $entity
                  ->label(),
              ]));
            }
            if ($redirect_logout_url) {
              $url = $redirect_logout_url
                ->toString(TRUE)
                ->getGeneratedUrl();
              $response = new TrustedRedirectResponse($url);
              $response
                ->addCacheableDependency($url);
            }
          }
          $rsp = [
            'response' => &$response,
          ];
          $context = [
            'client' => $client_name,
          ];
          $this->moduleHandler
            ->alter('openid_connect_redirect_logout', $rsp, $context);
        }
      }
    }
  }

  // Logout from Drupal.
  user_logout();
  return $response;
}