|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Drupal\quanthub_core\EventSubscriber; |
| 4 | + |
| 5 | +use Drupal\externalauth\Event\ExternalAuthEvents; |
| 6 | +use Drupal\externalauth\Event\ExternalAuthLoginEvent; |
| 7 | +use Drupal\oidc\OpenidConnectSessionInterface; |
| 8 | +use Drupal\oidc\Plugin\OpenidConnectRealm\GenericOpenidConnectRealm; |
| 9 | +use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
| 10 | + |
| 11 | +/** |
| 12 | + * Event subscriber to assign user roles. |
| 13 | + */ |
| 14 | +class OidcEventsSubscriber implements EventSubscriberInterface { |
| 15 | + |
| 16 | + /** |
| 17 | + * Roles mapping. |
| 18 | + * |
| 19 | + * @todo make configurable. |
| 20 | + */ |
| 21 | + const ROLES = [ |
| 22 | + 'DataPlatformBasic' => '', |
| 23 | + 'DataPlatformEnhanced' => '', |
| 24 | + 'DataPlatformMedia' => 'media', |
| 25 | + 'PortalContentEditor' => 'content_editor', |
| 26 | + 'AiAssistant' => 'ai', |
| 27 | + ]; |
| 28 | + |
| 29 | + /** |
| 30 | + * The OpenID Connect session service. |
| 31 | + * |
| 32 | + * @var \Drupal\oidc\OpenidConnectSessionInterface |
| 33 | + */ |
| 34 | + protected $session; |
| 35 | + |
| 36 | + /** |
| 37 | + * {@inheritdoc} |
| 38 | + */ |
| 39 | + public function __construct(OpenidConnectSessionInterface $session) { |
| 40 | + $this->session = $session; |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * {@inheritdoc} |
| 45 | + */ |
| 46 | + public static function getSubscribedEvents() { |
| 47 | + $events[ExternalAuthEvents::LOGIN][] = 'onLogin'; |
| 48 | + |
| 49 | + return $events; |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Updates the synced user roles on login. |
| 54 | + * |
| 55 | + * @param \Drupal\externalauth\Event\ExternalAuthLoginEvent $event |
| 56 | + * The login event. |
| 57 | + * |
| 58 | + * @throws \Drupal\Core\Entity\EntityStorageException |
| 59 | + */ |
| 60 | + public function onLogin(ExternalAuthLoginEvent $event) { |
| 61 | + $plugin_id = $this->session->getRealmPluginId(); |
| 62 | + $provider = 'oidc:' . $this->session->getRealmPluginId(); |
| 63 | + $roles_claim = $this->session->getJsonWebTokens()->getClaim('roles'); |
| 64 | + |
| 65 | + // The provider must match the realm and provide the claim. |
| 66 | + if (!$plugin_id || $provider !== $event->getProvider() || $roles_claim === NULL) { |
| 67 | + return; |
| 68 | + } |
| 69 | + |
| 70 | + $roles = []; |
| 71 | + if (is_array($roles_claim)) { |
| 72 | + foreach ($roles_claim as $role) { |
| 73 | + if (empty(self::ROLES[$role])) { |
| 74 | + continue; |
| 75 | + } |
| 76 | + $roles[] = self::ROLES[$role]; |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + // Only generic realms support this. |
| 81 | + $plugin = $this->session->getRealmPlugin(); |
| 82 | + if ($plugin instanceof GenericOpenidConnectRealm && $plugin->getDefaultRoleId()) { |
| 83 | + $roles[] = $plugin->getDefaultRoleId(); |
| 84 | + } |
| 85 | + |
| 86 | + $event->getAccount() |
| 87 | + ->set('roles', array_unique($roles)) |
| 88 | + ->save(); |
| 89 | + } |
| 90 | + |
| 91 | +} |
0 commit comments