diff --git a/modules/hdl/config/install/hdl.settings.yml b/modules/hdl/config/install/hdl.settings.yml new file mode 100644 index 0000000..a091837 --- /dev/null +++ b/modules/hdl/config/install/hdl.settings.yml @@ -0,0 +1,6 @@ +hdl_prefix: '2286.9' +hdl_qualifier: 'R.2.N' +hdl_admin_handle: '2286/ASU_ADMIN' +hdl_admin_index: 300 +hdl_handle_api_endpoint: 'https://handle-qa.lib.asu.edu:8000/api/handles/' +hdl_handle_permissions: '111111111111' diff --git a/modules/hdl/hdl.info.yml b/modules/hdl/hdl.info.yml new file mode 100644 index 0000000..b6932c1 --- /dev/null +++ b/modules/hdl/hdl.info.yml @@ -0,0 +1,6 @@ +name: 'Handle (HDL) Persistent ID Minter' +description: "A minter accompanying the Persistent Identifers module for minting handles." +type: module +core: 8.x +dependencies: + - persistent_identifiers diff --git a/modules/hdl/hdl.module b/modules/hdl/hdl.module new file mode 100644 index 0000000..e0f6ecd --- /dev/null +++ b/modules/hdl/hdl.module @@ -0,0 +1,150 @@ + 'textfield', + '#access' => TRUE, + '#title' => 'Handle identifier prefix', + '#default_value' => $config->get('hdl_prefix'), + '#description' => t("This string will be prefix used for the handle. Ie. https://handle.net/2286.9 where 2286.9 is the prefix."), + '#states' => [ + 'visible' => [ + ':input[id="persistent_identifiers_minter"]' => ['value' => 'hdl.minter.hdl'], + ], + ], + ]; + + $form['hdl_qualifier'] = [ + '#type' => 'textfield', + '#access' => TRUE, + '#title' => 'Handle identifier qualifier', + '#default_value' => $config->get('hdl_qualifier'), + '#description' => t("This string will be prepended to identifier to make the unique handle. You might use it to distinguish different types of objects, like R.C. for Repository Collection and R.O. for Repository Object and R.M. for Repository Media. So your handle might look like https://handle.net/2286.9/R.O.7"), + '#states' => [ + 'visible' => [ + ':input[id="persistent_identifiers_minter"]' => ['value' => 'hdl.minter.hdl'], + ], + ], + ]; + + $form['hdl_admin_handle'] = [ + '#type' => 'textfield', + '#access' => TRUE, + '#title' => 'Admin Handle', + '#default_value' => $config->get('hdl_admin_handle'), + '#description' => t("The Admin Handle used for permissions in the handle JSON"), + '#states' => [ + 'visible' => [ + ':input[id="persistent_identifiers_minter"]' => ['value' => 'hdl.minter.hdl'], + ], + ], + ]; + + $form['hdl_admin_index'] = [ + '#type' => 'textfield', + '#access' => TRUE, + '#title' => 'Admin Index', + '#default_value' => $config->get('hdl_admin_index'), + '#description' => t("The Admin index used for permissions in the handle JSON"), + '#states' => [ + 'visible' => [ + ':input[id="persistent_identifiers_minter"]' => ['value' => 'hdl.minter.hdl'], + ], + ], + ]; + + $form['hdl_handle_permissions'] = [ + '#type' => 'textfield', + '#access' => TRUE, + '#title' => 'Handle Permissions', + '#default_value' => $config->get('hdl_handle_permissions'), + '#description' => t("The permissions to have to the admin user on the handle"), + '#states' => [ + 'visible' => [ + ':input[id="persistent_identifiers_minter"]' => ['value' => 'hdl.minter.hdl'], + ], + ], + ]; + + $form['hdl_handle_api_endpoint'] = [ + '#type' => 'textfield', + '#access' => TRUE, + '#title' => 'Handle API Endpoint', + '#default_value' => $config->get('hdl_handle_api_endpoint'), + '#description' => t("The API endpoint for your handle server"), + '#states' => [ + 'visible' => [ + ':input[id="persistent_identifiers_minter"]' => ['value' => 'hdl.minter.hdl'], + ], + ], + ]; + + $form['hdl_handle_basic_auth_password'] = [ + '#type' => 'password', + // '#access' => TRUE, + '#title' => 'Handle Basic Auth Password', + '#default_value' => $config->get('hdl_handle_basic_auth_password'), + '#description' => t("The password to be used with the admin handle and index to authenticate for the API"), + '#states' => [ + 'visible' => [ + ':input[id="persistent_identifiers_minter"]' => ['value' => 'hdl.minter.hdl'], + ], + ], + ]; + + $form['#submit'][] = 'hdl_submit'; + } +} + +/** + * Submit callback. + * + * Saves the value of the minter-specific field defined in the implementation + * of hook_form_alter() above. + * + * @param array $form + * The form. + * @param \Drupal\Core\Form\FormStateInterface $form_state + * The form state. + */ +function hdl_submit(array &$form, FormStateInterface $form_state) { + $existing_config = \Drupal::config('hdl.settings'); + $admin_handle = $form_state->getValue('hdl_admin_handle', $existing_config->get('hdl_admin_handle')); + $prefix = $form_state->getValue('hdl_prefix', $existing_config->get('hdl_prefix')); + $qualifier = $form_state->getValue('hdl_qualifier', $existing_config->get('hdl_qualifier')); + $api_endpoint = $form_state->getValue('hdl_handle_api_endpoint', $existing_config->get('hdl_handle_api_endpoint')); + $index = $form_state->getValue('hdl_admin_index', $existing_config->get('hdl_admin_index')); + $permissions = $form_state->getValue('hdl_handle_permissions', $existing_config->get('hdl_handle_permissions')); + $password = $form_state->getValue('hdl_handle_basic_auth_password', $existing_config->get('hdl_handle_basic_auth_password')); + $config_factory = \Drupal::configFactory(); + $config_factory->getEditable('hdl.settings') + ->set('hdl_admin_handle', trim($admin_handle)) + ->set('hdl_prefix', trim($prefix)) + ->set('hdl_qualifier', trim($qualifier)) + ->set('hdl_handle_api_endpoint', trim($api_endpoint)) + ->set('hdl_admin_index', trim($index)) + ->set('hdl_handle_permissions', trim($permissions)) + ->set('hdl_handle_basic_auth_password', trim($password)) + ->save(); +} diff --git a/modules/hdl/hdl.services.yml b/modules/hdl/hdl.services.yml new file mode 100644 index 0000000..31d827c --- /dev/null +++ b/modules/hdl/hdl.services.yml @@ -0,0 +1,5 @@ +services: + # The list of minters at /admin/config/persistent_identifiers/settings is derived + # from minters that follow this naming pattern. + hdl.minter.hdl: + class: Drupal\hdl\Minter\Hdl diff --git a/modules/hdl/src/Minter/Hdl.php b/modules/hdl/src/Minter/Hdl.php new file mode 100644 index 0000000..012fbb6 --- /dev/null +++ b/modules/hdl/src/Minter/Hdl.php @@ -0,0 +1,97 @@ +get('hdl_prefix'); + $handle_type_qualifier = $config->get('hdl_qualifier'); + $handle = $handle_prefix . '/' . $handle_type_qualifier . '.' . $entity->id(); + $host = \Drupal::request()->getSchemeAndHttpHost(); + $url = $host . $entity->toUrl()->toString(); + $admin_handle = $config->get('hdl_admin_handle'); + $handle_admin_index = $config->get('hdl_admin_index'); + $endpoint_url = $config->get('hdl_handle_api_endpoint'); + $permissions = $config->get('hdl_handle_permissions'); + $password = $config->get('hdl_handle_basic_auth_password'); + $handle_json = [ + [ + 'index' => 1, + 'type' => "URL", + 'data' => [ + 'format' => "string", + 'value' => $url, + ], + ], + [ + 'index' => 100, + 'type' => 'HS_ADMIN', + 'data' => [ + 'format' => 'admin', + 'value' => [ + 'handle' => $admin_handle, + 'index' => $handle_admin_index, + 'permissions' => $permissions, + ], + ], + ], + ]; + + $client = \Drupal::httpClient(); + try { + $request = $client->request('PUT', $endpoint_url . $handle . "?overwrite=true", ['json' => $handle_json, 'auth' => [$handle_admin_index . '%3A' . $admin_handle, $password], 'headers' => ['Content-Type' => 'application/json', 'Accept' => 'application/json']]); + \Drupal::logger('persistent identifiers')->info(print_r($request, TRUE)); + } catch (ClientException $e) { + \Drupal::logger('persistent identifiers')->error(print_r($e, TRUE)); + return FALSE; + } + $full_handle = "https://handle.net/" . $handle; + return $full_handle; + } + +}