Skip to content

Commit 4fd4408

Browse files
authored
D8CORE-8340: Block access to password reset and register routes based on saml settings (#31)
1 parent 477dc6e commit 4fd4408

3 files changed

Lines changed: 104 additions & 0 deletions

File tree

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Drupal\stanford_samlauth\EventSubscriber;
6+
7+
use Drupal\Core\Config\ConfigFactoryInterface;
8+
use Drupal\Core\Routing\RouteSubscriberBase;
9+
use Symfony\Component\Routing\RouteCollection;
10+
11+
/**
12+
* Route subscriber.
13+
*/
14+
final class SamlAuthRouteSubscriber extends RouteSubscriberBase {
15+
16+
/**
17+
* Constructs a SamlAuthRouteSubscriber object.
18+
*/
19+
public function __construct(
20+
private readonly ConfigFactoryInterface $configFactory,
21+
) {}
22+
23+
/**
24+
* {@inheritdoc}
25+
*/
26+
protected function alterRoutes(RouteCollection $collection): void {
27+
$login_roles = $this->configFactory->get('samlauth.authentication')
28+
->get('drupal_login_roles') ?: [];
29+
$hide_local_login = $this->configFactory->get('stanford_samlauth.settings')
30+
->get('hide_local_login');
31+
32+
// If local login is allowed or there are some allowed roles to use local
33+
// login, don't restrict access to the routes.
34+
if (array_filter($login_roles) || !$hide_local_login) {
35+
return;
36+
}
37+
$routes_to_block = [
38+
'user.register',
39+
'user.pass',
40+
'user.pass.http',
41+
'user.login.http',
42+
];
43+
foreach ($routes_to_block as $route) {
44+
if ($route = $collection->get($route)) {
45+
$route->setRequirement('_access', 'FALSE');
46+
}
47+
}
48+
}
49+
50+
}

stanford_samlauth.services.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,9 @@ services:
88
arguments: [ '@stanford_samlauth.workgroup_api', '@config.factory', '@current_user', '@path.matcher', '@path.current', '@path_alias.manager' ]
99
tags:
1010
- { name: event_subscriber }
11+
12+
stanford_samlauth.route_subscriber:
13+
class: Drupal\stanford_samlauth\EventSubscriber\SamlAuthRouteSubscriber
14+
arguments: ['@config.factory']
15+
tags:
16+
- { name: event_subscriber }
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
namespace Drupal\Tests\stanford_samlauth\Kernel\EventSubscriber;
4+
5+
use Drupal\Tests\stanford_samlauth\Kernel\StanfordSamlAuthTestBase;
6+
use Drupal\user\Entity\Role;
7+
use PHPUnit\Framework\Attributes\TestWith;
8+
9+
/**
10+
* Test route subscriber.
11+
*/
12+
class SamlAuthRouteSubscriberTest extends StanfordSamlAuthTestBase {
13+
14+
/**
15+
* {@inheritDoc}
16+
*/
17+
public function setup(): void {
18+
parent::setup();
19+
$this->installConfig('samlauth');
20+
}
21+
22+
#[TestWith(['hide_local_login' => TRUE, 'role_mapping' => TRUE])]
23+
#[TestWith(['hide_local_login' => FALSE, 'role_mapping' => TRUE])]
24+
#[TestWith(['hide_local_login' => TRUE, 'role_mapping' => FALSE])]
25+
#[TestWith(['hide_local_login' => FALSE, 'role_mapping' => FALSE])]
26+
public function testUserSyncEvent(bool $hide_local_login, bool $role_mapping) {
27+
$this->config('stanford_samlauth.settings')
28+
->set('hide_local_login', $hide_local_login)
29+
->save();
30+
31+
if ($role_mapping) {
32+
$roles = array_keys(Role::loadMultiple());
33+
$this->config('samlauth.authentication')
34+
->set('drupal_login_roles', $roles)
35+
->save();
36+
}
37+
38+
/** @var \Drupal\Core\Routing\Router $router */
39+
$router = \Drupal::service('router.no_access_checks');
40+
$access = $router->getRouteCollection()
41+
->get('user.pass')
42+
->getRequirement('_access');
43+
44+
$expected = $hide_local_login && !$role_mapping ? 'FALSE' : 'TRUE';
45+
$this->assertEquals($expected, $access);
46+
}
47+
48+
}

0 commit comments

Comments
 (0)