-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1710 from danskernesdigitalebibliotek/DDFBRA-133-…
…create-graphql-consumer-on-deploy Added dpl_consumers module that handles the creation of a new graphql…
- Loading branch information
Showing
7 changed files
with
158 additions
and
0 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
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,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()); | ||
} | ||
} |
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,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(); | ||
} |
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,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 |
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,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(); | ||
} |
22 changes: 22 additions & 0 deletions
22
web/modules/custom/dpl_consumers/src/DplGraphqlConsumersConstants.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,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'; | ||
|
||
} |
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