-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
Showing
5 changed files
with
135 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
web/modules/custom/bnf/bnf_client/src/Form/BnfLoginForm.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
41 changes: 41 additions & 0 deletions
41
web/modules/custom/bnf/bnf_server/src/Controller/LoginController.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |