Skip to content

Commit

Permalink
Merge pull request #1762 from danskernesdigitalebibliotek/DDFBRA-221-…
Browse files Browse the repository at this point in the history
…update-consumer-creation-after-adding-simple-oauth

DDBRA-221  - Update consumer creation after adding simple oauth
  • Loading branch information
Dresse authored Nov 22, 2024
2 parents 47fbfe3 + e0e6f53 commit 67e91b1
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 13 deletions.
4 changes: 4 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
COMPOSE_PROJECT_NAME=dpl-cms
COMPOSER_MEMORY_LIMIT=-1

# GraphQL Consumer environment variables used for local development
GRAPHQL_CONSUMER_SECRET=some-secret

#OPENID_CLIENT_ID=some-client-id
#OPENID_CLIENT_SECRET=some-client-secret
#OPENID_AGENCY_ID=some-agency-id
5 changes: 5 additions & 0 deletions Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,11 @@ tasks:
cmds:
- cmd: task dev:cli -- drush default-content:export-module dpl_example_content

dev:go:set-graphql-secret:
desc: Sets the GraphQL consumer secret based on the .env variable.
cmds:
- cmd: task dev:cli -- drush dpl_consumers:set-consumer-secret

dev:go:graphql-credentials:
desc: Get the GraphQL consumer credentials
cmds:
Expand Down
38 changes: 28 additions & 10 deletions web/modules/custom/dpl_consumers/dpl_consumers.crud.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,38 @@ function dpl_consumers_create_user(): void {
* Create a consumer.
*/
function dpl_consumers_create_consumer(): void {
$consumer = \Drupal::entityTypeManager()->getStorage('consumer')->create([
'label' => DplGraphqlConsumersConstants::GRAPHQL_CONSUMER_CONSUMER_LABEL,
'id' => DplGraphqlConsumersConstants::GRAPHQL_CONSUMER_CONSUMER_ID,
'client_id' => DplGraphqlConsumersConstants::GRAPHQL_CONSUMER_CLIENT_ID,
'third_party' => FALSE,
]);
$user = \Drupal::entityTypeManager()
->getStorage('user')
->loadByProperties(['name' => DplGraphqlConsumersConstants::GRAPHQL_CONSUMER_USER_NAME]);

$user = reset($user);

if (empty($user)) {
return;
}

$consumer->save();
$secret = getenv('GRAPHQL_CONSUMER_SECRET');

if ($secret) {
$consumer = \Drupal::entityTypeManager()->getStorage('consumer')->create([
'label' => DplGraphqlConsumersConstants::GRAPHQL_CONSUMER_LABEL,
'id' => DplGraphqlConsumersConstants::GRAPHQL_CONSUMER_CONSUMER_ID,
'client_id' => DplGraphqlConsumersConstants::GRAPHQL_CONSUMER_CLIENT_ID,
'secret' => $secret,
'third_party' => FALSE,
'user_id' => $user->id(),
'roles' => [DplGraphqlConsumersConstants::GRAPHQL_CONSUMER_ROLE_ID],
]);

$consumer->save();
}
}

/**
* Delete the user.
*/
function dpl_consumers_delete_user(): void {
try {
// Delete the user.
$user = \Drupal::entityTypeManager()
->getStorage('user')
->loadByProperties(['name' => DplGraphqlConsumersConstants::GRAPHQL_CONSUMER_USER_NAME]);
Expand All @@ -61,17 +77,19 @@ function dpl_consumers_delete_user(): void {
*/
function dpl_consumers_delete_consumer(): void {
try {
// Delete the consumer.
$consumer = \Drupal::entityTypeManager()
->getStorage('consumer')
->loadByProperties(['label' => DplGraphqlConsumersConstants::GRAPHQL_CONSUMER_CONSUMER_LABEL]);
->loadByProperties(['client_id' => DplGraphqlConsumersConstants::GRAPHQL_CONSUMER_CLIENT_ID]);

// We assume that there is only one consumer with the given client ID
// as it is used an as unique identifier (machine name).
if (!empty($consumer)) {
$consumer = reset($consumer);
$consumer->delete();
}
}
catch (\Exception $e) {
// We just log here in the deletion. It's not critical if it fails.
\Drupal::logger('dpl_consumers')->error($e->getMessage());
}
}
9 changes: 7 additions & 2 deletions web/modules/custom/dpl_consumers/dpl_consumers.deploy.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,17 @@
* Run consumer creation on deploy.
*
* We run the user and consumer creation in a deploy_hook instead of
* a install_hook, as we run into problems with the password_policy
* an install_hook, as we run into problems with the password_policy
* module not being installed when we try to create the user. Doing
* it this way instead, we make sure that everything is ready for the
* user and consumer creation.
*
* We make sure to delete already existing user and consumers before
* as we don't want to create duplicates.
*/
function dpl_consumers_deploy_create_user(): void {
function dpl_consumers_deploy_10001(): void {
dpl_consumers_delete_user();
dpl_consumers_delete_consumer();
dpl_consumers_create_user();
dpl_consumers_create_consumer();
}
1 change: 1 addition & 0 deletions web/modules/custom/dpl_consumers/dpl_consumers.info.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ core_version_requirement: ^10 || ^11
dependencies:
- drupal:user
- consumers:consumers
- simple_oauth:simple_oauth
2 changes: 2 additions & 0 deletions web/modules/custom/dpl_consumers/drush.services.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
services:
dpl_consumer.commands:
class: \Drupal\dpl_consumers\Commands\DplConsumersCommands
arguments:
- '@entity_type.manager'
tags:
- { name: drush.command }
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Drupal\dpl_consumers\Commands;

use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\dpl_consumers\DplGraphqlConsumersConstants;
use Drush\Commands\DrushCommands;

Expand All @@ -12,10 +13,61 @@
*/
final class DplConsumersCommands extends DrushCommands {

/**
* The entity type manager service.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected EntityTypeManagerInterface $entityTypeManager;

/**
* Constructor.
*/
public function __construct(
EntityTypeManagerInterface $entityTypeManager,
) {
parent::__construct();
$this->entityTypeManager = $entityTypeManager;
}

/**
* Function is used for setting the consumer secret.
*
* @command dpl_consumers:set-consumer-secret
*
* @throws \Exception
*/
public function setConsumerSecret(): string {
try {
/** @var \Drupal\Core\Entity\EntityStorageInterface $consumer */
$consumer = $this->entityTypeManager
->getStorage('consumer')
->loadByProperties(['client_id' => DplGraphqlConsumersConstants::GRAPHQL_CONSUMER_CLIENT_ID]);

$consumer = reset($consumer);

$secret = getenv('GRAPHQL_CONSUMER_SECRET');
if (!empty($secret)) {
$consumer->secret = $secret;
$consumer->save();

return 'Consumer secret set successfully.';
}
else {
throw new \Exception('Consumer secret was not found.');
}
}
catch (\Exception $e) {
throw new \Exception($e->getMessage());
}
}

/**
* Function is used for printing out the consumer credentials to the console.
*
* @command dpl_consumers:consumer-credentials
*
* @throws \Exception
*/
public function getConsumerCredentials(): void {
$graphql_consumer_client_id = DplGraphqlConsumersConstants::GRAPHQL_CONSUMER_CLIENT_ID;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class DplGraphqlConsumersConstants {

const GRAPHQL_CONSUMER_USER_NAME = 'GraphQL Consumer';

const GRAPHQL_CONSUMER_CONSUMER_LABEL = 'GraphQL Consumer';
const GRAPHQL_CONSUMER_LABEL = 'GraphQL Consumer';

const GRAPHQL_CONSUMER_CONSUMER_ID = 'graphql_consumer';

Expand Down

0 comments on commit 67e91b1

Please sign in to comment.