You are here

class DrupalAuthForSSPSubscriber in DrupalAuth for SimpleSAMLphp 8

DrupalAuth for SimpleSAMLphp event subscriber.

Hierarchy

  • class \Drupal\drupalauth4ssp\EventSubscriber\DrupalAuthForSSPSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of DrupalAuthForSSPSubscriber

1 string reference to 'DrupalAuthForSSPSubscriber'
drupalauth4ssp.services.yml in ./drupalauth4ssp.services.yml
drupalauth4ssp.services.yml
1 service uses DrupalAuthForSSPSubscriber
drupalauth4ssp.event_subscriber in ./drupalauth4ssp.services.yml
Drupal\drupalauth4ssp\EventSubscriber\DrupalAuthForSSPSubscriber

File

src/EventSubscriber/DrupalAuthForSSPSubscriber.php, line 16

Namespace

Drupal\drupalauth4ssp\EventSubscriber
View source
class DrupalAuthForSSPSubscriber implements EventSubscriberInterface {

  /**
   * Account proxy.
   *
   * @var \Drupal\Core\Session\AccountProxyInterface
   */
  protected $accountProxy;

  /**
   * Constructs event subscriber.
   *
   * @param \Drupal\Core\Session\AccountProxyInterface $account_proxy
   *   Account proxy.
   */
  public function __construct(AccountProxyInterface $account_proxy) {
    $this->accountProxy = $account_proxy;
  }

  /**
   * Kernel response event handler.
   *
   * @param \Symfony\Component\HttpKernel\Event\FilterResponseEvent $event
   *   Response event.
   */
  public function checkRedirection(FilterResponseEvent $event) {
    if ($event
      ->getResponse() instanceof RedirectResponse) {
      $response = $event
        ->getResponse();
      $path = $response
        ->getTargetUrl();
      $frontPage = Url::fromRoute('<front>')
        ->setAbsolute()
        ->toString();

      // Redirect after log out.
      $responseIsHttpFound = $response
        ->getStatusCode() === Response::HTTP_FOUND;
      $isRedirectToFrontPage = $path === $frontPage && $responseIsHttpFound;
      $destination =& drupal_static('drupalauth4ssp_user_logout');
      if ($isRedirectToFrontPage && !empty($destination)) {
        $response
          ->setTargetUrl($destination);
        $event
          ->stopPropagation();
        return;
      }

      // If this was request to login and user was authenticated by cookie.
      $returnTo = $event
        ->getRequest()->query
        ->get('ReturnTo');
      $isLoginRequest = $event
        ->getRequest()->attributes
        ->get('_route') === 'user.login';
      if ($isLoginRequest && $responseIsHttpFound && $returnTo) {
        drupalauth4ssp_set_user_cookie($this->accountProxy);
        $response
          ->setTargetUrl($returnTo);
        $event
          ->stopPropagation();
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    return [
      KernelEvents::RESPONSE => [
        'checkRedirection',
      ],
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DrupalAuthForSSPSubscriber::$accountProxy protected property Account proxy.
DrupalAuthForSSPSubscriber::checkRedirection public function Kernel response event handler.
DrupalAuthForSSPSubscriber::getSubscribedEvents public static function
DrupalAuthForSSPSubscriber::__construct public function Constructs event subscriber.