diff --git a/docker-compose.yml b/docker-compose.yml index d0e1cc00e..55c5733ea 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -31,9 +31,6 @@ x-environment: LAGOON_ENVIRONMENT: 'local' LAGOON_ENVIRONMENT_TYPE: ${LAGOON_ENVIRONMENT_TYPE:-local} WEBROOT: web - GRAPHQL_USER_NAME: graphql_consumer - GRAPHQL_USER_PASSWORD: test - BNF_SERVER_BASE_ENDPOINT: "https://dpl-cms.local" # Uncomment if you like to have the system behave like in production #LAGOON_ENVIRONMENT_TYPE: production diff --git a/web/modules/custom/bnf/bnf.services.yml b/web/modules/custom/bnf/bnf.services.yml index 3fa9c3c8c..8d3e330b4 100644 --- a/web/modules/custom/bnf/bnf.services.yml +++ b/web/modules/custom/bnf/bnf.services.yml @@ -5,6 +5,7 @@ services: autowire: true arguments: $logger: '@logger.channel.bnf' + Drupal\bnf\Services\BnfImporter: '@bnf.importer' logger.channel.bnf: parent: logger.channel_base diff --git a/web/modules/custom/bnf/bnf_client/bnf_client.links.menu.yml b/web/modules/custom/bnf/bnf_client/bnf_client.links.menu.yml index 217daedc1..051a46998 100644 --- a/web/modules/custom/bnf/bnf_client/bnf_client.links.menu.yml +++ b/web/modules/custom/bnf/bnf_client/bnf_client.links.menu.yml @@ -4,8 +4,15 @@ bnf_client.import_content: parent: system.admin_content route_name: bnf_client.import_form weight: 10 + bnf_client.login: title: 'Login to BNF' parent: system.admin_content route_name: bnf_client.server_redirect weight: 11 + +bnf_client.settings_form: + title: 'BNF configuration' + route_name: bnf_client.settings + description: 'Configure BNF client.' + parent: system.admin_config_services diff --git a/web/modules/custom/bnf/bnf_client/bnf_client.routing.yml b/web/modules/custom/bnf/bnf_client/bnf_client.routing.yml index 8c27c24fb..14c62f08d 100644 --- a/web/modules/custom/bnf/bnf_client/bnf_client.routing.yml +++ b/web/modules/custom/bnf/bnf_client/bnf_client.routing.yml @@ -22,3 +22,13 @@ bnf_client.server_redirect: _title: 'Login to BNF' requirements: _permission: 'bnf client import nodes' + +bnf_client.settings: + path: '/admin/config/services/bnf' + defaults: + _form: '\Drupal\bnf_client\Form\SettingsForm' + _title: 'BNF configuration' + requirements: + _permission: 'administer site configuration' + options: + _admin_route: TRUE diff --git a/web/modules/custom/bnf/bnf_client/config/install/bnf_client.settings.yml b/web/modules/custom/bnf/bnf_client/config/install/bnf_client.settings.yml new file mode 100644 index 000000000..22af9bcbd --- /dev/null +++ b/web/modules/custom/bnf/bnf_client/config/install/bnf_client.settings.yml @@ -0,0 +1 @@ +base_url: 'http://bibliotekernesnationaleformidling.dk/' diff --git a/web/modules/custom/bnf/bnf_client/config/schema/bnf_client.schema.yml b/web/modules/custom/bnf/bnf_client/config/schema/bnf_client.schema.yml new file mode 100644 index 000000000..2fbcbdc50 --- /dev/null +++ b/web/modules/custom/bnf/bnf_client/config/schema/bnf_client.schema.yml @@ -0,0 +1,9 @@ +# Schema for the configuration files of the BNF Client module. + +bnf_client.settings: + type: config_object + label: 'BNF client configuration' + mapping: + base_url: + type: uri + label: 'BNF server URL' diff --git a/web/modules/custom/bnf/bnf_client/src/Controller/BnfRedirecter.php b/web/modules/custom/bnf/bnf_client/src/Controller/BnfRedirecter.php index 67c29c954..60ee806f9 100644 --- a/web/modules/custom/bnf/bnf_client/src/Controller/BnfRedirecter.php +++ b/web/modules/custom/bnf/bnf_client/src/Controller/BnfRedirecter.php @@ -2,8 +2,8 @@ namespace Drupal\bnf_client\Controller; +use Drupal\bnf_client\Form\SettingsForm; use Drupal\Core\Controller\ControllerBase; - use Drupal\Core\Routing\TrustedRedirectResponse; use Drupal\Core\Url; use Symfony\Component\HttpFoundation\Request; @@ -17,8 +17,8 @@ class BnfRedirecter extends ControllerBase { * Logging in the editor on BNF, allowing them to browse available content. */ public function login(Request $request): TrustedRedirectResponse { - $bnfServer = (string) getenv('BNF_SERVER_BASE_ENDPOINT'); - $loginUrl = "$bnfServer/bnf/login"; + $bnfServer = $this->config(SettingsForm::CONFIG_NAME)->get('base_url'); + $loginUrl = "{$bnfServer}bnf/login"; $url = Url::fromUri($loginUrl, [ 'query' => [ diff --git a/web/modules/custom/bnf/bnf_client/src/Form/BnfImportConfirmForm.php b/web/modules/custom/bnf/bnf_client/src/Form/BnfImportConfirmForm.php index 8c8af244c..b2b240401 100644 --- a/web/modules/custom/bnf/bnf_client/src/Form/BnfImportConfirmForm.php +++ b/web/modules/custom/bnf/bnf_client/src/Form/BnfImportConfirmForm.php @@ -4,6 +4,7 @@ use Drupal\bnf\Exception\AlreadyExistsException; use Drupal\bnf\Services\BnfImporter; +use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\DependencyInjection\AutowireTrait; use Drupal\Core\DependencyInjection\ContainerInjectionInterface; use Drupal\Core\Form\FormInterface; @@ -12,15 +13,20 @@ use Drupal\Core\Routing\RouteMatchInterface; use Drupal\Core\StringTranslation\StringTranslationTrait; use Psr\Log\LoggerInterface; -use Symfony\Component\DependencyInjection\ContainerInterface; +use Symfony\Component\DependencyInjection\Attribute\Autowire; /** * Displaying an import preview, and allowing editor to import. */ class BnfImportConfirmForm implements FormInterface, ContainerInjectionInterface { - use StringTranslationTrait; use AutowireTrait; + use StringTranslationTrait; + + /** + * The BNF site base URL. + */ + protected string $baseUrl; /** * {@inheritDoc} @@ -29,19 +35,11 @@ public function __construct( protected RouteMatchInterface $routeMatch, protected MessengerInterface $messenger, protected BnfImporter $bnfImporter, + #[Autowire(service: 'logger.channel.bnf')] protected LoggerInterface $logger, - ) {} - - /** - * {@inheritDoc} - */ - public static function create(ContainerInterface $container): static { - return new static( - $container->get('current_route_match'), - $container->get('messenger'), - $container->get('bnf.importer'), - $container->get('logger.channel.bnf'), - ); + ConfigFactoryInterface $configFactory, + ) { + $this->baseUrl = $configFactory->get(SettingsForm::CONFIG_NAME)->get('base_url'); } /** @@ -58,7 +56,7 @@ public function buildForm(array $form, FormStateInterface $form_state): array { $form['#title'] = $this->t('Confirm import of BNF content', [], ['context' => 'BNF']); $uuid = $this->routeMatch->getParameter('uuid'); - $bnfServer = (string) getenv('BNF_SERVER_BASE_ENDPOINT') . '/graphql'; + $bnfServer = $this->baseUrl . 'graphql'; $form_state->set('uuid', $uuid); $form_state->set('bnfServer', $bnfServer); diff --git a/web/modules/custom/bnf/bnf_client/src/Form/SettingsForm.php b/web/modules/custom/bnf/bnf_client/src/Form/SettingsForm.php new file mode 100644 index 000000000..745646122 --- /dev/null +++ b/web/modules/custom/bnf/bnf_client/src/Form/SettingsForm.php @@ -0,0 +1,90 @@ + 'fieldset', + '#title' => $this->t('BNF configuration'), + '#tree' => FALSE, + ]; + + $form['bnf_client']['base_url'] = [ + '#type' => 'textfield', + '#size' => 100, + '#title' => $this->t('BNF server URL'), + '#description' => $this->t('For example https://bibliotekernesnationaleformidling.dk/.'), + '#default_value' => $this->config(self::CONFIG_NAME)->get('base_url'), + '#required' => TRUE, + ]; + + return parent::buildForm($form, $form_state); + } + + /** + * {@inheritdoc} + */ + public function validateForm(array &$form, FormStateInterface $form_state): void { + $url = $form_state->getValue('base_url'); + $element = $form['bnf_client']['base_url']; + + // This doesn't support IDN names like `jøsses.dk`, but we'll live with that + // for the moment being. + if (!filter_var($url, FILTER_VALIDATE_URL)) { + $form_state->setError($element, $this->t('Please enter a valid URL.')); + } + + if (!preg_match('/^https:/', $url)) { + $form_state->setError($element, $this->t('Only HTTPS is supported.')); + } + + if (mb_substr($url[-1], -1) !== '/') { + $form_state->setError($element, $this->t('URL must end with a /.')); + } + } + + /** + * {@inheritdoc} + */ + public function submitForm(array &$form, FormStateInterface $form_state): void { + parent::submitForm($form, $form_state); + + $this->config(self::CONFIG_NAME) + ->set('base_url', $form_state->getValue('base_url')) + ->save(); + } + +} diff --git a/web/modules/custom/bnf/bnf_client/src/Services/BnfExporter.php b/web/modules/custom/bnf/bnf_client/src/Services/BnfExporter.php index c2c426ef0..a033d0423 100644 --- a/web/modules/custom/bnf/bnf_client/src/Services/BnfExporter.php +++ b/web/modules/custom/bnf/bnf_client/src/Services/BnfExporter.php @@ -4,6 +4,8 @@ use Drupal\bnf\BnfStateEnum; use Drupal\bnf\Exception\AlreadyExistsException; +use Drupal\bnf_client\Form\SettingsForm; +use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Routing\UrlGeneratorInterface; use Drupal\Core\StringTranslation\TranslationInterface; use Drupal\node\NodeInterface; @@ -20,6 +22,11 @@ */ class BnfExporter { + /** + * The BNF site base URL. + */ + protected string $baseUrl; + /** * Constructor. */ @@ -28,7 +35,10 @@ public function __construct( protected UrlGeneratorInterface $urlGenerator, protected TranslationInterface $translation, protected LoggerInterface $logger, - ) {} + ConfigFactoryInterface $configFactory, + ) { + $this->baseUrl = $configFactory->get(SettingsForm::CONFIG_NAME)->get('base_url'); + } /** * Requesting BNF server to import the supplied node. @@ -55,7 +65,7 @@ public function exportNode(NodeInterface $node): void { GRAPHQL; try { - $bnfServer = (string) getenv('BNF_SERVER_BASE_ENDPOINT') . '/graphql'; + $bnfServer = $this->baseUrl . 'graphql'; if (!filter_var($bnfServer, FILTER_VALIDATE_URL)) { throw new \InvalidArgumentException('The provided BNF server URL is not valid.'); @@ -72,7 +82,7 @@ public function exportNode(NodeInterface $node): void { 'headers' => [ 'Content-Type' => 'application/json', ], - 'auth' => [getenv('GRAPHQL_USER_NAME'), getenv('GRAPHQL_USER_PASSWORD')], + 'auth' => ['bnf_graphql', getenv('BNF_GRAPHQL_CONSUMER_USER_PASSWORD')], 'json' => [ 'query' => $mutation, ], diff --git a/web/modules/custom/bnf/src/Services/BnfImporter.php b/web/modules/custom/bnf/src/Services/BnfImporter.php index 53325dc0f..bb12701e8 100644 --- a/web/modules/custom/bnf/src/Services/BnfImporter.php +++ b/web/modules/custom/bnf/src/Services/BnfImporter.php @@ -272,7 +272,7 @@ public function loadNodeData(string $uuid, string $endpointUrl, string $nodeType 'headers' => [ 'Content-Type' => 'application/json', ], - 'auth' => [getenv('GRAPHQL_USER_NAME'), getenv('GRAPHQL_USER_PASSWORD')], + 'auth' => ['bnf_graphql', getenv('BNF_GRAPHQL_CONSUMER_USER_PASSWORD')], 'json' => [ 'query' => $query, ],