Commit 3e8335b3 authored by Nate Andersen's avatar Nate Andersen Committed by Sascha Grossenbacher
Browse files

Issue #3047241 by oknate, safetypin: Remove deprecated code for Drupal 9

parent 3c680ed7
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -23,7 +23,7 @@ function simplesamlphp_auth_install() {
  $user_settings->set('register', 'admin_only');
  $user_settings->save();
  // Inform the user about disabling the open registration.
  drupal_set_message(t('The SimpleSAMLphp Authentication module disabled the user registration. You can manually enable it again in the <a href=":user_settings_url">Account settings</a>.', [
  \Drupal::messenger()->addMessage(t('The SimpleSAMLphp Authentication module disabled the user registration. You can manually enable it again in the <a href=":user_settings_url">Account settings</a>.', [
    ':user_settings_url' => Url::fromRoute('entity.user.admin_form')->toString(),
  ]), 'warning');
  $config->save();
@@ -64,7 +64,7 @@ function simplesamlphp_auth_requirements($phase) {
        'severity'    => REQUIREMENT_INFO,
        'title'       => 'simpleSAMLphp_auth',
        'value'       => t('SimpleSAMLphp authentication is NOT activated'),
        'description' => t('It can be activated on the <a href=":config_page">configuration page</a>.', [':config_page' => \Drupal::url('simplesamlphp_auth.admin_settings')]),
        'description' => t('It can be activated on the <a href=":config_page">configuration page</a>.', [':config_page' => Url::fromRoute('simplesamlphp_auth.admin_settings')->toString()]),
      ];
    }
  }
+1 −1
Original line number Diff line number Diff line
@@ -4,7 +4,7 @@ services:
    arguments: ['@config.factory', '@current_user', '@router.admin_context', '@module_handler', '@request_stack', '@messenger']
  simplesamlphp_auth.drupalauth:
    class: Drupal\simplesamlphp_auth\Service\SimplesamlphpDrupalAuth
    arguments: ['@simplesamlphp_auth.manager', '@config.factory', '@entity_type.manager', '@logger.channel.simplesamlphp_auth', '@externalauth.externalauth', '@current_user']
    arguments: ['@simplesamlphp_auth.manager', '@config.factory', '@entity_type.manager', '@logger.channel.simplesamlphp_auth', '@externalauth.externalauth', '@current_user', '@messenger', '@module_handler']
  simplesamlphp_auth_event_subscriber:
    class: Drupal\simplesamlphp_auth\EventSubscriber\SimplesamlSubscriber
    arguments: ['@simplesamlphp_auth.manager', '@current_user', '@config.factory', '@logger.channel.simplesamlphp_auth', '@current_route_match']
+2 −1
Original line number Diff line number Diff line
@@ -5,6 +5,7 @@ namespace Drupal\simplesamlphp_auth\EventSubscriber;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Url;
use Drupal\simplesamlphp_auth\Service\SimplesamlphpAuthManager;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\KernelEvents;
@@ -133,7 +134,7 @@ class SimplesamlSubscriber implements EventSubscriberInterface {
    if ($this->account->isAnonymous() && $this->routeMatcht->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');
      $saml_login_path = Url::fromRoute('simplesamlphp_auth.saml_login')->toString();

      // Redirect directly to the external IdP.
      $response = new RedirectResponse($saml_login_path, RedirectResponse::HTTP_FOUND);
+38 −10
Original line number Diff line number Diff line
@@ -8,12 +8,17 @@ use Drupal\user\UserInterface;
use Drupal\Core\Session\AccountInterface;
use Psr\Log\LoggerInterface;
use Drupal\externalauth\ExternalAuthInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;

/**
 * Service to link SimpleSAMLphp authentication with Drupal users.
 */
class SimplesamlphpDrupalAuth {

  use StringTranslationTrait;

  /**
   * SimpleSAMLphp Authentication helper.
   *
@@ -56,6 +61,20 @@ class SimplesamlphpDrupalAuth {
   */
  protected $currentUser;

  /**
   * The messenger.
   *
   * @var \Drupal\Core\Messenger\MessengerInterface
   */
  protected $messenger;

  /**
   * The module handler service.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  protected $moduleHandler;

  /**
   * {@inheritdoc}
   *
@@ -71,14 +90,20 @@ class SimplesamlphpDrupalAuth {
   *   The ExternalAuth service.
   * @param \Drupal\Core\Session\AccountInterface $account
   *   The currently logged in user.
   * @param \Drupal\Core\Messenger\MessengerInterface $messenger
   *   The messenger.
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   The module handler service.
   */
  public function __construct(SimplesamlphpAuthManager $simplesaml_auth, ConfigFactoryInterface $config_factory, EntityTypeManagerInterface $entity_type_manager, LoggerInterface $logger, ExternalAuthInterface $externalauth, AccountInterface $account) {
  public function __construct(SimplesamlphpAuthManager $simplesaml_auth, ConfigFactoryInterface $config_factory, EntityTypeManagerInterface $entity_type_manager, LoggerInterface $logger, ExternalAuthInterface $externalauth, AccountInterface $account, MessengerInterface $messenger, ModuleHandlerInterface $module_handler) {
    $this->simplesamlAuth = $simplesaml_auth;
    $this->config = $config_factory->get('simplesamlphp_auth.settings');
    $this->entityTypeManager = $entity_type_manager;
    $this->logger = $logger;
    $this->externalauth = $externalauth;
    $this->currentUser = $account;
    $this->messenger = $messenger;
    $this->moduleHandler = $module_handler;
  }

  /**
@@ -127,7 +152,8 @@ class SimplesamlphpDrupalAuth {

      // We're not allowed to register new users on the site through simpleSAML.
      // We let the user know about this and redirect to the user/login page.
      drupal_set_message(t("We are sorry. While you have successfully authenticated, you are not yet entitled to access this site. Please ask the site administrator to provision access for you."));
      $this->messenger
        ->addMessage($this->t('We are sorry. While you have successfully authenticated, you are not yet entitled to access this site. Please ask the site administrator to provision access for you.'), 'status');
      $this->simplesamlAuth->logout(base_path());

      return FALSE;
@@ -157,7 +183,8 @@ class SimplesamlphpDrupalAuth {
        }
        // User is not permitted to login to Drupal via SAML.
        // Log out of SAML and redirect to the front page.
        drupal_set_message(t('We are sorry, your user account is not SAML enabled.'));
        $this->messenger
          ->addMessage($this->t('We are sorry, your user account is not SAML enabled.'), 'status');
        $this->simplesamlAuth->logout(base_path());
        return FALSE;
      }
@@ -169,8 +196,8 @@ class SimplesamlphpDrupalAuth {
        // Allow other modules to decide if there is an existing Drupal user,
        // based on the supplied SAML atttributes.
        $attributes = $this->simplesamlAuth->getAttributes();
        foreach (\Drupal::moduleHandler()->getImplementations('simplesamlphp_auth_existing_user') as $module) {
          $return_value = \Drupal::moduleHandler()->invoke($module, 'simplesamlphp_auth_existing_user', [$attributes]);
        foreach ($this->moduleHandler->getImplementations('simplesamlphp_auth_existing_user') as $module) {
          $return_value = $this->moduleHandler->invoke($module, 'simplesamlphp_auth_existing_user', [$attributes]);
          if ($return_value instanceof UserInterface) {
            $account = $return_value;
            if ($this->config->get('debug')) {
@@ -192,7 +219,8 @@ class SimplesamlphpDrupalAuth {
      }
      catch (\Exception $ex) {
        watchdog_exception('simplesamlphp_auth', $ex);
        drupal_set_message(t('Error registering user: An account with this username already exists.'), 'error');
        $this->messenger
          ->addMessage($this->t('Error registering user: An account with this username already exists.'), 'error');
      }
    }

@@ -224,7 +252,7 @@ class SimplesamlphpDrupalAuth {
          if ($this->currentUser->id() != $existing_account->id()) {
            $existing = TRUE;
            $this->logger->critical("Error on synchronizing name attribute for uid %new_uid: an account with the username %username and uid %existing_uid already exists.", ['%username' => $name, '%new_uid' => $this->currentUser->id(), '%existing_uid' => $existing_account->id()]);
            drupal_set_message(t('Error synchronizing username: an account with this username already exists.'), 'error');
            $this->messenger->addMessage($this->t('Error synchronizing username: an account with this username already exists.'), 'error');
          }
        }

@@ -234,7 +262,7 @@ class SimplesamlphpDrupalAuth {
      }
      else {
        $this->logger->critical("Error on synchronizing name attribute: no username available for Drupal user %id.", ['%id' => $account->id()]);
        drupal_set_message(t('Error synchronizing username: no username is provided by SAML.'), 'error');
        $this->messenger->addMessage($this->t('Error synchronizing username: no username is provided by SAML.'), 'error');
      }
    }

@@ -245,7 +273,7 @@ class SimplesamlphpDrupalAuth {
      }
      else {
        $this->logger->critical("Error on synchronizing mail attribute: no email address available for Drupal user %id.", ['%id' => $account->id()]);
        drupal_set_message(t('Error synchronizing mail: no email address is provided by SAML.'), 'error');
        $this->messenger->addMessage($this->t('Error synchronizing mail: no email address is provided by SAML.'), 'error');
      }
    }

@@ -308,7 +336,7 @@ class SimplesamlphpDrupalAuth {
    }

    $attributes = $this->simplesamlAuth->getAttributes();
    \Drupal::modulehandler()->alter('simplesamlphp_auth_user_roles', $roles, $attributes);
    $this->moduleHandler->alter('simplesamlphp_auth_user_roles', $roles, $attributes);
    return $roles;
  }

+40 −12
Original line number Diff line number Diff line
@@ -7,6 +7,7 @@ use Drupal\user\UserInterface;
use Drupal\simplesamlphp_auth\Service\SimplesamlphpDrupalAuth;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Messenger\MessengerInterface;

/**
 * SimplesamlphpDrupalAuth unit tests.
@@ -60,26 +61,47 @@ class SimplesamlphpDrupalAuthTest extends UnitTestCase {
   */
  protected $entityAccount;

  /**
   * A mocked messenger.
   *
   * @var \Drupal\Core\Messenger\MessengerInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $messenger;

  /**
   * A mocked ModuleHandlerInterface.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $moduleHandler;

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();

    // Create a Mock EntityManager object.
    $this->entityManager = $this->getMock('\Drupal\Core\Entity\EntityManagerInterface');

    // Create a Mock Logger object.
    $this->logger = $this->getMockBuilder('\Psr\Log\LoggerInterface')
      ->disableOriginalConstructor()
      ->getMock();

    // Create a Mock SimplesamlphpAuthManager object.
    $this->messenger = $this->getMockBuilder(MessengerInterface::class)
      ->disableOriginalConstructor()
      ->getMock();

    $this->moduleHandler = $this->getMockBuilder(ModuleHandlerInterface::class)
      ->disableOriginalConstructor()
      ->getMock();

    $this->moduleHandler->expects($this->any())
      ->method('alter');

    $this->simplesaml = $this->getMockBuilder('\Drupal\simplesamlphp_auth\Service\SimplesamlphpAuthManager')
      ->disableOriginalConstructor()
      ->getMock();

    // Set up default test configuration Mock object.
    $this->configFactory = $this->getConfigFactoryStub([
      'simplesamlphp_auth.settings' => [
        'register_users' => TRUE,
@@ -92,12 +114,6 @@ class SimplesamlphpDrupalAuthTest extends UnitTestCase {
    // Create a Mock User object to test against.
    $this->entityAccount = $this->getMock('Drupal\user\UserInterface');

    $container = new ContainerBuilder();
    $module_handler = $this->getMock(ModuleHandlerInterface::class);
    $module_handler->expects($this->any())
      ->method('alter');
    $container->set('module_handler', $module_handler);
    \Drupal::setContainer($container);
  }

  /**
@@ -122,6 +138,8 @@ class SimplesamlphpDrupalAuthTest extends UnitTestCase {
        $this->logger,
        $this->externalauth,
        $this->entityAccount,
        $this->messenger,
        $this->moduleHandler,
      ])
      ->getMock();

@@ -190,6 +208,8 @@ class SimplesamlphpDrupalAuthTest extends UnitTestCase {
        $this->logger,
        $this->externalauth,
        $this->entityAccount,
        $this->messenger,
        $this->moduleHandler,
      ])
      ->getMock();

@@ -238,6 +258,8 @@ class SimplesamlphpDrupalAuthTest extends UnitTestCase {
        $this->logger,
        $externalauth,
        $this->entityAccount,
        $this->messenger,
        $this->moduleHandler,
      ])
      ->getMock();

@@ -306,6 +328,8 @@ class SimplesamlphpDrupalAuthTest extends UnitTestCase {
        $this->logger,
        $externalauth,
        $this->entityAccount,
        $this->messenger,
        $this->moduleHandler,
      ])
      ->getMock();

@@ -364,7 +388,9 @@ class SimplesamlphpDrupalAuthTest extends UnitTestCase {
      $this->entityManager,
      $this->logger,
      $this->externalauth,
      $this->entityAccount
      $this->entityAccount,
      $this->messenger,
      $this->moduleHandler
    );

    $simplesaml_drupalauth->synchronizeUserAttributes($this->entityAccount, TRUE);
@@ -405,7 +431,9 @@ class SimplesamlphpDrupalAuthTest extends UnitTestCase {
      $this->entityManager,
      $this->logger,
      $this->externalauth,
      $this->entityAccount
      $this->entityAccount,
      $this->messenger,
      $this->moduleHandler
    );

    $matching_roles = $simplesaml_drupalauth->getMatchingRoles();