-
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.
Merge pull request #1900 from danskernesdigitalebibliotek/bnf-login-f…
…orm_DDFHER-165 Adding a BNF login form for client sites. DDFHER-165
- Loading branch information
Showing
5 changed files
with
99 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
35 changes: 35 additions & 0 deletions
35
web/modules/custom/bnf/bnf_client/src/Controller/BnfRedirecter.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,35 @@ | ||
<?php | ||
|
||
namespace Drupal\bnf_client\Controller; | ||
|
||
use Drupal\Core\Controller\ControllerBase; | ||
|
||
use Drupal\Core\Routing\TrustedRedirectResponse; | ||
use Drupal\Core\Url; | ||
use Symfony\Component\HttpFoundation\Request; | ||
|
||
/** | ||
* Redirecting the editor to BNF. | ||
*/ | ||
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"; | ||
|
||
$url = Url::fromUri($loginUrl, [ | ||
'query' => [ | ||
'siteName' => $this->config('system.site')->get('name'), | ||
'callbackUrl' => $request->getSchemeAndHttpHost(), | ||
], | ||
]); | ||
|
||
$url->setAbsolute(); | ||
|
||
return new TrustedRedirectResponse($url->toString()); | ||
} | ||
|
||
} |
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,9 @@ | ||
--- | ||
bnf_server.cookie_login: | ||
path: '/bnf/login' | ||
defaults: | ||
_controller: '\Drupal\bnf_server\Controller\LoginController::login' | ||
requirements: | ||
_permission: 'access content' | ||
options: | ||
no_cache: true |
42 changes: 42 additions & 0 deletions
42
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,42 @@ | ||
<?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 { | ||
$queries = $request->query; | ||
$url = (string) $queries->get('callbackUrl'); | ||
$name = (string) $queries->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; | ||
} | ||
|
||
} |