Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make /user/register redirect optional #85

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions behat.yml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ default:
user registration: '/user/register'
extensions:
Drupal\MinkExtension:
goutte: ~
selenium2: ~
goutte:
guzzle_parameters:
verify: false
pfrenssen marked this conversation as resolved.
Show resolved Hide resolved
ajax_timeout: 10
browser_name: 'chrome'
javascript_session: 'selenium2'
Expand Down
1 change: 1 addition & 0 deletions config/install/oe_authentication.settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ register_path: 'eim/external/register.cgi'
validation_path: 'TicketValidationService'
assurance_level: TOP
ticket_types: SERVICE,PROXY
redirect_user_register_route: true
pfrenssen marked this conversation as resolved.
Show resolved Hide resolved
5 changes: 4 additions & 1 deletion config/schema/oe_authentication.schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,7 @@ oe_authentication.settings:
label: 'Application assurance levels'
ticket_types:
type: string
label: 'Application available ticket types'
label: 'Application available ticket types'
redirect_user_register_route:
pfrenssen marked this conversation as resolved.
Show resolved Hide resolved
type: boolean
label: 'Redirect Drupal user registration route to EU Login'
17 changes: 17 additions & 0 deletions oe_authentication.post_update.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

/**
* @file
* Post update functions for OE Authentication module.
*/

declare(strict_types = 1);

/**
* Add the 'redirect_user_register_route' module setting.
*/
function oe_authentication_post_update_user_register_redirect(): void {
\Drupal::configFactory()->getEditable('oe_authentication.settings')
->set('redirect_user_register_route', TRUE)
->save();
}
8 changes: 7 additions & 1 deletion oe_authentication.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,17 @@ services:
tags:
- { name: access_check, applies_to: _external_user_access_check }
oe_authentication.route_subscriber:
class: \Drupal\oe_authentication\Routing\RouteSubscriber
class: Drupal\oe_authentication\Routing\RouteSubscriber
arguments: ['@config.factory']
tags:
- { name: event_subscriber }
oe_authentication.event_subscriber:
class: \Drupal\oe_authentication\Event\EuLoginEventSubscriber
tags:
- { name: event_subscriber }
arguments: ['@config.factory', '@messenger']
oe_authentication.config_subscriber:
class: Drupal\oe_authentication\Event\UserRegisterRouteRedirectConfigSubscriber
arguments: ['@router.builder']
tags:
- { name: event_subscriber }
10 changes: 6 additions & 4 deletions src/Controller/RegisterController.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,13 @@ public function register(): TrustedRedirectResponse {
$url = $this->getRegisterUrl()->toString();
$response = new TrustedRedirectResponse($url);

$cache = new CacheableMetadata();
$cache->addCacheContexts(['user.roles:anonymous']);
$response->addCacheableDependency($cache);
$cache_metadata = (new CacheableMetadata())
->addCacheContexts(['user.roles:anonymous']);
$oe_auth_settings = $this->configFactory->get('oe_authentication.settings');

return $response;
return $response
->addCacheableDependency($cache_metadata)
->addCacheableDependency($oe_auth_settings);
}

/**
Expand Down
62 changes: 62 additions & 0 deletions src/Event/UserRegisterRouteRedirectConfigSubscriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

declare(strict_types = 1);

namespace Drupal\oe_authentication\Event;

use Drupal\Core\Config\ConfigCrudEvent;
use Drupal\Core\Config\ConfigEvents;
use Drupal\Core\Routing\RouteBuilderInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
* Acts when oe_authentication.settings:redirect_user_register_route changes.
*/
class UserRegisterRouteRedirectConfigSubscriber implements EventSubscriberInterface {

/**
* The route builder service.
*
* @var \Drupal\Core\Routing\RouteBuilderInterface
*/
protected $routeBuilder;

/**
* Constructs a new event subscriber instance.
*
* @param \Drupal\Core\Routing\RouteBuilderInterface $route_builder
* The route builder service.
*/
public function __construct(RouteBuilderInterface $route_builder) {
$this->routeBuilder = $route_builder;
}

/**
* {@inheritdoc}
*/
public static function getSubscribedEvents(): array {
return [
ConfigEvents::SAVE => 'onConfigSave',
];
}

/**
* Rebuilds route table on changing the user registration redirection.
*
* We need to rebuild the route table when an administrator changes the option
* to redirect the user registration form to EU Login. In normal operations
* the route table is only updated when the cache is being rebuilt, but the
* administrator expects this to take effect instantly.
*
pfrenssen marked this conversation as resolved.
Show resolved Hide resolved
* @param \Drupal\Core\Config\ConfigCrudEvent $event
* The config CRUD event.
*/
public function onConfigSave(ConfigCrudEvent $event): void {
if ($event->getConfig()->getName() === 'oe_authentication.settings') {
if ($event->isChanged('redirect_user_register_route')) {
$this->routeBuilder->rebuild();
}
}
}

}
6 changes: 6 additions & 0 deletions src/Form/AuthenticationSettingsForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ public function buildForm(array $form, FormStateInterface $form_state) {
'#title' => $this->t('Application available ticket types'),
'#default_value' => $this->config(static::CONFIG_NAME)->get('ticket_types'),
];
$form['redirect_user_register_route'] = [
'#type' => 'checkbox',
'#title' => $this->t('Redirect user registration route to EU Login'),
'#default_value' => $this->config(static::CONFIG_NAME)->get('redirect_user_register_route'),
];
return parent::buildForm($form, $form_state);
}

Expand All @@ -66,6 +71,7 @@ public function submitForm(array &$form, FormStateInterface $form_state) {
->set('validation_path', $form_state->getValue('validation_path'))
->set('assurance_level', $form_state->getValue('assurance_level'))
->set('ticket_types', $form_state->getValue('ticket_types'))
->set('redirect_user_register_route', $form_state->getValue('redirect_user_register_route'))
->save();
parent::submitForm($form, $form_state);
}
Expand Down
36 changes: 29 additions & 7 deletions src/Routing/RouteSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Drupal\oe_authentication\Routing;

use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Routing\RouteSubscriberBase;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
Expand All @@ -13,6 +14,23 @@
*/
class RouteSubscriber extends RouteSubscriberBase {

/**
* The config factory service.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $configFactory;

/**
* Constructs a new route event subscriber.
*
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The config factory service.
*/
public function __construct(ConfigFactoryInterface $config_factory) {
$this->configFactory = $config_factory;
}

/**
* {@inheritdoc}
*/
Expand All @@ -32,13 +50,17 @@ protected function alterRoutes(RouteCollection $collection): void {

}

// Replace the core register route controller.
$route = $collection->get('user.register');
if ($route instanceof Route) {
$defaults = $route->getDefaults();
unset($defaults['_form']);
$defaults['_controller'] = '\Drupal\oe_authentication\Controller\RegisterController::register';
$route->setDefaults($defaults);
// Switch the Drupal user register form with a redirect to the EU Login
// registration if this option is enabled in the module configuration.
$config = $this->configFactory->get('oe_authentication.settings');
if ($config->get('redirect_user_register_route')) {
pfrenssen marked this conversation as resolved.
Show resolved Hide resolved
$route = $collection->get('user.register');
if ($route instanceof Route) {
pfrenssen marked this conversation as resolved.
Show resolved Hide resolved
$defaults = $route->getDefaults();
unset($defaults['_form']);
$defaults['_controller'] = '\Drupal\oe_authentication\Controller\RegisterController::register';
$route->setDefaults($defaults);
}
}

// Replace the cas callback route controller.
Expand Down
1 change: 0 additions & 1 deletion tests/Behat/AuthenticationContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Drupal\Tests\oe_authentication\Behat;

use Drupal\DrupalExtension\Context\ConfigContext;
use Drupal\user\Entity\User;
use Behat\Behat\Hook\Scope\BeforeScenarioScope;
use Drupal\DrupalExtension\Context\RawDrupalContext;
Expand Down
17 changes: 16 additions & 1 deletion tests/features/ecas-register.feature
Original file line number Diff line number Diff line change
@@ -1,12 +1,27 @@
@javascript @ecas-login
@api @ecas-login
Feature: Register through OE Authentication
In order to be able to have new users
As an anonymous user of the system
I need to be able to go to the registration URL

@DrupalLogin
Scenario: Register
Given I am an anonymous user
When I visit "the user registration page"
# Redirected to the Ecas mockup server.
And I click "External"
Then I should see "Create an account"

Given I am logged in as a user with the "administer authentication configuration" permission
When I am on "the Authentication configuration page"
And I uncheck "Redirect user registration route to EU Login"
Then I press "Save configuration"

Given I am an anonymous user
When I visit "the user registration page"
Then I should see "Create new account"
pfrenssen marked this conversation as resolved.
Show resolved Hide resolved

Given I am logged in as a user with the "administer authentication configuration" permission
When I am on "the Authentication configuration page"
And I check "Redirect user registration route to EU Login"
Then I press "Save configuration"