Skip to content

Commit

Permalink
Merge pull request #1710 from danskernesdigitalebibliotek/DDFBRA-133-…
Browse files Browse the repository at this point in the history
…create-graphql-consumer-on-deploy

Added dpl_consumers module that handles the creation of a new graphql…
  • Loading branch information
Dresse authored Nov 4, 2024
2 parents 5c84e02 + e406f7b commit 95b7e09
Show file tree
Hide file tree
Showing 7 changed files with 158 additions and 0 deletions.
1 change: 1 addition & 0 deletions config/sync/core.extension.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ module:
dpl_breadcrumb: 0
dpl_cache_settings: 0
dpl_campaign: 0
dpl_consumers: 0
dpl_cookies: 0
dpl_dashboard: 0
dpl_error_pages: 0
Expand Down
77 changes: 77 additions & 0 deletions web/modules/custom/dpl_consumers/dpl_consumers.crud.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

/**
* @file
* Contains functions to create and delete users and consumers.
*/

use Drupal\dpl_consumers\DplGraphqlConsumersConstants;
use Drupal\user\Entity\User;

/**
* Create a user with the GraphQL consumer role.
*/
function dpl_consumers_create_user(): void {
$user = User::create([
'name' => DplGraphqlConsumersConstants::GRAPHQL_CONSUMER_USER_NAME,
'status' => 1,
]);

$user->addRole(DplGraphqlConsumersConstants::GRAPHQL_CONSUMER_ROLE_ID);
$user->save();
}

/**
* 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,
]);

$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]);

if (!empty($user)) {
$user = reset($user);
$user->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());
}
}

/**
* Delete the consumer.
*/
function dpl_consumers_delete_consumer(): void {
try {
// Delete the consumer.
$consumer = \Drupal::entityTypeManager()
->getStorage('consumer')
->loadByProperties(['label' => DplGraphqlConsumersConstants::GRAPHQL_CONSUMER_CONSUMER_LABEL]);

if (!empty($consumer)) {
$consumer = reset($consumer);
$consumer->delete();
}
}
catch (\Exception $e) {
\Drupal::logger('dpl_consumers')->error($e->getMessage());
}
}
24 changes: 24 additions & 0 deletions web/modules/custom/dpl_consumers/dpl_consumers.deploy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

/**
* @file
* Deploy hooks for dpl_consumers module.
*/

declare(strict_types=1);

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

/**
* 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
* 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.
*/
function dpl_consumers_deploy_create_user(): void {
dpl_consumers_create_user();
dpl_consumers_create_consumer();
}
8 changes: 8 additions & 0 deletions web/modules/custom/dpl_consumers/dpl_consumers.info.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
name: "DPL Consumers"
type: module
description: "Module for adding consumers to DPL CMS"
package: DPL
core_version_requirement: ^10 || ^11
dependencies:
- drupal:user
- consumers:consumers
18 changes: 18 additions & 0 deletions web/modules/custom/dpl_consumers/dpl_consumers.install
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

/**
* @file
* Install, update and uninstall functions for the dpl_consumers module.
*/

declare(strict_types=1);

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

/**
* Implements hook_uninstall().
*/
function dpl_consumers_uninstall(): void {
dpl_consumers_delete_user();
dpl_consumers_delete_consumer();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace Drupal\dpl_consumers;

/**
* DPL consumer constants.
*/
class DplGraphqlConsumersConstants {

const GRAPHQL_CONSUMER_ROLE_ID = 'graphql_consumer';

const GRAPHQL_CONSUMER_USER_NAME = 'GraphQL Consumer';

const GRAPHQL_CONSUMER_CONSUMER_LABEL = 'GraphQL Consumer';

const GRAPHQL_CONSUMER_CONSUMER_ID = 'graphql_consumer';

const GRAPHQL_CONSUMER_CLIENT_ID = 'graphql_consumer';

}
8 changes: 8 additions & 0 deletions web/modules/custom/dpl_update/dpl_update.install
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ function dpl_update_install(): string {
$messages[] = dpl_update_update_10021();
$messages[] = dpl_update_update_10022();
$messages[] = dpl_update_update_10023();
$messages[] = dpl_update_update_10024();

return implode('\r\n', $messages);
}
Expand Down Expand Up @@ -286,3 +287,10 @@ function dpl_update_update_10022(): string {
function dpl_update_update_10023(): string {
return _dpl_update_install_modules(['dpl_graphql']);
}

/**
* Install dpl_consumers.
*/
function dpl_update_update_10024(): string {
return _dpl_update_install_modules(['dpl_consumers']);
}

0 comments on commit 95b7e09

Please sign in to comment.