You are here

SamlSPDrupalLoginController.php in SAML Service Provider 3.x

File

modules/saml_sp_drupal_login/src/Controller/SamlSPDrupalLoginController.php
View source
<?php

namespace Drupal\saml_sp_drupal_login\Controller;

use Drupal\Core\Access\AccessResult;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Url;
use Drupal\saml_sp\Entity\Idp;
use Laminas\Diactoros\Response\JsonResponse;
use Symfony\Component\HttpFoundation\RedirectResponse;

/**
 * Provides route responses for the SAML SP module.
 */
class SamlSPDrupalLoginController extends ControllerBase {

  /**
   * Initiate a SAML login for the given IdP.
   */
  public function initiate(Idp $idp) {
    if ($this
      ->currentUser()
      ->isAuthenticated()) {
      $redirect_path = $this
        ->config('saml_sp_drupal_login.config')
        ->get('logged_in_redirect');
      dpm($redirect_path, '$redirect_path');
      if (strpos($redirect_path, '/') === 0) {
        $url = URL::fromUserInput($redirect_path);
      }
      else {
        $url = URL::fromRoute($redirect_path);
      }

      // the user is already logged in, redirect
      return new RedirectResponse($url
        ->toString());
    }

    // Start the authentication process; invoke
    // saml_sp_drupal_login__saml_authenticate() when done.
    $return = saml_sp_start($idp, 'saml_sp_drupal_login__saml_authenticate');
    if (!empty($return)) {

      // Something was returned, echo it to the screen.
      return $return;
    }
  }

  /**
   * Tests condition for requesting accounts.
   */
  public function access(AccountInterface $account) {
    $authenticated = saml_sp_drupal_login_is_authenticated();
    return AccessResult::allowedIf($account
      ->isAnonymous() && $authenticated);
  }

}

Classes

Namesort descending Description
SamlSPDrupalLoginController Provides route responses for the SAML SP module.