SimplesamlExternalauthSubscriber.php in simpleSAMLphp Authentication 8.3
File
src/EventSubscriber/SimplesamlExternalauthSubscriber.php
View source
<?php
namespace Drupal\simplesamlphp_auth\EventSubscriber;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\user\UserInterface;
use Drupal\simplesamlphp_auth\Service\SimplesamlphpDrupalAuth;
use Drupal\simplesamlphp_auth\Service\SimplesamlphpAuthManager;
use Drupal\externalauth\Event\ExternalAuthEvents;
use Drupal\externalauth\Event\ExternalAuthLoginEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Psr\Log\LoggerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
class SimplesamlExternalauthSubscriber implements EventSubscriberInterface {
protected $simplesaml;
public $simplesamlDrupalauth;
protected $config;
protected $logger;
protected $moduleHandler;
public function __construct(SimplesamlphpAuthManager $simplesaml, SimplesamlphpDrupalAuth $simplesaml_drupalauth, ConfigFactoryInterface $config_factory, LoggerInterface $logger, ModuleHandlerInterface $module_handler) {
$this->simplesaml = $simplesaml;
$this->simplesamlDrupalauth = $simplesaml_drupalauth;
$this->config = $config_factory
->get('simplesamlphp_auth.settings');
$this->logger = $logger;
$this->moduleHandler = $module_handler;
}
public function externalauthLogin(ExternalAuthLoginEvent $event) {
if ($event
->getProvider() == "simplesamlphp_auth") {
if (!$this->simplesaml
->isActivated()) {
return;
}
if (!$this->simplesaml
->isAuthenticated()) {
return;
}
$account = $event
->getAccount();
$this->simplesamlDrupalauth
->synchronizeUserAttributes($account);
$account_altered = FALSE;
$attributes = $this->simplesaml
->getAttributes();
foreach ($this->moduleHandler
->getImplementations('simplesamlphp_auth_user_attributes') as $module) {
$return_value = $this->moduleHandler
->invoke($module, 'simplesamlphp_auth_user_attributes', [
$account,
$attributes,
]);
if ($return_value instanceof UserInterface) {
if ($this->config
->get('debug')) {
$this->logger
->debug('Drupal user attributes have altered based on SAML attributes by %module module.', [
'%module' => $module,
]);
}
$account_altered = TRUE;
$account = $return_value;
}
}
if ($account_altered) {
$account
->save();
}
}
}
public static function getSubscribedEvents() {
$events[ExternalAuthEvents::LOGIN][] = [
'externalauthLogin',
];
return $events;
}
}