function saml_sp_drupal_login_form_user_login_form_alter in SAML Service Provider 8.2
Same name and namespace in other branches
- 8.3 modules/saml_sp_drupal_login/saml_sp_drupal_login.module \saml_sp_drupal_login_form_user_login_form_alter()
- 4.x modules/saml_sp_drupal_login/saml_sp_drupal_login.module \saml_sp_drupal_login_form_user_login_form_alter()
- 3.x modules/saml_sp_drupal_login/saml_sp_drupal_login.module \saml_sp_drupal_login_form_user_login_form_alter()
File
- modules/
saml_sp_drupal_login/ saml_sp_drupal_login.module, line 35 - SAML Drupal Login
Code
function saml_sp_drupal_login_form_user_login_form_alter(&$form, $form_state, $form_id) {
$config = \Drupal::config('saml_sp_drupal_login.config');
$idps = $config
->get('idp');
$enabled_idps = array();
if (!empty($idps)) {
foreach ($idps as $key => $value) {
if ($value) {
$enabled_idps[$key] = $key;
}
}
}
if (empty($enabled_idps)) {
// there are no enabled IdPs, so we aren't doing anything to the form
return;
}
if ($config
->get('force_saml_only')) {
// Disable caching of the login page.
\Drupal::service('page_cache_kill_switch')
->trigger();
// only SAML logins are accepted, so don't show the form
foreach (Element::children($form) as $key) {
$form[$key]['#access'] = FALSE;
}
if (count($enabled_idps) == 1) {
// there is only one IdP so redirecto to it's login page to remove one step
$redirect_url = \Drupal::url('saml_sp_drupal_login.login', [
'idp' => array_shift($enabled_idps),
]);
$response = new RedirectResponse($redirect_url);
$response
->send();
}
}
$idps = saml_sp__load_all_idps();
$links = array();
foreach ($enabled_idps as $value) {
$links[] = Link::createFromRoute(t('Login to @site_name using %idp.', [
'@site_name' => \Drupal::config('system.site')
->get('name'),
'%idp' => $idps[$value]->label,
]), 'saml_sp_drupal_login.login', [
'idp' => $value,
]);
}
$form['saml_sp_drupal_login_links'] = [
'#theme' => 'item_list',
'#items' => $links,
];
$form['#cache']['tags'] = array_merge($form['#cache']['tags'], $config
->getCacheTags());
}