Skip to content

Commit

Permalink
Issue #3256745 by paul121: Move default farm OAuth2 client to a separ…
Browse files Browse the repository at this point in the history
…ate module
  • Loading branch information
paul121 authored and mstenta committed Nov 1, 2023
1 parent 4226394 commit c64247b
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 32 deletions.
1 change: 1 addition & 0 deletions farm.profile
Expand Up @@ -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'),
Expand Down
32 changes: 0 additions & 32 deletions modules/core/api/farm_api.install
Expand Up @@ -5,7 +5,6 @@
* Install, update and uninstall functions for the farm_api module.
*/

use Drupal\consumers\Entity\Consumer;
use Drupal\Core\Utility\Error;

/**
Expand Down Expand Up @@ -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();
}
}

/**
Expand Down
25 changes: 25 additions & 0 deletions modules/core/api/farm_api.post_update.php
Expand Up @@ -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();
}

}
@@ -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
@@ -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();
}
}

0 comments on commit c64247b

Please sign in to comment.