From 3a653b4edd7749a4ce3de6aafbf5036436f6a003 Mon Sep 17 00:00:00 2001 From: Christophe Jossart Date: Mon, 16 Oct 2023 18:54:39 +0200 Subject: [PATCH] feat(publisher): create publisher consumer if it does not exist yet (#1439) --- .../silverback_gatsby_oauth.info.yml | 9 +++ .../silverback_gatsby_oauth.install | 61 +++++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 packages/composer/amazeelabs/silverback_gatsby/modules/silverback_gatsby_oauth/silverback_gatsby_oauth.info.yml create mode 100644 packages/composer/amazeelabs/silverback_gatsby/modules/silverback_gatsby_oauth/silverback_gatsby_oauth.install diff --git a/packages/composer/amazeelabs/silverback_gatsby/modules/silverback_gatsby_oauth/silverback_gatsby_oauth.info.yml b/packages/composer/amazeelabs/silverback_gatsby/modules/silverback_gatsby_oauth/silverback_gatsby_oauth.info.yml new file mode 100644 index 000000000..b2eb15283 --- /dev/null +++ b/packages/composer/amazeelabs/silverback_gatsby/modules/silverback_gatsby_oauth/silverback_gatsby_oauth.info.yml @@ -0,0 +1,9 @@ +name: Silverback Gatsby OAuth +type: module +description: 'Integration of Publisher with OAuth.' +package: Silverback +dependencies: + - silverback_gatsby:silverback_gatsby + - simple_oauth:simple_oauth + - consumers:consumers +core_version_requirement: ^8 || ^9 || ^10 diff --git a/packages/composer/amazeelabs/silverback_gatsby/modules/silverback_gatsby_oauth/silverback_gatsby_oauth.install b/packages/composer/amazeelabs/silverback_gatsby/modules/silverback_gatsby_oauth/silverback_gatsby_oauth.install new file mode 100644 index 000000000..98a620a92 --- /dev/null +++ b/packages/composer/amazeelabs/silverback_gatsby/modules/silverback_gatsby_oauth/silverback_gatsby_oauth.install @@ -0,0 +1,61 @@ +getStorage('user_role')->load('publisher'); + if (!$publisherRole instanceof RoleInterface) { + throw new \Exception('Publisher Role does not exist. It is required to setup the Publisher OAuth Consumer.'); + } + + $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, + 'roles' => [ + 'publisher', + ], + ])->save(); + } +}