You are here

function mo_auth_configure_kba_submit in Google Authenticator / 2 Factor Authentication - 2FA 7

File

./configure_kba.inc, line 35

Code

function mo_auth_configure_kba_submit($form, &$form_state) {
  $form_state['rebuild'] = TRUE;
  global $base_url, $user;
  $user = user_load($user->uid);
  $user_email = $user->miniorange_registered_email[LANGUAGE_NONE][0]['value'];
  $ques1 = $form_state['input']['mo2f_kbaquestion1'];
  $ans1 = $form_state['input']['mo2f_kbaanswer1'];
  $ques2 = $form_state['input']['mo2f_kbaquestion2'];
  $ans2 = $form_state['input']['mo2f_kbaanswer2'];
  $ques3 = $form_state['input']['mo2f_kbaquestion3'];
  $ans3 = $form_state['input']['mo2f_kbaanswer3'];
  if ($ques1 == $ques2 || $ques1 == $ques3 || $ques2 == $ques3) {
    form_set_error('configuration', t('The questions you select must not be same.'));
    return;
  }
  $qa1 = array(
    "question" => $ques1,
    "answer" => $ans1,
  );
  $qa2 = array(
    "question" => $ques2,
    "answer" => $ans2,
  );
  $qa3 = array(
    "question" => $ques3,
    "answer" => $ans3,
  );
  $kba = array(
    $qa1,
    $qa2,
    $qa3,
  );
  $customer = new MiniorangeCustomerProfile();
  $miniorange_user = new MiniorangeUser($customer
    ->getCustomerID(), $user_email, NULL, NULL, AuthenticationType::$KBA['code']);
  $auth_api_handler = new AuthenticationAPIHandler($customer
    ->getCustomerID(), $customer
    ->getAPIKey());
  $response = $auth_api_handler
    ->register($miniorange_user, AuthenticationType::$KBA['code'], NULL, NULL, $kba);

  // Clear all the messages
  drupal_get_messages();

  // read API response
  if ($response->status == 'SUCCESS') {
    drupal_set_message(t(''), 'status');
    $configured_methods = mo_auth_get_configured_methods($user->uid);

    // $configured_methods[] = AuthenticationType::$GOOGLE_AUTHENTICATOR['code'];
    // $user->configured_auth_methods[LANGUAGE_NONE][0]['value'] = $configured_methods;
    if (!in_array(AuthenticationType::$KBA['code'], $configured_methods)) {
      array_push($user->configured_auth_methods[LANGUAGE_NONE], array(
        'value' => AuthenticationType::$KBA['code'],
      ));
    }
    $user_api_handler = new UsersAPIHandler($customer
      ->getCustomerID(), $customer
      ->getAPIKey());
    $response = $user_api_handler
      ->update($miniorange_user);
    if ($response->status == 'SUCCESS') {
      user_save($user);
      drupal_goto('admin/config/people/mo_auth/setup');
    }
  }
  elseif ($response->status == 'FAILED') {
    form_set_error('form', t('The passcode you have entered is incorrect. Please try again.'));

    // $form_state['redirect'] = FALSE;
    // drupal_goto('admin/config/people/mo_auth/setup-twofactor');
  }
  else {
    form_set_error('form', t('An error occured while processing your request. Please try again.'));
    watchdog('miniorange_2fa', $response->message);

    // drupal_goto('admin/config/people/mo_auth/setup-twofactor');
  }
}