3
0
Fork 0
mirror of https://github.com/farmOS/farmOS.git synced 2024-02-23 11:37:38 +01:00

Issue #3256745 by paul121: Move default farm OAuth2 client to a separate module

This commit is contained in:
Paul Weidner 2023-10-30 14:51:58 -07:00 committed by Michael Stenta
parent 4226394e74
commit c64247b966
5 changed files with 89 additions and 32 deletions

View file

@ -58,6 +58,7 @@ function farm_modules() {
'farm_import_csv' => t('CSV importer'),
'farm_kml' => t('KML export features'),
'farm_import_kml' => t('KML asset importer'),
'farm_api_default_consumer' => t('Default API Consumer'),
'farm_fieldkit' => t('Field Kit integration'),
'farm_l10n' => t('Translation/localization features'),
'farm_role_account_admin' => t('Account Admin role'),

View file

@ -5,7 +5,6 @@
* Install, update and uninstall functions for the farm_api module.
*/
use Drupal\consumers\Entity\Consumer;
use Drupal\Core\Utility\Error;
/**
@ -73,37 +72,6 @@ function farm_api_install() {
$default_consumer->delete();
}
// Create a "Farm default" consumer.
$base_url = \Drupal::service('router.request_context')->getCompleteBaseUrl();
$farm_consumer = Consumer::create([
'label' => 'Farm default',
'client_id' => 'farm',
'access_token_expiration' => 3600,
'redirect' => $base_url,
'is_default' => TRUE,
'owner_id' => '',
'secret' => NULL,
'confidential' => FALSE,
'third_party' => FALSE,
]);
$farm_consumer->save();
}
/**
* Implements hook_uninstall().
*/
function farm_api_uninstall() {
// Load the default farm consumer.
$consumers = \Drupal::entityTypeManager()->getStorage('consumer')
->loadByProperties(['client_id' => 'farm']);
// If found, delete the consumer.
if (!empty($consumers)) {
$farm_consumer = reset($consumers);
$farm_consumer->delete();
}
}
/**

View file

@ -38,3 +38,28 @@ function farm_api_post_update_enable_static_oauth2_scopes(&$sandbox = NULL) {
$simple_oauth_settings->set('scope_provider', 'static');
$simple_oauth_settings->save();
}
/**
* Enable default consumer module.
*/
function farm_api_post_update_enable_default_consumer_module(&$sandbox = NULL) {
// Check for an existing farm default consumer.
$consumers = \Drupal::entityTypeManager()->getStorage('consumer')
->loadByProperties(['client_id' => 'farm']);
if (!empty($consumers)) {
// Enable default consumer module.
if (!\Drupal::service('module_handler')->moduleExists('farm_api_default_consumer')) {
\Drupal::service('module_installer')->install(['farm_api_default_consumer']);
}
// Update values on the consumer.
/** @var \Drupal\consumers\Entity\ConsumerInterface $farm_default */
$farm_default = reset($consumers);
$farm_default->set('user_id', NULL);
$farm_default->set('grant_types', ['password']);
$farm_default->save();
}
}

View file

@ -0,0 +1,8 @@
name: farmOS Default API Consumer
description: Provides a default consumer for using the farmOS API.
type: module
package: farmOS
core_version_requirement: ^10
dependencies:
- farm:farm_api
- simple_oauth_password_grant:simple_oauth_password_grant

View file

@ -0,0 +1,55 @@
<?php
/**
* @file
* Install and uninstall functions for the farm_api_default_consumer module.
*/
use Drupal\consumers\Entity\Consumer;
/**
* Implements hook_install().
*/
function farm_api_default_consumer_install() {
// Check for an existing farm default consumer.
$consumers = \Drupal::entityTypeManager()->getStorage('consumer')
->loadByProperties(['client_id' => 'farm']);
// If not found, create the farm default consumer.
if (empty($consumers)) {
$base_url = \Drupal::service('router.request_context')->getCompleteBaseUrl();
$farm_consumer = Consumer::create([
'label' => 'Farm default',
'client_id' => 'farm',
'access_token_expiration' => 3600,
'grant_types' => [
'password',
],
'redirect' => $base_url,
'is_default' => TRUE,
'owner_id' => NULL,
'secret' => NULL,
'confidential' => FALSE,
'third_party' => FALSE,
]);
$farm_consumer->save();
}
}
/**
* Implements hook_uninstall().
*/
function farm_api_default_consumer_uninstall() {
// Load the default farm consumer.
$consumers = \Drupal::entityTypeManager()->getStorage('consumer')
->loadByProperties(['client_id' => 'farm']);
// If found, delete the consumer.
if (!empty($consumers)) {
$farm_consumer = reset($consumers);
$farm_consumer->delete();
}
}