You are here

class LtiToolProviderRolesEventSubscriber in LTI Tool Provider 8

Same name and namespace in other branches
  1. 2.x modules/lti_tool_provider_roles/src/EventSubscriber/LtiToolProviderRolesEventSubscriber.php \Drupal\lti_tool_provider_roles\EventSubscriber\LtiToolProviderRolesEventSubscriber

Hierarchy

Expanded class hierarchy of LtiToolProviderRolesEventSubscriber

1 string reference to 'LtiToolProviderRolesEventSubscriber'
lti_tool_provider_roles.services.yml in modules/lti_tool_provider_roles/lti_tool_provider_roles.services.yml
modules/lti_tool_provider_roles/lti_tool_provider_roles.services.yml
1 service uses LtiToolProviderRolesEventSubscriber
lti_tool_provider_roles.event_subscriber in modules/lti_tool_provider_roles/lti_tool_provider_roles.services.yml
Drupal\lti_tool_provider_roles\EventSubscriber\LtiToolProviderRolesEventSubscriber

File

modules/lti_tool_provider_roles/src/EventSubscriber/LtiToolProviderRolesEventSubscriber.php, line 14

Namespace

Drupal\lti_tool_provider_roles\EventSubscriber
View source
class LtiToolProviderRolesEventSubscriber implements EventSubscriberInterface {

  /**
   * @var ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * @var EventDispatcherInterface
   */
  protected $eventDispatcher;

  /**
   * LtiToolProviderRolesEventSubscriber constructor.
   * @param ConfigFactoryInterface $configFactory
   * @param EventDispatcherInterface $eventDispatcher
   */
  public function __construct(ConfigFactoryInterface $configFactory, EventDispatcherInterface $eventDispatcher) {
    $this->configFactory = $configFactory;
    $this->eventDispatcher = $eventDispatcher;
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() : array {
    return [
      LtiToolProviderAuthenticatedEvent::EVENT_NAME => 'onAuthenticated',
    ];
  }

  /**
   * @param LtiToolProviderAuthenticatedEvent $event
   */
  public function onAuthenticated(LtiToolProviderAuthenticatedEvent $event) {
    $mapped_roles = Drupal::config('lti_tool_provider_roles.settings')
      ->get('mapped_roles');
    $context = $event
      ->getContext();
    $user = $event
      ->getUser();
    $user_roles = user_roles(true);
    $lti_roles = parse_roles($context['roles']);
    if ($user
      ->getDisplayName() === 'ltiuser') {
      return;
    }
    if (!$mapped_roles || !count($mapped_roles)) {
      return;
    }
    foreach ($mapped_roles as $user_role => $lti_role) {
      if (array_key_exists($user_role, $user_roles)) {
        if (in_array($lti_role, $lti_roles)) {
          $user
            ->addRole($user_role);
        }
        else {
          $user
            ->removeRole($user_role);
        }
      }
    }
    try {
      $rolesEvent = new LtiToolProviderRolesEvent($context, $user);
      LtiToolProviderEvent::dispatchEvent($this->eventDispatcher, $rolesEvent);
      if ($rolesEvent
        ->isCancelled()) {
        throw new Exception($event
          ->getMessage());
      }
      $user
        ->save();
    } catch (Exception $e) {
      Drupal::logger('lti_tool_provider_roles')
        ->error($e
        ->getMessage());
    }
  }

}

Members