diff --git a/src/EventSubscriber/SimplesamlSubscriber.php b/src/EventSubscriber/SimplesamlSubscriber.php index 886ab1e..59e03dd 100644 --- a/src/EventSubscriber/SimplesamlSubscriber.php +++ b/src/EventSubscriber/SimplesamlSubscriber.php @@ -108,10 +108,37 @@ class SimplesamlSubscriber implements EventSubscriberInterface { } /** + * Redirect anonymous users from the Drupal login page directly to the external IdP. + * + * @param GetResponseEvent $event + * The subscribed event. + */ + public function login_directly_with_external_IdP(GetResponseEvent $event) { + + if ($this->config->get('allow.default_login')) { + return; + } + + // Check if an anonymous user tries to access the Drupal login page. + if (\Drupal::currentUser()->isAnonymous() && \Drupal::routeMatch()->getRouteName() == 'user.login') { + + // Get the path (default: '/saml_login') from the 'simplesamlphp_auth.saml_login' route. + $saml_login_path = \Drupal::url('simplesamlphp_auth.saml_login'); + + // Redirect directly to the external IdP. + $response = new RedirectResponse($saml_login_path, RedirectResponse::HTTP_FOUND); + $event->setResponse($response); + $event->stopPropagation(); + + } + } + + /** * {@inheritdoc} */ public static function getSubscribedEvents() { $events[KernelEvents::REQUEST][] = ['checkAuthStatus']; + $events[KernelEvents::REQUEST][] = ['login_directly_with_external_IdP']; return $events; } diff --git a/src/Form/LocalSettingsForm.php b/src/Form/LocalSettingsForm.php index a1fefc1..8e341b2 100644 --- a/src/Form/LocalSettingsForm.php +++ b/src/Form/LocalSettingsForm.php @@ -39,7 +39,7 @@ class LocalSettingsForm extends ConfigFormBase { '#type' => 'checkbox', '#title' => $this->t('Allow authentication with local Drupal accounts'), '#default_value' => $config->get('allow.default_login'), - '#description' => $this->t('Check this box if you want to let people log in with local Drupal accounts (without using simpleSAMLphp). If you want to restrict this privilege to certain users you can enter the Drupal user IDs in the field below.'), + '#description' => $this->t('Check this box if you want to let people log in with local Drupal accounts (without using simpleSAMLphp). If this is not selected, users will be directly redirected to the external identity provider. If this is selected, you can restrict this privilege to certain users below.'), ]; $form['authentication']['allow_set_drupal_pwd'] = [ '#type' => 'checkbox',