From cee1d9ce8d63b1d3b59aaae47ebea98941a8ed35 Mon Sep 17 00:00:00 2001 From: Benjamin Rasmussen Date: Thu, 2 Jan 2025 11:17:32 +0100 Subject: [PATCH] Adding a BNF login form for client sites. DDFHER-165 A login form, that we use to POST to BNF server, telling them who we are. It is necessary for it to be a form, as the server will need to set some kind of session/cookie on the actual end-users browser. --- .../bnf/bnf_client/bnf_client.links.menu.yml | 5 ++ .../bnf/bnf_client/bnf_client.routing.yml | 8 +++ .../bnf/bnf_client/src/Form/BnfLoginForm.php | 71 +++++++++++++++++++ .../bnf/bnf_server/bnf_server.routing.yml | 10 +++ .../src/Controller/LoginController.php | 41 +++++++++++ 5 files changed, 135 insertions(+) create mode 100644 web/modules/custom/bnf/bnf_client/src/Form/BnfLoginForm.php create mode 100644 web/modules/custom/bnf/bnf_server/bnf_server.routing.yml create mode 100644 web/modules/custom/bnf/bnf_server/src/Controller/LoginController.php 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 5bce3976d..5f2b24ff6 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,3 +4,8 @@ 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.login_form + weight: 11 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 6d3987e66..a2478865a 100644 --- a/web/modules/custom/bnf/bnf_client/bnf_client.routing.yml +++ b/web/modules/custom/bnf/bnf_client/bnf_client.routing.yml @@ -14,3 +14,11 @@ bnf_client.import_confirm_form: _title: 'Confirm import' requirements: _permission: 'bnf client import nodes' + +bnf_client.login_form: + path: '/admin/bnf/login' + defaults: + _form: '\Drupal\bnf_client\Form\BnfLoginForm' + _title: 'Login to BNF' + requirements: + _permission: 'bnf client import nodes' diff --git a/web/modules/custom/bnf/bnf_client/src/Form/BnfLoginForm.php b/web/modules/custom/bnf/bnf_client/src/Form/BnfLoginForm.php new file mode 100644 index 000000000..11a4834a0 --- /dev/null +++ b/web/modules/custom/bnf/bnf_client/src/Form/BnfLoginForm.php @@ -0,0 +1,71 @@ +setMethod('POST'); + + // It would be nice if we could pull this route programmatically, but + // it is defined in bnf_server, which is inaccessible due to being + // disabled on client sites. + $form['#action'] = "$bnfServer/bnf/login"; + + $form['callbackUrl'] = [ + '#type' => 'hidden', + '#value' => $this->getRequest()->getSchemeAndHttpHost(), + ]; + + $form['siteName'] = [ + '#type' => 'hidden', + '#value' => $this->config('system.site')->get('name'), + ]; + + $form['info'] = [ + '#type' => 'container', + '#markup' => $this->t( + 'Login to BNF, to browse available content that you can import to your own site.
+ You have a chance to preview the content before it is imported.', + [], ['context' => 'BNF'] + ), + ]; + + $form['actions']['submit'] = [ + '#type' => 'submit', + '#value' => $this->t('Login to BNF', [], ['context' => 'BNF']), + ]; + + return $form; + } + + /** + * {@inheritDoc} + */ + public function submitForm(array &$form, FormStateInterface $form_state): void { + // Left empty on purpose - the class demands it. + } + +} diff --git a/web/modules/custom/bnf/bnf_server/bnf_server.routing.yml b/web/modules/custom/bnf/bnf_server/bnf_server.routing.yml new file mode 100644 index 000000000..5686bd931 --- /dev/null +++ b/web/modules/custom/bnf/bnf_server/bnf_server.routing.yml @@ -0,0 +1,10 @@ +--- +bnf_server.cookie_login: + path: '/bnf/login' + defaults: + _controller: '\Drupal\bnf_server\Controller\LoginController::login' + methods: [POST] + requirements: + _permission: 'access content' + options: + no_cache: 'TRUE' diff --git a/web/modules/custom/bnf/bnf_server/src/Controller/LoginController.php b/web/modules/custom/bnf/bnf_server/src/Controller/LoginController.php new file mode 100644 index 000000000..750a8d244 --- /dev/null +++ b/web/modules/custom/bnf/bnf_server/src/Controller/LoginController.php @@ -0,0 +1,41 @@ +get('callbackUrl'); + $name = $request->get('siteName'); + + if (empty($url)) { + throw new BadRequestHttpException('Callback URL cannot be empty.'); + } + + $response = new RedirectResponse('/'); + + $response->headers->setCookie(new Cookie(self::COOKIE_CALLBACK_URL, $url)); + $response->headers->setCookie(new Cookie(self::COOKIE_SITE_NAME, $name)); + + return $response; + } + +}