You are here

function simplesamlphp_auth_form_alter in simpleSAMLphp Authentication 7.3

Same name and namespace in other branches
  1. 6.3 simplesamlphp_auth.module \simplesamlphp_auth_form_alter()
  2. 6.2 simplesamlphp_auth.module \simplesamlphp_auth_form_alter()
  3. 7 simplesamlphp_auth.module \simplesamlphp_auth_form_alter()
  4. 7.2 simplesamlphp_auth.module \simplesamlphp_auth_form_alter()

Implements hook_form_alter().

File

./simplesamlphp_auth.module, line 288
simpleSAMLphp authentication module for Drupal.

Code

function simplesamlphp_auth_form_alter(&$form, $form_state, $form_id) {
  if (!_simplesamlphp_auth_isEnabled()) {

    // Exit without executing.
    return;
  }
  $is_user_profile_account_form = $form_id == 'user_profile_form' && $form['#user_category'] == 'account';
  $login_path = variable_get('simplesamlphp_auth_login_path', 'saml_login');
  $login_name = variable_get('simplesamlphp_auth_login_link_display_name', t('Federated Log In'));
  $destination = drupal_get_destination();
  $link = l($login_name, $login_path, array(
    'query' => $destination,
  ));

  // Add SAML login link to user login form.
  if ($form_id == 'user_login_block' || $form_id == 'user_account_form') {
    $links = $form['links']['#markup'];
    $links = str_replace('</ul>', '<li class="saml">' . $link . '</li></ul>', $links);
    $form['links']['#markup'] = $links;
  }
  if ($form_id == 'user_login') {
    $form['links']['#markup'] = $link;
  }
  if (($form_id == 'user_register_form' || $is_user_profile_account_form) && user_access('change saml authentication setting')) {
    $form['saml'] = array(
      '#type' => 'checkbox',
      '#title' => t('Enable this user to leverage SAML authentication'),
      '#default_value' => $form_id == 'user_register_form' ? TRUE : (bool) user_get_authmaps($form['#user']->name),
    );
    $form['#submit'][] = 'simplesaml_auth_user_profile_form_submit';
  }

  // If the user has a simplesamlphp_auth authmap record, then don't require
  // them to know their Drupal password. This will allow them to change their
  // e-mail address, and set a Drupal password if they want to (and are
  // allowed).
  if ($is_user_profile_account_form && (isset($form['#user']->init) && $form['#user']->init) && _simplesaml_auth_user_has_authmap($form['#user']->name)) {
    unset($form['account']['current_pass']);
    unset($form['account']['current_pass_required_values']);
    $form['#validate'] = array_diff($form['#validate'], array(
      'user_validate_current_pass',
    ));

    // If the user is a simplesamlphp_auth user and is NOT allowed to set their
    // Drupal password, remove the fields from the form.
    if (!variable_get('simplesamlphp_auth_allowsetdrupalpwd')) {
      unset($form['account']['pass']);
    }
  }
}