class LtiToolProviderRolesEventSubscriber in LTI Tool Provider 2.x
Same name and namespace in other branches
- 8 modules/lti_tool_provider_roles/src/EventSubscriber/LtiToolProviderRolesEventSubscriber.php \Drupal\lti_tool_provider_roles\EventSubscriber\LtiToolProviderRolesEventSubscriber
Hierarchy
- class \Drupal\lti_tool_provider_roles\EventSubscriber\LtiToolProviderRolesEventSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface
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
File
- modules/
lti_tool_provider_roles/ src/ EventSubscriber/ LtiToolProviderRolesEventSubscriber.php, line 14
Namespace
Drupal\lti_tool_provider_roles\EventSubscriberView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
LtiToolProviderRolesEventSubscriber:: |
protected | property | ||
LtiToolProviderRolesEventSubscriber:: |
protected | property | ||
LtiToolProviderRolesEventSubscriber:: |
public static | function | ||
LtiToolProviderRolesEventSubscriber:: |
public | function | ||
LtiToolProviderRolesEventSubscriber:: |
public | function | LtiToolProviderRolesEventSubscriber constructor. |