Skip to content

Commit 89fb6c3

Browse files
committed
Cleaned up OIDC settings
1 parent da21d29 commit 89fb6c3

4 files changed

Lines changed: 44 additions & 72 deletions

File tree

web/profiles/custom/os2loop/modules/os2loop_user_login/README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,10 @@ Go to Administration › Configuration › OS2Loop › OS2Loop user login settin
77

88
## OpenID Connect
99

10-
The modules [OpenID Connect](https://www.drupal.org/project/openid_connect) and
11-
[OpenID Connect Microsoft Azure Active Directory
12-
client](https://www.drupal.org/project/openid_connect_windows_aad) are used for
13-
OpenID Connect login. *Note*: Eventhough it's called “OpenID Connect Microsoft
14-
Azure Active Directory client” it also work with other OpenID Connect identity
15-
providers.
16-
17-
In the default configuration both login methods assume that the identitity
10+
The module [OpenID Connect](https://www.drupal.org/project/openid_connect) is
11+
used for OpenID Connect login.
12+
13+
In the default configuration the login method assumes that the identitity
1814
provider returns a `name` claim which is used as the Drupal user name and that a
1915
`groups` claim is a list of groups that can be mapped to Drupal roles.
2016

@@ -83,12 +79,16 @@ $config['openid_connect.client.generic']['settings']['authorization_endpoint'] =
8379
$config['openid_connect.client.generic']['settings']['token_endpoint'] = …; // Get this from your OpenID Connect Discovery endpoint
8480
// Optional
8581
$config['openid_connect.client.generic']['settings']['end_session_endpoint'] = …; // Get this from your OpenID Connect Discovery endpoint
82+
83+
// Disable "Autostart login process"
84+
$config['openid_connect.settings']['autostart_login'] = false;
8685
```
8786

8887
Check your overwrites by running
8988

9089
```sh
9190
vendor/bin/drush config:get --include-overridden openid_connect.client.generic
91+
vendor/bin/drush config:get --include-overridden openid_connect.settings
9292
```
9393

9494
#### Groups to roles mapping

web/profiles/custom/os2loop/modules/os2loop_user_login/os2loop_user_login.module

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,8 @@
55
* The module file for os2loop_user_login.
66
*/
77

8-
use Drupal\Core\Form\FormStateInterface;
98
use Drupal\user\UserInterface;
109

11-
/**
12-
* Implements hook_form_alter().
13-
*
14-
* @see \Drupal\os2loop_user_login\Helper\Helper::alterForm()
15-
*/
16-
function os2loop_user_login_form_alter(&$form, FormStateInterface $form_state, $form_id) {
17-
Drupal::service('os2loop_user_login.helper')->alterForm($form, $form_state, $form_id);
18-
}
19-
2010
/**
2111
* Implements hook_menu_local_tasks_alter().
2212
*

web/profiles/custom/os2loop/modules/os2loop_user_login/src/Form/SettingsForm.php

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -73,37 +73,43 @@ public function buildForm(array $form, FormStateInterface $form_state) {
7373
$form['show_drupal_login'] = [
7474
'#type' => 'checkbox',
7575
'#title' => $this->t('Show Drupal login'),
76-
'#default_value' => $config->get('show_drupal_login'),
76+
'#default_value' => FALSE,
77+
'#disabled' => TRUE,
7778
'#description' => $this->t(
78-
'Show Drupal (username and password) login on user login page. If not enabled, the login form will still be visible if <a href="@login_url"><code>#drupal-login</code></a> is appended to the url (<a href="@login_url">@login_url</a>).',
79+
'This option has been removed. This is now controlled by the "@config_title" setting in the <a href=":config_url">OpenID Connect settings</a>.',
7980
[
80-
'@login_url' => Url::fromRoute('user.login', [], [
81-
'absolute' => TRUE,
82-
'fragment' => 'drupal-login',
83-
])->toString(),
84-
]),
81+
'@config_title' => $this->t('OpenID buttons display in user login form'),
82+
':config_url' => Url::fromRoute('openid_connect.admin_settings')->toString(),
83+
],
84+
),
8585
];
8686

8787
$form['show_oidc_login'] = [
8888
'#type' => 'checkbox',
8989
'#title' => $this->t('Show OpenID Connect login'),
90-
'#default_value' => $config->get('show_oidc_login'),
90+
'#default_value' => FALSE,
91+
'#disabled' => TRUE,
9192
'#description' => $this->t(
92-
'Show OpenID Connect login button on user login page. Set up proper <a href="@config_url">OpenID Connect configuration</a> before enabling this.',
93+
'This option has been removed. This is now controlled by the "@config_title" setting in the <a href=":config_url">OpenID Connect settings</a>.',
9394
[
94-
'@config_url' => Url::fromRoute('openid_connect.admin_settings')->toString(),
95-
]
95+
'@config_title' => $this->t('OpenID buttons display in user login form'),
96+
':config_url' => Url::fromRoute('openid_connect.admin_settings')->toString(),
97+
],
9698
),
9799
];
98100

99-
$options['oidc'] = $this->t('OpenID Connect');
100101
$form['default_login_method'] = [
101102
'#type' => 'select',
102103
'#title' => $this->t('Default login method'),
103-
'#options' => $options,
104-
'#empty_value' => '',
105-
'#default_value' => $config->get('default_login_method'),
106-
'#description' => $this->t('The default login method to use. If specified, anonymous users will automatically be logged in with this method.'),
104+
'#default_value' => FALSE,
105+
'#disabled' => TRUE,
106+
'#description' => $this->t(
107+
'This option has been removed. This is now controlled by the "@config_title" setting in the <a href=":config_url">OpenID Connect settings</a>.',
108+
[
109+
'@config_title' => $this->t('Autostart login process'),
110+
':config_url' => Url::fromRoute('openid_connect.admin_settings')->toString(),
111+
],
112+
),
107113
];
108114

109115
$form['hide_logout_menu_item'] = [
@@ -121,9 +127,6 @@ public function buildForm(array $form, FormStateInterface $form_state) {
121127
*/
122128
public function submitForm(array &$form, FormStateInterface $form_state) {
123129
$this->configFactory->getEditable(static::SETTINGS_NAME)
124-
->set('show_drupal_login', $form_state->getValue('show_drupal_login'))
125-
->set('show_oidc_login', $form_state->getValue('show_oidc_login'))
126-
->set('default_login_method', $form_state->getValue('default_login_method'))
127130
->set('hide_logout_menu_item', $form_state->getValue('hide_logout_menu_item'))
128131
->save();
129132

web/profiles/custom/os2loop/modules/os2loop_user_login/src/Helper/Helper.php

Lines changed: 14 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use Drupal\Core\Entity\EntityFieldManager;
66
use Drupal\Core\Entity\EntityTypeManagerInterface;
77
use Drupal\Core\Extension\ModuleHandlerInterface;
8-
use Drupal\Core\Form\FormStateInterface;
98
use Drupal\Core\Messenger\MessengerInterface;
109
use Drupal\Core\Path\CurrentPathStack;
1110
use Drupal\Core\Session\AccountInterface;
@@ -28,6 +27,13 @@ class Helper {
2827
*/
2928
private $config;
3029

30+
/**
31+
* The OpenID Connect config.
32+
*
33+
* @var \Drupal\Core\Config\ImmutableConfig
34+
*/
35+
private $openIdConnectConfig;
36+
3137
/**
3238
* The entity type manager.
3339
*
@@ -75,6 +81,7 @@ class Helper {
7581
*/
7682
public function __construct(Settings $settings, ModuleHandlerInterface $module_handler, EntityTypeManagerInterface $entity_type_manager, EntityFieldManager $entity_field_manager, MessengerInterface $messenger, RequestStack $requestStack, CurrentPathStack $currentPathStack) {
7783
$this->config = $settings->getConfig(SettingsForm::SETTINGS_NAME);
84+
$this->openIdConnectConfig = $settings->getConfig('openid_connect.settings');
7885
$this->moduleHandler = $module_handler;
7986
$this->entityTypeManager = $entity_type_manager;
8087
$this->entityFieldManager = $entity_field_manager;
@@ -83,39 +90,6 @@ public function __construct(Settings $settings, ModuleHandlerInterface $module_h
8390
$this->currentPathStack = $currentPathStack;
8491
}
8592

86-
/**
87-
* Implements hook_form_alter().
88-
*
89-
* Show different login options depending on the site configuration.
90-
*/
91-
public function alterForm(&$form, FormStateInterface $form_state, $form_id) {
92-
if ('openid_connect_login_form' === $form_id) {
93-
if (!$this->config->get('show_oidc_login')) {
94-
$form['#access'] = FALSE;
95-
}
96-
}
97-
elseif ('user_login_form' === $form_id) {
98-
if (!$this->config->get('show_drupal_login')) {
99-
$form['#attached']['library'][] = 'os2loop_user_login/user-login-form';
100-
101-
// Wrap default Drupal login form in an element with a known id
102-
// (drupal-login) so we can visually hide it.
103-
foreach ($form as $key => $value) {
104-
if (0 !== strpos($key, '#')) {
105-
$form['drupal_login'][$key] = array_merge($value);
106-
unset($form[$key]);
107-
}
108-
}
109-
$form['drupal_login'] += [
110-
'#type' => 'fieldset',
111-
'#title' => $this->t('Drupal login'),
112-
'#weight' => 100,
113-
'#attributes' => ['id' => 'drupal-login'],
114-
];
115-
}
116-
}
117-
}
118-
11993
/**
12094
* Implements hook_preprocess_block().
12195
*/
@@ -138,7 +112,12 @@ public function preprocessBlock(array &$variables) {
138112
return;
139113
}
140114

141-
$defaultLoginMethod = $this->config->get('default_login_method');
115+
// The OpenID Connect module's "Autostart login process" triggers only on
116+
// login, register or password reset pages. We need to trigger it in the
117+
// userlogin block as well and use JavaScript to submit the (OIDC) login
118+
// form if found on the page
119+
// (cf. @os2loop_theme/templates/block/block--user-login-block.html.twig).
120+
$defaultLoginMethod = TRUE === $this->openIdConnectConfig->get('autostart_login') ? 'oidc' : NULL;
142121
switch ($defaultLoginMethod) {
143122
case 'oidc':
144123
$variables['default_login_form_id'] = 'openid-connect-login-form';

0 commit comments

Comments
 (0)