From c0ac4d6e8badb71fec91ae6beba23ece0d30a851 Mon Sep 17 00:00:00 2001 From: Christophe Jossart Date: Mon, 16 Oct 2023 12:55:07 +0200 Subject: [PATCH 1/2] feat(publisher): create publisher consumer if it does not exist yet --- .../silverback_gatsby.info.yml | 1 + .../silverback_gatsby.post_update.php | 63 +++++++++++++++++++ 2 files changed, 64 insertions(+) diff --git a/packages/composer/amazeelabs/silverback_gatsby/silverback_gatsby.info.yml b/packages/composer/amazeelabs/silverback_gatsby/silverback_gatsby.info.yml index c46bcbe82..c73b338f5 100644 --- a/packages/composer/amazeelabs/silverback_gatsby/silverback_gatsby.info.yml +++ b/packages/composer/amazeelabs/silverback_gatsby/silverback_gatsby.info.yml @@ -5,4 +5,5 @@ package: Silverback dependencies: - graphql:graphql (>=8.x-4.0) - graphql_directives:graphql_directives + - consumers:consumers core_version_requirement: ^8 || ^9 || ^10 diff --git a/packages/composer/amazeelabs/silverback_gatsby/silverback_gatsby.post_update.php b/packages/composer/amazeelabs/silverback_gatsby/silverback_gatsby.post_update.php index d09d606eb..c1324270e 100644 --- a/packages/composer/amazeelabs/silverback_gatsby/silverback_gatsby.post_update.php +++ b/packages/composer/amazeelabs/silverback_gatsby/silverback_gatsby.post_update.php @@ -1,6 +1,7 @@ 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(); + } + +} From d8bb597a544fe07e94c65a88f90a6d458d91a1a6 Mon Sep 17 00:00:00 2001 From: Christophe Jossart Date: Mon, 16 Oct 2023 17:29:26 +0200 Subject: [PATCH 2/2] refactor: move post update in a submodule for bc As we throw, make sure that previous projects won't have any issue when missing env vars. --- .../silverback_gatsby_oauth.info.yml | 9 +++ .../silverback_gatsby_oauth.install | 61 ++++++++++++++++++ .../silverback_gatsby.info.yml | 1 - .../silverback_gatsby.post_update.php | 63 ------------------- 4 files changed, 70 insertions(+), 64 deletions(-) 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(); + } +} diff --git a/packages/composer/amazeelabs/silverback_gatsby/silverback_gatsby.info.yml b/packages/composer/amazeelabs/silverback_gatsby/silverback_gatsby.info.yml index c73b338f5..c46bcbe82 100644 --- a/packages/composer/amazeelabs/silverback_gatsby/silverback_gatsby.info.yml +++ b/packages/composer/amazeelabs/silverback_gatsby/silverback_gatsby.info.yml @@ -5,5 +5,4 @@ package: Silverback dependencies: - graphql:graphql (>=8.x-4.0) - graphql_directives:graphql_directives - - consumers:consumers core_version_requirement: ^8 || ^9 || ^10 diff --git a/packages/composer/amazeelabs/silverback_gatsby/silverback_gatsby.post_update.php b/packages/composer/amazeelabs/silverback_gatsby/silverback_gatsby.post_update.php index c1324270e..d09d606eb 100644 --- a/packages/composer/amazeelabs/silverback_gatsby/silverback_gatsby.post_update.php +++ b/packages/composer/amazeelabs/silverback_gatsby/silverback_gatsby.post_update.php @@ -1,7 +1,6 @@ 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(); - } - -}