Skip to content

Commit

Permalink
Merge pull request #1994 from danskernesdigitalebibliotek/DDFBRA-354-…
Browse files Browse the repository at this point in the history
…graphql-query-i-dpl-cms-til-at-hente-library-user-tokens

Add graphql query ro aquire tokens from Adgangsplatformen
  • Loading branch information
spaceo authored Jan 21, 2025
2 parents d530686 + ee14fd1 commit b092a74
Show file tree
Hide file tree
Showing 10 changed files with 206 additions and 0 deletions.
2 changes: 2 additions & 0 deletions config/sync/user.role.patron.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ dependencies:
- rest.resource.campaign.match
- rest.resource.proxy-url
module:
- graphql
- rest
id: patron
label: Patron
weight: 6
is_admin: null
permissions:
- 'execute graphql_compose_server arbitrary graphql requests'
- 'restful get proxy-url'
- 'restful post campaign:match'
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
extend type Adgangsplatformen {
library: String
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace Drupal\dpl_library_token\Plugin\GraphQL\DataProducer;

use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\dpl_library_token\LibraryTokenHandler;
use Drupal\graphql\Plugin\GraphQL\DataProducer\DataProducerPluginBase;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
* Exposes an Adgangsplatformen library token.
*
* If a token has been generated, it will be returned. Otherwise,
* NULL is returned.
*
* @DataProducer(
* id = "adgangsplatformen_library_token_producer",
* name = "Adgangsplatformen Library Token Producer",
* description = "Exposes access tokens.",
* produces = @ContextDefinition("any",
* label = "Request Response"
* )
* )
*/
class AdgangsplatformenLibraryTokenProducer extends DataProducerPluginBase implements ContainerFactoryPluginInterface {

/**
* Creates an instance of the producer using dependency injection.
*
* This method ensures the necessary services, such as the logger and importer
* are available for processing the import request.
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get(id: 'dpl_library_token.handler'),
);
}

/**
* {@inheritdoc}
*/
public function __construct(
array $configuration,
string $pluginId,
mixed $pluginDefinition,
protected LibraryTokenHandler $libraryTokenHandler,
) {
parent::__construct($configuration, $pluginId, $pluginDefinition);
}

/**
* Resolves the library access token.
*/
public function resolve(): string | null {
return $this->libraryTokenHandler->getToken();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Drupal\dpl_library_token\Plugin\GraphQL\SchemaExtension;

use Drupal\graphql\GraphQL\ResolverBuilder;
use Drupal\graphql\GraphQL\ResolverRegistryInterface;
use Drupal\graphql\Plugin\GraphQL\SchemaExtension\SdlSchemaExtensionPluginBase;

/**
* Dpl Token functionality.
*
* @SchemaExtension(
* id = "dpl_library_token",
* name = "Dpl Library Token extension",
* description = "Adding library token to the Dpl Token query.",
* schema = "graphql_compose"
* )
*/
class DplLibraryTokenExtension extends SdlSchemaExtensionPluginBase {

/**
* {@inheritdoc}
*/
public function registerResolvers(ResolverRegistryInterface $registry): void {
$builder = new ResolverBuilder();
$registry->addFieldResolver('Adgangsplatformen', 'library',
$builder->produce('adgangsplatformen_library_token_producer')
);
}

}
1 change: 1 addition & 0 deletions web/modules/custom/dpl_login/dpl_login.info.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ core_version_requirement: ^9 || ^10
dependencies:
- dpl_fbs:dpl_fbs
- openid_connect:openid_connect
- dpl_library_token:dpl_library_token
8 changes: 8 additions & 0 deletions web/modules/custom/dpl_login/graphql/dpl_tokens.base.graphqls
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

type Adgangsplatformen {
user: String
}

type DplTokens {
adgangsplatformen: Adgangsplatformen
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

extend type Query {
dplTokens: DplTokens
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace Drupal\dpl_login\Plugin\GraphQL\DataProducer;

use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\dpl_login\UserTokens;
use Drupal\graphql\Plugin\GraphQL\DataProducer\DataProducerPluginBase;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
* Exposes an Adgangsplatformen user token.
*
* If a patron has been authenticated an access token should be available.
* Otherwise NULL is returned.
*
* @DataProducer(
* id = "adgangsplatformen_user_token_producer",
* name = "Adgangsplatformen User Token Producer",
* description = "Exposes user access tokens.",
* produces = @ContextDefinition("any",
* label = "Request Response"
* )
* )
*/
class AdgangsplatformenUserTokenProducer extends DataProducerPluginBase implements ContainerFactoryPluginInterface {

/**
* Creates an instance of the producer using dependency injection.
*
* This method ensures the necessary services, such as the logger and importer
* are available for processing the import request.
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('dpl_login.user_tokens'),
);
}

/**
* {@inheritdoc}
*/
public function __construct(
array $configuration,
string $pluginId,
mixed $pluginDefinition,
protected UserTokens $userTokens,
) {
parent::__construct($configuration, $pluginId, $pluginDefinition);
}

/**
* Resolves the access token based on the token type.
*/
public function resolve(): string | null {
return $this->userTokens->getCurrent()?->token;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Drupal\dpl_login\Plugin\GraphQL\SchemaExtension;

use Drupal\graphql\GraphQL\ResolverBuilder;
use Drupal\graphql\GraphQL\ResolverRegistryInterface;
use Drupal\graphql\Plugin\GraphQL\SchemaExtension\SdlSchemaExtensionPluginBase;

/**
* Dpl Token functionality.
*
* @SchemaExtension(
* id = "dpl_tokens",
* name = "Dpl Tokens extension",
* description = "Adding Dpl Token functionality.",
* schema = "graphql_compose"
* )
*/
class DplTokensExtension extends SdlSchemaExtensionPluginBase {

/**
* {@inheritdoc}
*/
public function registerResolvers(ResolverRegistryInterface $registry): void {
$builder = new ResolverBuilder();
$registry->addFieldResolver('Query', 'dplTokens', $builder->callback(fn () => TRUE));
$registry->addFieldResolver('DplTokens', 'adgangsplatformen', $builder->callback(fn () => TRUE));
$registry->addFieldResolver('Adgangsplatformen', 'user',
$builder->produce('adgangsplatformen_user_token_producer')
);

}

}

0 comments on commit b092a74

Please sign in to comment.