-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
175 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
modules/quanthub_chat_overlay/quanthub_chat_overlay.permissions.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
access ai: | ||
title: 'Access AI tools' | ||
description: 'Access AI chat overlay and features.' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
modules/quanthub_core/src/Cache/AllowedDatasetsCacheContext.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
|
||
namespace Drupal\quanthub_core\Cache; | ||
|
||
use Drupal\Core\Cache\CacheableMetadata; | ||
use Drupal\Core\Cache\Context\CacheContextInterface; | ||
use Drupal\Core\Cache\Context\UserCacheContextBase; | ||
use Drupal\Core\Session\AccountInterface; | ||
use Drupal\quanthub_core\AllowedContentManager; | ||
|
||
/** | ||
* Defines the User Allowed Datasets cache context service. | ||
* | ||
* Cache context ID: 'user.datasets'. | ||
*/ | ||
class AllowedDatasetsCacheContext extends UserCacheContextBase implements CacheContextInterface { | ||
|
||
/** | ||
* The Allowed Content Manager service. | ||
* | ||
* @var \Drupal\quanthub_core\AllowedContentManager | ||
*/ | ||
protected $allowedContentManager; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function __construct(AccountInterface $user, AllowedContentManager $allowed_content_manager) { | ||
parent::__construct($user); | ||
|
||
$this->allowedContentManager = $allowed_content_manager; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public static function getLabel() { | ||
return t('Allowed Datasets'); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getContext() { | ||
$datasets = NULL; | ||
if (getenv('WSO_IGNORE') !== 'TRUE') { | ||
$datasets = $this->allowedContentManager->getAllowedDatasetList(); | ||
sort($datasets); | ||
} | ||
// We don't need to secure this information, crc32 is enough. | ||
return hash('crc32', serialize($datasets)); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getCacheableMetadata() { | ||
return (new CacheableMetadata())->setCacheTags(['user:' . $this->user->id()]); | ||
} | ||
|
||
} |
93 changes: 93 additions & 0 deletions
93
modules/quanthub_core/src/EventSubscriber/OidcEventsSubscriber.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
<?php | ||
|
||
namespace Drupal\quanthub_core\EventSubscriber; | ||
|
||
use Drupal\externalauth\Event\ExternalAuthEvents; | ||
use Drupal\externalauth\Event\ExternalAuthLoginEvent; | ||
use Drupal\oidc\OpenidConnectSessionInterface; | ||
use Drupal\oidc\Plugin\OpenidConnectRealm\GenericOpenidConnectRealm; | ||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
|
||
/** | ||
* Event subscriber to assign user roles. | ||
*/ | ||
class OidcEventsSubscriber implements EventSubscriberInterface { | ||
|
||
/** | ||
* Roles mapping. | ||
* | ||
* @todo make configurable. | ||
*/ | ||
const ROLES = [ | ||
'Quanthub.ExternalUsers' => '', | ||
'Quanthub.Users' => '', | ||
'DataPlatformBasic' => '', | ||
'DataPlatformEnhanced' => '', | ||
'DataPlatformMedia' => 'media', | ||
'PortalContentEditor' => 'content_editor', | ||
'AiAssistant' => 'ai', | ||
]; | ||
|
||
/** | ||
* The OpenID Connect session service. | ||
* | ||
* @var \Drupal\oidc\OpenidConnectSessionInterface | ||
*/ | ||
protected $session; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function __construct(OpenidConnectSessionInterface $session) { | ||
$this->session = $session; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public static function getSubscribedEvents() { | ||
$events[ExternalAuthEvents::LOGIN][] = 'onLogin'; | ||
|
||
return $events; | ||
} | ||
|
||
/** | ||
* Updates the synced user roles on login. | ||
* | ||
* @param \Drupal\externalauth\Event\ExternalAuthLoginEvent $event | ||
* The login event. | ||
* | ||
* @throws \Drupal\Core\Entity\EntityStorageException | ||
*/ | ||
public function onLogin(ExternalAuthLoginEvent $event) { | ||
$plugin_id = $this->session->getRealmPluginId(); | ||
$provider = 'oidc:' . $this->session->getRealmPluginId(); | ||
$roles_claim = $this->session->getJsonWebTokens()->getClaim('roles'); | ||
|
||
// The provider must match the realm and provide the claim. | ||
if (!$plugin_id || $provider !== $event->getProvider() || $roles_claim === NULL) { | ||
return; | ||
} | ||
|
||
$roles = []; | ||
if (is_array($roles_claim)) { | ||
foreach ($roles_claim as $role) { | ||
if (empty(self::ROLES[$role])) { | ||
continue; | ||
} | ||
$roles[] = self::ROLES[$role]; | ||
} | ||
} | ||
|
||
// Only generic realms support this. | ||
$plugin = $this->session->getRealmPlugin(); | ||
if ($plugin instanceof GenericOpenidConnectRealm && $plugin->getDefaultRoleId()) { | ||
$roles[] = $plugin->getDefaultRoleId(); | ||
} | ||
|
||
$event->getAccount() | ||
->set('roles', array_unique($roles)) | ||
->save(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters