diff --git a/farm.profile b/farm.profile index dcafb2f15e..0c8e2afcde 100644 --- a/farm.profile +++ b/farm.profile @@ -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'), diff --git a/modules/core/api/farm_api.install b/modules/core/api/farm_api.install index abaf6eb446..24fbfc2a3c 100644 --- a/modules/core/api/farm_api.install +++ b/modules/core/api/farm_api.install @@ -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(); - } } /** diff --git a/modules/core/api/farm_api.post_update.php b/modules/core/api/farm_api.post_update.php index 6bd6c687de..511f4a0c8f 100644 --- a/modules/core/api/farm_api.post_update.php +++ b/modules/core/api/farm_api.post_update.php @@ -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(); + } + +} diff --git a/modules/core/api/modules/farm_api_default_consumer/farm_api_default_consumer.info.yml b/modules/core/api/modules/farm_api_default_consumer/farm_api_default_consumer.info.yml new file mode 100644 index 0000000000..44719c488d --- /dev/null +++ b/modules/core/api/modules/farm_api_default_consumer/farm_api_default_consumer.info.yml @@ -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 diff --git a/modules/core/api/modules/farm_api_default_consumer/farm_api_default_consumer.install b/modules/core/api/modules/farm_api_default_consumer/farm_api_default_consumer.install new file mode 100644 index 0000000000..189bccefc8 --- /dev/null +++ b/modules/core/api/modules/farm_api_default_consumer/farm_api_default_consumer.install @@ -0,0 +1,55 @@ +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(); + } +}