Skip to content

Commit

Permalink
refactor: move consumer setup in template
Browse files Browse the repository at this point in the history
  • Loading branch information
colorfield committed Nov 1, 2023
1 parent 96fa474 commit 6e1fc32
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 1 deletion.
1 change: 0 additions & 1 deletion apps/cms/config/sync/core.extension.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ module:
silverback_cloudinary: 0
silverback_external_preview: 0
silverback_gatsby: 0
silverback_gatsby_oauth: 0
silverback_graphql_persisted: 0
silverback_gutenberg: 0
silverback_iframe: 0
Expand Down
58 changes: 58 additions & 0 deletions packages/drupal/custom/custom.deploy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

/**
* Set up the Publisher OAuth Consumer and delete the Default one.
*/
function custom_deploy_set_consumers(array &$sandbox): string {
// Skip for Silverback environments.
// It might be used for OAuth development purpose only in Silverback
// and can be set manually for this case.
// Matches the default Publisher behavior
// that disables Publisher OAuth for non Lagoon environments.
if (getenv('SB_ENVIRONMENT')) {
return t('Skipping for Silverback environment.');
}

// Check requirements.
$entityTypeManager = \Drupal::entityTypeManager();

$publisherUrl = getenv('PUBLISHER_URL');
if (!$publisherUrl) {
throw new \Exception('PUBLISHER_URL environment variable is not set. It is required to setup the Publisher OAuth Consumer.');
}

$clientSecret = getenv('PUBLISHER_OAUTH2_CLIENT_SECRET');
if (!$clientSecret) {
throw new \Exception('PUBLISHER_OAUTH2_CLIENT_SECRET environment variable is not set. It is required to setup the Publisher OAuth Consumer.');
}

$consumersStorage = $entityTypeManager->getStorage('consumer');
$existingConsumers = $consumersStorage->loadMultiple();
$hasPublisherConsumer = FALSE;
/** @var \Drupal\consumers\Entity\ConsumerInterface $consumer */
foreach($existingConsumers as $consumer) {
// As a side effect, delete the default consumer.
// It is installed by the Consumers module.
if ($consumer->getClientId() === 'default_consumer') {
$consumer->delete();
}
if ($consumer->getClientId() === 'publisher') {
$hasPublisherConsumer = TRUE;
}
}

// Create the Publisher Consumer if it does not exist.
if (!$hasPublisherConsumer) {
$oAuthCallback = $publisherUrl . '/oauth/callback';
$consumersStorage->create([
'label' => 'Publisher',
'client_id' => 'publisher',
'is_default' => TRUE,
'secret' => $clientSecret,
'redirect' => $oAuthCallback,
])->save();
return t('Created Publisher OAuth Consumer.');
}

return t('Publisher OAuth Consumer already exists.');
}
4 changes: 4 additions & 0 deletions packages/drupal/custom/custom.info.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@ name: Various customizations
package: Custom
type: module
core_version_requirement: ^9.0 || ^10.0
dependencies:
- silverback_gatsby:silverback_gatsby
- simple_oauth:simple_oauth
- consumers:consumers

0 comments on commit 6e1fc32

Please sign in to comment.