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/Service/SimplesamlphpDrupalAuth.php b/src/Service/SimplesamlphpDrupalAuth.php index 6bdb8c5..7481c9b 100644 --- a/src/Service/SimplesamlphpDrupalAuth.php +++ b/src/Service/SimplesamlphpDrupalAuth.php @@ -8,13 +8,13 @@ use Drupal\user\UserInterface; use Drupal\Core\Session\AccountInterface; use Psr\Log\LoggerInterface; use Drupal\externalauth\ExternalAuthInterface; -use Drupal\Core\Messenger\MessengerTrait; +use Drupal\Core\Messenger\MessengerInterface; +use Drupal\Core\Extension\ModuleHandlerInterface; /** * Service to link SimpleSAMLphp authentication with Drupal users. */ class SimplesamlphpDrupalAuth { - use MessengerTrait; /** * SimpleSAMLphp Authentication helper. @@ -59,6 +59,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} * * @param SimplesamlphpAuthManager $simplesaml_auth @@ -73,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; } /** @@ -129,8 +149,7 @@ 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. - $this - ->messenger() + $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()); @@ -161,8 +180,7 @@ class SimplesamlphpDrupalAuth { } // User is not permitted to login to Drupal via SAML. // Log out of SAML and redirect to the front page. - $this - ->messenger() + $this->messenger ->addMessage(t('We are sorry, your user account is not SAML enabled.'), 'status'); $this->simplesamlAuth->logout(base_path()); return FALSE; @@ -175,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')) { @@ -198,8 +216,7 @@ class SimplesamlphpDrupalAuth { } catch (\Exception $ex) { watchdog_exception('simplesamlphp_auth', $ex); - $this - ->messenger() + $this->messenger ->addMessage(t('Error registering user: An account with this username already exists.'), 'error'); } } @@ -232,9 +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()]); - $this - ->messenger() - ->addMessage(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'); } } @@ -244,9 +259,7 @@ class SimplesamlphpDrupalAuth { } else { $this->logger->critical("Error on synchronizing name attribute: no username available for Drupal user %id.", ['%id' => $account->id()]); - $this - ->messenger() - ->addMessage(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'); } } @@ -257,9 +270,7 @@ class SimplesamlphpDrupalAuth { } else { $this->logger->critical("Error on synchronizing mail attribute: no email address available for Drupal user %id.", ['%id' => $account->id()]); - $this - ->messenger() - ->addMessage(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'); } } @@ -322,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; }