You are here

public function LtiToolProviderAttributesEventSubscriber::onAuthenticated in LTI Tool Provider 8

Same name and namespace in other branches
  1. 2.x modules/lti_tool_provider_attributes/src/EventSubscriber/LtiToolProviderAttributesEventSubscriber.php \Drupal\lti_tool_provider_attributes\EventSubscriber\LtiToolProviderAttributesEventSubscriber::onAuthenticated()

Parameters

LtiToolProviderAuthenticatedEvent $event:

File

modules/lti_tool_provider_attributes/src/EventSubscriber/LtiToolProviderAttributesEventSubscriber.php, line 52

Class

LtiToolProviderAttributesEventSubscriber

Namespace

Drupal\lti_tool_provider_attributes\EventSubscriber

Code

public function onAuthenticated(LtiToolProviderAuthenticatedEvent $event) {
  $mapped_attributes = $this->configFactory
    ->get('lti_tool_provider_attributes.settings')
    ->get('mapped_attributes');
  $context = $event
    ->getContext();
  $user = $event
    ->getUser();
  if ($user
    ->getDisplayName() === 'ltiuser') {
    return;
  }
  if (!$mapped_attributes || !count($mapped_attributes)) {
    return;
  }
  foreach ($mapped_attributes as $user_attribute => $lti_attribute) {
    if (isset($context[$mapped_attributes[$user_attribute]]) && !empty($context[$mapped_attributes[$user_attribute]])) {
      $user
        ->set($user_attribute, $context[$mapped_attributes[$user_attribute]]);
    }
  }
  try {
    $attributesEvent = new LtiToolProviderAttributesEvent($context, $user);
    LtiToolProviderEvent::dispatchEvent($this->eventDispatcher, $attributesEvent);
    if ($attributesEvent
      ->isCancelled()) {
      throw new Exception($event
        ->getMessage());
    }
    $user
      ->save();
  } catch (Exception $e) {
    Drupal::logger('lti_tool_provider_attributes')
      ->error($e
      ->getMessage());
  }
}