Skip to content

Commit

Permalink
Delete old consumer and add new ones in new deploy hook
Browse files Browse the repository at this point in the history
  • Loading branch information
spaceo committed Dec 25, 2024
1 parent 844246e commit db790ef
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 4 deletions.
59 changes: 55 additions & 4 deletions web/modules/custom/dpl_consumers/dpl_consumers.deploy.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@

declare(strict_types=1);

use Drupal\dpl_consumers\Consumer;
use Drupal\dpl_consumers\ConsumerRole;
use Drupal\dpl_consumers\ConsumerUser;

require_once __DIR__ . '/dpl_consumers.crud.php';

/**
Expand All @@ -22,8 +26,55 @@
* as we don't want to create duplicates.
*/
function dpl_consumers_deploy_10001(): void {
dpl_consumers_delete_user();
dpl_consumers_delete_consumer();
dpl_consumers_create_user();
dpl_consumers_create_consumer();
// Noop. We don't need to do anything here.
// Since we have changed our minds.
// @see dpl_consumers_deploy_10002().
}

/**
* Expand the users, roles, consumers so they can handle both BNF and GO.
*
* So we delete previous entities and create new ones.
*/
function dpl_consumers_deploy_10002(): void {
// Delete consume and users that we want to handle differently.
// We want to create consumers and consumer users
// specifically for the two known consumers (BNF and Go) and connected users.
Consumer::deleteByClientId("graphql_consumer");
(new ConsumerUser('GraphQL Consumer', ''))->delete();
(new ConsumerUser('graphql_consumer', ''))->delete();

// Check if we have the necessary secrets for new consumers and their users.
if (!$bnf_consumer_secret = getenv('BNF_GRAPHQL_CONSUMER_SECRET')) {
throw new \Exception('BNF_GRAPHQL_CONSUMER_SECRET not found.');
}
if (!$go_consumer_secret = getenv('GO_GRAPHQL_CONSUMER_SECRET')) {
throw new \Exception('GO_GRAPHQL_CONSUMER_SECRET not found.');
}
if (!$bnf_consumer_user_password = getenv('BNF_GRAPHQL_CONSUMER_USER_PASSWORD')) {
throw new \Exception('BNF_GRAPHQL_CONSUMER_USER_PASSWORD not found.');
}
if (!$go_consumer_user_password = getenv('GO_GRAPHQL_CONSUMER_USER_PASSWORD')) {
throw new \Exception('GO_GRAPHQL_CONSUMER_USER_PASSWORD not found.');
}

// Create new consumers (BNF and Go) and their users and roles.
/** @var \Drupal\dpl_consumers\Services\ConsumerHandler $handler */
$consumer_handler = \Drupal::service('dpl_consumers.consumer_handler');
$consumers = [
[
'consumer' => new Consumer('BNF GraphQL', 'bnf_graphql', 'bnf_graphql', $bnf_consumer_secret),
'user' => new ConsumerUser('bnf_graphql', $bnf_consumer_user_password),
'role' => new ConsumerRole('bnf_graphql_client'),
],
[
'consumer' => new Consumer('GO GraphQL', 'go_graphql', 'go_graphql', $go_consumer_secret),
'user' => new ConsumerUser('go_graphql', $go_consumer_user_password),
'role' => new ConsumerRole('go_graphql_client'),
],
];

foreach ($consumers as $consumer) {
$consumer_handler->setComponents($consumer['consumer'], $consumer['user'], $consumer['role'])->create();
}
}
49 changes: 49 additions & 0 deletions web/modules/custom/dpl_consumers/dpl_consumers.module
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,52 @@ function dpl_consumers_get_consumer_uuid(string $client_id): string {
throw new \Exception($e->getMessage());
}
}

/**
*
*/
function mikTest() {
Consumer::delete("graphql_consumer");
(new ConsumerUser('GraphQL Consumer', ''))->delete();
(new ConsumerUser('graphql_consumer', ''))->delete();
(new ConsumerRole('graphql_consumer'))->delete();

// @todo Delete this block.
putenv("BNF_GRAPHQL_CONSUMER_SECRET=dUOAxbWr72cnigpkZXN1SuyuXfeHxZb5");
putenv("BNF_GRAPHQL_CONSUMER_USER_PASSWORD=dUOAxbWr72cnigpkZXN1SuyuXfeHxZb5");
putenv("GO_GRAPHQL_CONSUMER_SECRET=GVOzhK8BogbZHrEzXW0W5HYFtScQrABt");
putenv("GO_GRAPHQL_CONSUMER_USER_PASSWORD=oA8mqZFxmFubREH77T3qeApeoUxoNS7Y");

if (!$bnf_consumer_secret = getenv('BNF_GRAPHQL_CONSUMER_SECRET')) {
throw new \Exception('BNF_GRAPHQL_CONSUMER_SECRET not found.');
}
if (!$go_consumer_secret = getenv('GO_GRAPHQL_CONSUMER_SECRET')) {
throw new \Exception('GO_GRAPHQL_CONSUMER_SECRET not found.');
}
if (!$bnf_consumer_user_password = getenv('BNF_GRAPHQL_CONSUMER_USER_PASSWORD')) {
throw new \Exception('BNF_GRAPHQL_CONSUMER_USER_PASSWORD not found.');
}
if (!$go_consumer_user_password = getenv('GO_GRAPHQL_CONSUMER_USER_PASSWORD')) {
throw new \Exception('GO_GRAPHQL_CONSUMER_USER_PASSWORD not found.');
}

/** @var \Drupal\dpl_consumers\Services\ConsumerHandler $handler */
$consumer_handler = \Drupal::service('dpl_consumers.consumer_handler');
$consumers = [
[
'consumer' => new Consumer('BNF GraphQL', 'bnf_graphql', 'bnf_graphql', $bnf_consumer_secret),
'user' => new ConsumerUser('bnf_graphql', $bnf_consumer_user_password),
'role' => new ConsumerRole('bnf_graphql_client'),
],
[
'consumer' => new Consumer('GO GraphQL', 'go_graphql', 'go_graphql', $go_consumer_secret),
'user' => new ConsumerUser('go_graphql', $go_consumer_user_password),
'role' => new ConsumerRole('go_graphql_client'),
],
];

foreach ($consumers as $consumer) {
$consumer_handler->setComponents($consumer['consumer'], $consumer['user'], $consumer['role'])->create();
}

}

0 comments on commit db790ef

Please sign in to comment.