-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(publisher): create publisher consumer if it does not exist yet (#…
- Loading branch information
1 parent
5b82c4f
commit 3a653b4
Showing
2 changed files
with
70 additions
and
0 deletions.
There are no files selected for viewing
9 changes: 9 additions & 0 deletions
9
...eelabs/silverback_gatsby/modules/silverback_gatsby_oauth/silverback_gatsby_oauth.info.yml
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,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 |
61 changes: 61 additions & 0 deletions
61
...zeelabs/silverback_gatsby/modules/silverback_gatsby_oauth/silverback_gatsby_oauth.install
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,61 @@ | ||
<?php | ||
|
||
use Drupal\user\RoleInterface; | ||
|
||
function silverback_gatsby_oauth_install() { | ||
// 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; | ||
} | ||
|
||
// Check requirements. | ||
$entityTypeManager = \Drupal::entityTypeManager(); | ||
$publisherRole = $entityTypeManager->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(); | ||
} | ||
} |