You are here

public function LoginHandler::shibLogin in Shibboleth Authentication 8

Return value

\Symfony\Component\HttpFoundation\RedirectResponse

File

src/Login/LoginHandler.php, line 135

Class

LoginHandler
Class LoginHandler.

Namespace

Drupal\shib_auth\Login

Code

public function shibLogin() {
  try {

    // Register new user if user does not exist.
    if (!$this
      ->checkUserExists()) {

      // Use the Shib email, if we've got it.
      if (!empty($this->shib_session
        ->getEmail())) {

        // Add custom Email to the session.
        $this->custom_data_store
          ->set('custom_email', $this->shib_session
          ->getEmail());
      }

      // Check if custom email has been set.
      if (!$this->custom_data_store
        ->get('custom_email')) {
        $this->custom_data_store
          ->set('return_url', \Drupal::request()
          ->getRequestUri());

        // Redirect to email form if custom email has not been set.
        $response = new RedirectResponse(Url::fromRoute('shib_auth.custom_data_form')
          ->toString());
        return $response;
      }
      else {
        $user_registered = $this
          ->registerNewUser();
      }
    }
    else {
      $user_registered = TRUE;
    }
    if ($user_registered) {
      $this
        ->authenticateUser();
      return FALSE;
    }
  } catch (\Exception $e) {

    // Log the error to Drupal log messages.
    $this->shib_logger
      ->error($e);
    $user = \Drupal::currentUser();
    if ($user
      ->isAuthenticated()) {

      // Kill the drupal session.
      // @todo - Do we need to kill the session for anonymous users, too? If so, how do we set the error message?
      user_logout();
    }
    if ($this
      ->getErrorMessage()) {
      $this->messenger
        ->addError($this
        ->getErrorMessage());
    }
    $return_url = '';
    if ($this->adv_config
      ->get('url_redirect_logout')) {
      $return_url = '?return=' . $this->adv_config
        ->get('url_redirect_logout');
    }

    // Redirect to shib logout url.
    return new RedirectResponse($this->config
      ->get('shibboleth_logout_handler_url') . $return_url);
  }
  return FALSE;
}