Skip to content

Commit

Permalink
Adding a BNF login form for client sites. DDFHER-165
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
rasben committed Jan 2, 2025
1 parent 850df4b commit cee1d9c
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 0 deletions.
5 changes: 5 additions & 0 deletions web/modules/custom/bnf/bnf_client/bnf_client.links.menu.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 8 additions & 0 deletions web/modules/custom/bnf/bnf_client/bnf_client.routing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
71 changes: 71 additions & 0 deletions web/modules/custom/bnf/bnf_client/src/Form/BnfLoginForm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

namespace Drupal\bnf_client\Form;

use Drupal\Core\Form\FormBase;

use Drupal\Core\Form\FormStateInterface;

/**
* 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.
*/
class BnfLoginForm extends FormBase {

/**
* {@inheritDoc}
*/
public function getFormId() {
return 'bnf_login_form';
}

/**
* {@inheritDoc}
*/
public function buildForm(array $form, FormStateInterface $form_state): array {
$bnfServer = (string) getenv('BNF_SERVER_BASE_ENDPOINT');

$form_state->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.<br/>
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.
}

}
10 changes: 10 additions & 0 deletions web/modules/custom/bnf/bnf_server/bnf_server.routing.yml
Original file line number Diff line number Diff line change
@@ -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'
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Drupal\bnf_server\Controller;

use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\HttpFoundation\Cookie;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;

/**
* Recieving "login" information, used to display "import to my site" button.
*
* This endpoint is reached from library websites, when submitting the login
* form. It has data for where the user is coming from, so we can display
* a button for importing content.
*/
class LoginController extends ControllerBase {
const COOKIE_CALLBACK_URL = 'bnf_server_login.callback_url';
const COOKIE_SITE_NAME = 'bnf_server_login.site_name';

/**
* Receiving the login.
*/
public function login(Request $request): RedirectResponse {
$url = $request->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;
}

}

0 comments on commit cee1d9c

Please sign in to comment.