diff --git a/simplesamlphp_auth.install b/simplesamlphp_auth.install index 05020a6..456dd2c 100644 --- a/simplesamlphp_auth.install +++ b/simplesamlphp_auth.install @@ -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 Account settings.', [ + \Drupal::messenger()->addMessage(t('The SimpleSAMLphp Authentication module disabled the user registration. You can manually enable it again in the Account settings.', [ ':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 configuration page.', [':config_page' => \Drupal::url('simplesamlphp_auth.admin_settings')]), + 'description' => t('It can be activated on the configuration page.', [':config_page' => Url::fromRoute('simplesamlphp_auth.admin_settings')->toString()]), ]; } } diff --git a/simplesamlphp_auth.services.yml b/simplesamlphp_auth.services.yml index d90cadc..43dcc3e 100644 --- a/simplesamlphp_auth.services.yml +++ b/simplesamlphp_auth.services.yml @@ -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'] diff --git a/src/EventSubscriber/SimplesamlSubscriber.php b/src/EventSubscriber/SimplesamlSubscriber.php index 3bc47b9..9fef199 100644 --- a/src/EventSubscriber/SimplesamlSubscriber.php +++ b/src/EventSubscriber/SimplesamlSubscriber.php @@ -4,6 +4,7 @@ namespace Drupal\simplesamlphp_auth\EventSubscriber; use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Session\AccountInterface; +use Drupal\Core\Url; use Drupal\simplesamlphp_auth\Service\SimplesamlphpAuthManager; use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpKernel\KernelEvents; @@ -122,7 +123,7 @@ class SimplesamlSubscriber implements EventSubscriberInterface { 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'); + $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); diff --git a/src/Service/SimplesamlphpDrupalAuth.php b/src/Service/SimplesamlphpDrupalAuth.php index d8036ab..7481c9b 100644 --- a/src/Service/SimplesamlphpDrupalAuth.php +++ b/src/Service/SimplesamlphpDrupalAuth.php @@ -8,6 +8,8 @@ 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; /** * Service to link SimpleSAMLphp authentication with Drupal users. @@ -56,6 +58,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 +87,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 +149,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(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 +180,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(t('We are sorry, your user account is not SAML enabled.'), 'status'); $this->simplesamlAuth->logout(base_path()); return FALSE; } @@ -169,8 +193,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 +216,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(t('Error registering user: An account with this username already exists.'), 'error'); } } @@ -224,7 +249,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(t('Error synchronizing username: an account with this username already exists.'), 'error'); } } @@ -234,7 +259,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(t('Error synchronizing username: no username is provided by SAML.'), 'error'); } } @@ -245,7 +270,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(t('Error synchronizing mail: no email address is provided by SAML.'), 'error'); } } @@ -308,7 +333,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; }