You are here

public function MiniorangeOAuthClientCustomerSetup::submitForm in Drupal OAuth & OpenID Connect Login - OAuth2 Client SSO Login 8

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/MiniorangeOAuthClientCustomerSetup.php, line 212
Contains \Drupal\miniorange_oauth_client\Form\MiniorangeOAuthClientCustomerSetup.

Class

MiniorangeOAuthClientCustomerSetup

Namespace

Drupal\miniorange_oauth_client\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  $username = trim($form['markup_top_vt_start']['miniorange_oauth_client_customer_setup_username']['#value']);
  $phone = $form['markup_top_vt_start']['miniorange_oauth_client_customer_setup_phone']['#value'];
  $password = trim($form['markup_top_vt_start']['miniorange_oauth_client_customer_setup_password']['#value']['pass1']);
  if (empty($username) || empty($password)) {
    \Drupal::messenger()
      ->addMessage(t('The <b><u>Email </u></b> and <b><u>Password</u></b> fields are mandatory.'), 'error');
    return;
  }
  if (!\Drupal::service('email.validator')
    ->isValid($username)) {
    \Drupal::messenger()
      ->addMessage(t('The email address <i>' . $username . '</i> is not valid.'), 'error');
    return;
  }
  $customer_config = new MiniorangeOAuthClientCustomer($username, $phone, $password, NULL);
  $check_customer_response = json_decode($customer_config
    ->checkCustomer());
  if ($check_customer_response->status == 'CUSTOMER_NOT_FOUND') {
    \Drupal::configFactory()
      ->getEditable('miniorange_oauth_client.settings')
      ->set('miniorange_oauth_client_customer_admin_email', $username)
      ->save();
    \Drupal::configFactory()
      ->getEditable('miniorange_oauth_client.settings')
      ->set('miniorange_oauth_client_customer_admin_phone', $phone)
      ->save();
    \Drupal::configFactory()
      ->getEditable('miniorange_oauth_client.settings')
      ->set('miniorange_oauth_client_customer_admin_password', $password)
      ->save();
    $send_otp_response = json_decode($customer_config
      ->sendOtp());
    if ($send_otp_response->status == 'SUCCESS') {
      \Drupal::configFactory()
        ->getEditable('miniorange_oauth_client.settings')
        ->set('miniorange_oauth_client_tx_id', $send_otp_response->txId)
        ->save();
      $current_status = 'VALIDATE_OTP';
      \Drupal::configFactory()
        ->getEditable('miniorange_oauth_client.settings')
        ->set('miniorange_oauth_client_status', $current_status)
        ->save();
      \Drupal::messenger()
        ->addMessage(t('Verify email address by entering the passcode sent to @username', [
        '@username' => $username,
      ]));
    }
  }
  elseif ($check_customer_response->status == 'CURL_ERROR') {
    \Drupal::messenger()
      ->addMessage(t('cURL is not enabled. Please enable cURL'), 'error');
  }
  else {
    $customer_keys_response = json_decode($customer_config
      ->getCustomerKeys());
    if (json_last_error() == JSON_ERROR_NONE) {
      \Drupal::configFactory()
        ->getEditable('miniorange_oauth_client.settings')
        ->set('miniorange_oauth_client_customer_id', $customer_keys_response->id)
        ->save();
      \Drupal::configFactory()
        ->getEditable('miniorange_oauth_client.settings')
        ->set('miniorange_oauth_client_customer_admin_token', $customer_keys_response->token)
        ->save();
      \Drupal::configFactory()
        ->getEditable('miniorange_oauth_client.settings')
        ->set('miniorange_oauth_client_customer_admin_email', $username)
        ->save();
      \Drupal::configFactory()
        ->getEditable('miniorange_oauth_client.settings')
        ->set('miniorange_oauth_client_customer_admin_phone', $phone)
        ->save();
      \Drupal::configFactory()
        ->getEditable('miniorange_oauth_client.settings')
        ->set('miniorange_oauth_client_customer_api_key', $customer_keys_response->apiKey)
        ->save();
      $current_status = 'PLUGIN_CONFIGURATION';
      \Drupal::configFactory()
        ->getEditable('miniorange_oauth_client.settings')
        ->set('miniorange_oauth_client_status', $current_status)
        ->save();
      \Drupal::messenger()
        ->addMessage(t('Successfully retrieved your account.'));
      $register_to_upgrade = \Drupal::config('miniorange_oauth_client.settings')
        ->get('miniorange_oauth_client_redi_upgrade');
      if ($register_to_upgrade == 1) {
        \Drupal::configFactory()
          ->getEditable('miniorange_oauth_client.settings')
          ->set('miniorange_oauth_client_redi_upgrade', '0')
          ->save();
        global $base_url;
        $response = new RedirectResponse($base_url . "/admin/config/people/miniorange_oauth_client/licensing");
        $response
          ->send();
      }
    }
    else {
      if ($check_customer_response->status == 'TRANSACTION_LIMIT_EXCEEDED') {
        \Drupal::messenger()
          ->addMessage(t('An error has been occured. Please try after some time or contact us at <a href="mailto:drupalsupport@xecurify.com" target="_blank">drupalsupport@xecurify.com</a>.'), 'error');
      }
      else {
        \Drupal::messenger()
          ->addMessage(t('Invalid credentials'), 'error');
      }
    }
  }
}