You are here

function tfa_basic_setup_form_submit in TFA Basic plugins 7

Setup form submit.

1 string reference to 'tfa_basic_setup_form_submit'
tfa_basic_setup_form in ./tfa_basic.pages.inc
TFA setup form router.

File

./tfa_basic.pages.inc, line 561

Code

function tfa_basic_setup_form_submit($form, &$form_state) {
  $account = $form['account']['#value'];

  // Cancel button.
  if (isset($form_state['values']['cancel']) && $form_state['values']['op'] === $form_state['values']['cancel']) {
    drupal_set_message('TFA setup canceled.');
    $form_state['redirect'] = 'user/' . $account->uid . '/security/tfa';
    return;
  }

  // Password validation.
  if (isset($form_state['values']['current_pass'])) {
    $form_state['storage']['pass_confirmed'] = TRUE;
    $form_state['rebuild'] = TRUE;
    return;
  }
  elseif (!empty($form_state['values']['sms_number'])) {

    // Send code to number.
    $form_state['storage']['sms_number'] = $form_state['values']['sms_number'];
    $context = array(
      'uid' => $account->uid,
      'mobile_number' => $form_state['storage']['sms_number'],
    );
    $client = tfa_basic_get_twilio_client();
    $setup_plugin = new TfaBasicSmsSetup($context, $form_state['storage']['sms_number'], $client);
    $tfa_setup = new TfaSetup($setup_plugin, $context);
    $tfa_setup
      ->begin();
    $errors = $tfa_setup
      ->getErrorMessages();
    if (!empty($errors)) {
      foreach ($errors as $error) {
        form_set_error('number', $error);
      }
    }
    else {

      // No errors so store setup.
      $form_state['storage']['tfa_basic_sms'] = $tfa_setup;
    }
    $form_state['rebuild'] = TRUE;
    return;
  }

  // Disabling SMS delivery.
  if (isset($form_state['values']['sms_disable']) && $form_state['values']['op'] === $form_state['values']['sms_disable']) {
    tfa_basic_setup_save_data($account, array(
      'sms' => FALSE,
    ));
    drupal_set_message(t('TFA SMS delivery disabled.'));
    $form_state['redirect'] = 'user/' . $account->uid . '/security/tfa';
    watchdog('tfa_basic', 'TFA SMS disabled for user @name UID !uid', array(
      '@name' => $account->name,
      '!uid' => $account->uid,
    ), WATCHDOG_INFO);
    return;
  }
  elseif (!empty($form_state['storage']['step_method'])) {
    $method = $form_state['storage']['step_method'];
    $skipped_method = FALSE;

    // Support skipping optional steps when in full setup.
    if (isset($form_state['values']['skip']) && $form_state['values']['op'] === $form_state['values']['skip']) {
      $skipped_method = $method;
      $form_state['storage']['steps_skipped'][] = $method;
      unset($form_state['storage'][$method]);
    }

    // Trigger multi-step if in full setup.
    if (!empty($form_state['storage']['full_setup'])) {
      _tfa_basic_set_next_step($form_state, $method, $skipped_method);
    }

    // Plugin form submit.
    if (!empty($form_state['storage'][$method])) {
      $setup_class = $form_state['storage'][$method];
      if (!$setup_class
        ->submitForm($form, $form_state)) {
        drupal_set_message(t('There was an error during TFA setup. Your settings have not been saved.'), 'error');
        $form_state['redirect'] = 'user/' . $account->uid . '/security/tfa';
        return;
      }
    }

    // Save user TFA settings for relevant plugins that weren't skipped.
    if (empty($skipped_method) && $method == 'tfa_basic_sms' && isset($form_state['storage']['sms_number']) && in_array('tfa_basic_sms', $form_state['storage']['steps'])) {

      // Update mobile number if different than stored.
      if ($form_state['storage']['sms_number'] !== tfa_basic_get_mobile_number($account)) {
        tfa_basic_set_mobile_number($account, $form_state['storage']['sms_number']);
      }
      tfa_basic_setup_save_data($account, array(
        'sms' => TRUE,
      ));
    }

    // Return if multi-step.
    if (isset($form_state['rebuild']) && $form_state['rebuild']) {
      return;
    }

    // Else, setup complete and return to overview page.
    drupal_set_message(t('TFA setup complete.'));
    $form_state['redirect'] = 'user/' . $account->uid . '/security/tfa';

    // Log and notify if this was full setup.
    if (!empty($form_state['storage']['full_setup'])) {
      $data = array(
        'plugins' => array_diff($form_state['storage']['steps'], $form_state['storage']['steps_skipped']),
      );
      tfa_basic_setup_save_data($account, $data);
      $params = array(
        'account' => $account,
      );
      drupal_mail('tfa_basic', 'tfa_basic_tfa_enabled', $account->mail, user_preferred_language($account), $params);
      watchdog('tfa_basic', 'TFA enabled for user @name UID !uid', array(
        '@name' => $account->name,
        '!uid' => $account->uid,
      ), WATCHDOG_INFO);
    }
  }
}