Set default scopes for fieldkit consumer

This commit is contained in:
Paul Weidner 2023-10-30 15:51:13 -07:00 committed by Michael Stenta
parent 33f2805d3e
commit 0673bebbda
2 changed files with 21 additions and 0 deletions

View File

@ -6,12 +6,21 @@
*/
use Drupal\consumers\Entity\Consumer;
use Drupal\simple_oauth\Oauth2ScopeInterface;
/**
* Implements hook_install().
*/
function farm_fieldkit_install() {
// Check for default role scopes.
/** @var \Drupal\simple_oauth\Oauth2ScopeProviderInterface $scope_provider */
$scope_provider = \Drupal::service('simple_oauth.oauth2_scope.provider');
$scopes = $scope_provider->loadMultiple(['farm_manager', 'farm_worker']);
$scope_ids = array_map(function (Oauth2ScopeInterface $scope) {
return $scope->id();
}, $scopes);
// Create a consumer for the farmOS Field Kit client.
$fk_consumer = Consumer::create([
'label' => 'Field Kit',
@ -21,6 +30,7 @@ function farm_fieldkit_install() {
'refresh_token',
'password',
],
'scopes' => array_values($scope_ids),
'redirect' => 'https://farmOS.app',
'allowed_origins' => 'https://farmos.app',
'owner_id' => NULL,

View File

@ -5,6 +5,8 @@
* Post update functions for farm_fieldkit module.
*/
use Drupal\simple_oauth\Oauth2ScopeInterface;
/**
* Enable simple oauth password grant.
*/
@ -15,6 +17,14 @@ function farm_fieldkit_post_update_enable_password_grant(&$sandbox = NULL) {
\Drupal::service('module_installer')->install(['simple_oauth_password_grant']);
}
// Check for default role scopes.
/** @var \Drupal\simple_oauth\Oauth2ScopeProviderInterface $scope_provider */
$scope_provider = \Drupal::service('simple_oauth.oauth2_scope.provider');
$scopes = $scope_provider->loadMultiple(['farm_manager', 'farm_worker']);
$scope_ids = array_map(function (Oauth2ScopeInterface $scope) {
return $scope->id();
}, $scopes);
// Update existing fieldkit consumer.
$consumers = \Drupal::entityTypeManager()->getStorage('consumer')
->loadByProperties(['client_id' => 'fieldkit']);
@ -23,6 +33,7 @@ function farm_fieldkit_post_update_enable_password_grant(&$sandbox = NULL) {
$fieldkit = reset($consumers);
$fieldkit->set('user_id', NULL);
$fieldkit->set('grant_types', ['refresh_token', 'password']);
$fieldkit->set('scopes', array_values($scope_ids));
$fieldkit->save();
}
}