You are here

public function SimplesamlExternalauthSubscriber::externalauthLogin in simpleSAMLphp Authentication 8.3

React on an ExternalAuth login event.

Parameters

\Drupal\externalauth\Event\ExternalAuthLoginEvent $event: The subscribed event.

File

src/EventSubscriber/SimplesamlExternalauthSubscriber.php, line 83

Class

SimplesamlExternalauthSubscriber
Event subscriber subscribing to ExternalAuthEvents.

Namespace

Drupal\simplesamlphp_auth\EventSubscriber

Code

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);

    // Invoke a hook to let other modules alter the user account based on
    // SimpleSAMLphp attributes.
    $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();
    }
  }
}