public function configure_kba::submitForm in Google Authenticator / 2 Factor Authentication - 2FA 8
Same name and namespace in other branches
- 8.2 src/Form/configure_kba.php \Drupal\miniorange_2fa\form\configure_kba::submitForm()
Form submission handler.
Parameters
array $form: An associative array containing the structure of the form.
\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.
Overrides FormInterface::submitForm
File
- src/
Form/ configure_kba.php, line 134
Class
Namespace
Drupal\miniorange_2fa\formCode
public function submitForm(array &$form, \Drupal\Core\Form\FormStateInterface $form_state) {
$form_state
->setRebuild();
$input = $form_state
->getUserInput();
$user = User::load(\Drupal::currentUser()
->id());
$user_id = $user
->id();
$utilities = new MoAuthUtilities();
$custom_attribute = $utilities::get_users_custom_attribute($user_id);
$user_email = $custom_attribute[0]->miniorange_registered_email;
$ques1 = $input['mo2f_kbaquestion1'];
$ans1 = $input['mo2f_kbaanswer1'];
$ques2 = $input['mo2f_kbaquestion2'];
$ans2 = $input['mo2f_kbaanswer2'];
$ques3 = $input['mo2f_kbaquestion3'];
$ans3 = $input['mo2f_kbaanswer3'];
if (empty($ques1) || empty($ques2) || empty($ques3) || empty($ans1) || empty($ans2) || empty($ans3)) {
\Drupal::messenger()
->addMessage(t('All the questions are mandatory.'), 'error');
return;
}
elseif ($ques1 == $ques2 || $ques1 == $ques3 || $ques2 == $ques3) {
\Drupal::messenger()
->addMessage(t('The questions you select must not be same.'), 'error');
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::messenger()
->deleteAll();
// read API response
if ($response->status == 'SUCCESS') {
\Drupal::messenger()
->addMessage(t(''), 'status');
$configured_methods = MoAuthUtilities::mo_auth_get_configured_methods($user_id);
if (!in_array(AuthenticationType::$KBA['code'], $configured_methods)) {
array_push($configured_methods, AuthenticationType::$KBA['code']);
}
$config_methods = implode(', ', $configured_methods);
$user_api_handler = new UsersAPIHandler($customer
->getCustomerID(), $customer
->getAPIKey());
$response = $user_api_handler
->update($miniorange_user);
if ($response->status == 'SUCCESS') {
// Save User
$user_id = $user
->id();
$utilities = new MoAuthUtilities();
$available = $utilities::check_for_userID($user_id);
$database = \Drupal::database();
if ($available == TRUE) {
$database
->update('UserAuthenticationType')
->fields([
'activated_auth_methods' => AuthenticationType::$KBA['code'],
])
->condition('uid', $user_id, '=')
->execute();
$database
->update('UserAuthenticationType')
->fields([
'configured_auth_methods' => $config_methods,
])
->condition('uid', $user_id, '=')
->execute();
}
else {
echo "error while saving authentication method.";
exit;
}
$message = 'KBA Authentication configured successfully.';
MoAuthUtilities::show_error_or_success_message($message, 'status');
}
}
elseif ($response->status == 'FAILED') {
$message = 'An error occured while configuring KBA Authentication. Please try again.';
MoAuthUtilities::show_error_or_success_message($message, 'error');
}
else {
$message = 'An error occured while processing your request. Please try again.';
MoAuthUtilities::show_error_or_success_message($message, 'error');
}
}