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

LOOP-1010: Auto login #170

Merged
merged 2 commits into from
Aug 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ $config['openid_connect.settings']['userinfo_mappings']['os2loop_user_place'] =

### IdP configuration

Your identity provider must allow `https://«OS2Loop
url»/openid-connect/generic` as a valid return url.
Your identity provider must allow `https://«OS2Loop url»/openid-connect/generic`
as a valid return url.

```php
// web/sites/*/settings.local.php
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,19 @@ function os2loop_user_login_menu_local_tasks_alter(&$data, $route_name) {
/**
* Implements hook_user_login().
*/
function os2loop_user_user_login(UserInterface $account) {
function os2loop_user_login_user_login(UserInterface $account) {
Drupal::service('os2loop_user_login.helper')->userLogin($account);
}

/**
* Implements hook_preprocess_block().
*
* @see \Drupal\os2loop_user_login\Helper\Helper::preprocessBlock()
*/
function os2loop_user_login_preprocess_block(array &$variables) {
Drupal::service('os2loop_user_login.helper')->preprocessBlock($variables);
}

/**
* Implements hook_openid_connect_userinfo_alter().
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,21 @@ public function buildForm(array $form, FormStateInterface $form_state) {
),
];

$options = [];
$options['oidc'] = $this->t('OpenID Connect');
// @todo handle SAML
// $options['saml'] = $this->t('SAML');
if (!empty($options)) {
$form['default_login_method'] = [
'#type' => 'select',
'#title' => $this->t('Default login method'),
'#options' => $options,
'#empty_value' => '',
'#default_value' => $config->get('default_login_method'),
'#description' => $this->t('The default login method to use. If specified, anonymous users will automatically be logged id with this method.'),
];
}

return parent::buildForm($form, $form_state);
}

Expand All @@ -119,6 +134,7 @@ public function submitForm(array &$form, FormStateInterface $form_state) {
->set('show_drupal_login', $form_state->getValue('show_drupal_login'))
->set('show_oidc_login', $form_state->getValue('show_oidc_login'))
->set('show_saml_login', $form_state->getValue('show_saml_login'))
->set('default_login_method', $form_state->getValue('default_login_method'))
->save();

drupal_flush_all_caches();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,24 @@ public function alterForm(&$form, FormStateInterface $form_state, $form_id) {
}
}

/**
* Implements hook_preprocess_block().
*/
public function preprocessBlock(array &$variables) {
if ('userlogin' === ($variables['elements']['#id'] ?? NULL)) {
$defaultLoginMethod = $this->config->get('default_login_method');
switch ($defaultLoginMethod) {
case 'oidc':
$variables['default_login_form_id'] = 'openid-connect-login-form';
break;

case 'saml':
// @todo Handle SAML redirect
break;
}
}
}

/**
* Remove "Connected accounts" tab on user profile and edit form.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,15 @@
{{ content|without('user_links') }}
{% endblock %}
</div>

{# Check for default login form and submit if found. #}
{% if default_login_form_id|default(null) %}
<script>
(function() {
var form = document.getElementById({{ default_login_form_id|json_encode|raw }});
if (form) {
form.submit()
}
})()
</script>
{% endif %}