function farm_api_install in farmOS 2.x
Same name and namespace in other branches
- 7 modules/farm/farm_api/farm_api.install \farm_api_install()
Implements hook_install().
File
- modules/
core/ api/ farm_api.install, line 13 - Install, update and uninstall functions for the farm_api module.
Code
function farm_api_install() {
// Allow JSON:API write operations.
\Drupal::configFactory()
->getEditable('jsonapi.settings')
->set('read_only', FALSE)
->save();
// Change the base JSON:API path to /api.
\Drupal::configFactory()
->getEditable('jsonapi_extras.settings')
->set('path_prefix', 'api')
->save();
// Load the simple_oauth module settings.
$simple_oauth_settings = \Drupal::configFactory()
->getEditable('simple_oauth.settings');
// Increase access token expiration time to 1 hour.
$simple_oauth_settings
->set('access_token_expiration', 3600);
// Explicitly set the public/private key path.
$simple_oauth_settings
->set('public_key', '../keys/public.key');
$simple_oauth_settings
->set('private_key', '../keys/private.key');
// Save simple_oauth settings.
$simple_oauth_settings
->save();
// Generate key files for simple_oauth module.
// Code adapted from the contenta_jsonapi profile install tasks:
// https://github.com/contentacms/contenta_jsonapi/blob/8.x-3.x/contenta_jsonapi.profile
// Build all the dependencies manually to avoid having to rely on the
// container to be ready.
$dir_name = 'keys';
/** @var \Drupal\simple_oauth\Service\KeyGeneratorService $key_gen */
$key_gen = \Drupal::service('simple_oauth.key.generator');
/** @var \Drupal\simple_oauth\Service\Filesystem\FileSystemChecker $file_system_checker */
$file_system_checker = \Drupal::service('simple_oauth.filesystem_checker');
/** @var \Drupal\Core\File\FileSystem $file_system */
$file_system = \Drupal::service('file_system');
// Save keys in the "keys" directory outside of the webroot.
$relative_path = DRUPAL_ROOT . '/../' . $dir_name;
if (!$file_system_checker
->isDirectory($relative_path)) {
$file_system
->mkdir($relative_path);
}
$keys_path = $file_system
->realpath($relative_path);
$pub_filename = sprintf('%s/public.key', $keys_path);
$pri_filename = sprintf('%s/private.key', $keys_path);
// Create keys if they don't exist.
if (!$file_system_checker
->fileExist($pub_filename) || !$file_system_checker
->fileExist($pri_filename)) {
try {
$key_gen
->generateKeys($keys_path);
} catch (\Exception $e) {
watchdog_exception('farm_api', $e);
return;
}
}
// Delete the "Default Consumer" created by the consumers module.
$default_consumer = \Drupal::entityTypeManager()
->getStorage('consumer')
->loadByProperties([
'label' => 'Default Consumer',
]);
if (!empty($default_consumer)) {
$default_consumer = reset($default_consumer);
$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',
'redirect' => $base_url,
'is_default' => TRUE,
'owner_id' => '',
'secret' => '',
'confidential' => FALSE,
'third_party' => FALSE,
'grant_user_access' => TRUE,
'limit_user_access' => TRUE,
'limit_requested_access' => FALSE,
]);
$farm_consumer
->save();
}