You are here

public function MiniorangeConfigOAuthClient::submitForm in OAuth2 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/MiniorangeConfigOAuthClient.php, line 370

Class

MiniorangeConfigOAuthClient

Namespace

Drupal\oauth2_login\Form

Code

public function submitForm(array &$form, \Drupal\Core\Form\FormStateInterface $form_state) {
  global $base_url;
  $baseUrlValue = Utilities::getOAuthBaseURL($base_url);
  if (isset($form['miniorange_oauth_client_app'])) {
    $client_app = $form['miniorange_oauth_client_app']['#value'];
  }
  if (isset($form['miniorange_oauth_app_name']['#value'])) {
    $app_name = $form['miniorange_oauth_app_name']['#value'];
  }
  $app_name = str_replace(' ', '', $app_name);
  if (isset($form['miniorange_oauth_client_display_name']['#value'])) {
    $display_name = $form['miniorange_oauth_client_display_name']['#value'];
  }
  if (isset($form['miniorange_oauth_client_id'])) {
    $client_id = trim($form['miniorange_oauth_client_id']['#value']);
  }
  if (isset($form['miniorange_oauth_client_secret']['#value'])) {
    $client_secret = trim($form['miniorange_oauth_client_secret']['#value']);
  }
  if (isset($form['miniorange_oauth_client_scope']['#value'])) {
    $scope = trim($form['miniorange_oauth_client_scope']['#value']);
  }
  if (isset($form['miniorange_oauth_client_authorize_endpoint']['#value'])) {
    $authorize_endpoint = trim($form['miniorange_oauth_client_authorize_endpoint']['#value']);
  }
  if (isset($form['miniorange_oauth_client_access_token_endpoint']['#value'])) {
    $access_token_ep = trim($form['miniorange_oauth_client_access_token_endpoint']['#value']);
  }
  if (isset($form['miniorange_oauth_client_userinfo_endpoint']['#value'])) {
    $user_info_ep = trim($form['miniorange_oauth_client_userinfo_endpoint']['#value']);
  }
  if ($client_app == 'Select' || empty($client_app) || empty($app_name) || empty($client_id) || empty($client_secret) || empty($authorize_endpoint) || empty($access_token_ep) || empty($user_info_ep)) {
    if (empty($client_app) || $client_app == 'Select') {
      \Drupal::messenger()
        ->addMessage(t('The <b>Select Application</b> dropdown is required. Please Select your application.'), 'error');
      return;
    }
    \Drupal::messenger()
      ->addMessage(t('The <b>Custom App name</b>, <b>Client ID</b>, <b>Client Secret</b>, <b>Authorize Endpoint</b>, <b>Access Token Endpoint</b>
                , <b>Get User Info Endpoint</b> fields are required.'), 'error');
    return;
  }
  if (empty($client_app)) {
    $client_app = \Drupal::config('oauth2_login.settings')
      ->get('miniorange_oauth_client_app');
  }
  if (empty($app_name)) {
    $client_app = \Drupal::config('oauth2_login.settings')
      ->get('miniorange_auth_client_app_name');
  }
  if (empty($display_name)) {
    $display_name = '';
  }
  if (empty($client_id)) {
    $client_id = \Drupal::config('oauth2_login.settings')
      ->get('miniorange_auth_client_client_id');
  }
  if (empty($client_secret)) {
    $client_secret = \Drupal::config('oauth2_login.settings')
      ->get('miniorange_auth_client_client_secret');
  }
  if (empty($scope)) {
    $scope = \Drupal::config('oauth2_login.settings')
      ->get('miniorange_auth_client_scope');
  }
  if (empty($authorize_endpoint)) {
    $authorize_endpoint = \Drupal::config('oauth2_login.settings')
      ->get('miniorange_auth_client_authorize_endpoint');
  }
  if (empty($access_token_ep)) {
    $access_token_ep = \Drupal::config('oauth2_login.settings')
      ->get('miniorange_auth_client_access_token_ep');
  }
  if (empty($user_info_ep)) {
    $user_info_ep = \Drupal::config('oauth2_login.settings')
      ->get('miniorange_oauth_client_userinfo_endpoint');
  }
  $callback_uri = $baseUrlValue . "/mo_login";
  $app_values = \Drupal::config('oauth2_login.settings')
    ->get('miniorange_oauth_client_appval');
  if (!is_array($app_values)) {
    $app_values = array();
  }
  $app_values['client_id'] = $client_id;
  $app_values['client_secret'] = $client_secret;
  $app_values['app_name'] = $app_name;
  $app_values['display_name'] = $display_name;
  $app_values['scope'] = $scope;
  $app_values['authorize_endpoint'] = $authorize_endpoint;
  $app_values['access_token_ep'] = $access_token_ep;
  $app_values['user_info_ep'] = $user_info_ep;
  $app_values['callback_uri'] = $callback_uri;
  $app_values['client_app'] = $client_app;
  $enable_login_with_oauth = $form['miniorange_oauth_enable_login_with_oauth']['#value'];
  $enable_login = $enable_login_with_oauth == 1 ? TRUE : FALSE;
  $enable_with_header = $form['miniorange_oauth_send_with_header_oauth']['#value'];
  $enable_with_body = $form['miniorange_oauth_send_with_body_oauth']['#value'];
  $enable_header = $enable_with_header == 1 ? TRUE : FALSE;
  $enable_body = $enable_with_body == 1 ? TRUE : FALSE;
  \Drupal::configFactory()
    ->getEditable('oauth2_login.settings')
    ->set('miniorange_oauth_enable_login_with_oauth', $enable_login)
    ->save();
  \Drupal::configFactory()
    ->getEditable('oauth2_login.settings')
    ->set('miniorange_oauth_client_app', $client_app)
    ->save();
  \Drupal::configFactory()
    ->getEditable('oauth2_login.settings')
    ->set('miniorange_oauth_client_appval', $app_values)
    ->save();
  \Drupal::configFactory()
    ->getEditable('oauth2_login.settings')
    ->set('miniorange_auth_client_app_name', $app_name)
    ->save();
  \Drupal::configFactory()
    ->getEditable('oauth2_login.settings')
    ->set('miniorange_auth_client_display_name', $display_name)
    ->save();
  \Drupal::configFactory()
    ->getEditable('oauth2_login.settings')
    ->set('miniorange_auth_client_client_id', $client_id)
    ->save();
  \Drupal::configFactory()
    ->getEditable('oauth2_login.settings')
    ->set('miniorange_auth_client_client_secret', $client_secret)
    ->save();
  \Drupal::configFactory()
    ->getEditable('oauth2_login.settings')
    ->set('miniorange_auth_client_scope', $scope)
    ->save();
  \Drupal::configFactory()
    ->getEditable('oauth2_login.settings')
    ->set('miniorange_auth_client_authorize_endpoint', $authorize_endpoint)
    ->save();
  \Drupal::configFactory()
    ->getEditable('oauth2_login.settings')
    ->set('miniorange_auth_client_access_token_ep', $access_token_ep)
    ->save();
  \Drupal::configFactory()
    ->getEditable('oauth2_login.settings')
    ->set('miniorange_auth_client_user_info_ep', $user_info_ep)
    ->save();
  \Drupal::configFactory()
    ->getEditable('oauth2_login.settings')
    ->set('miniorange_auth_client_stat', "Review Config")
    ->save();
  \Drupal::configFactory()
    ->getEditable('oauth2_login.settings')
    ->set('miniorange_auth_client_callback_uri', $callback_uri)
    ->save();
  \Drupal::configFactory()
    ->getEditable('oauth2_login.settings')
    ->set('miniorange_oauth_send_with_header_oauth', $enable_header)
    ->save();
  \Drupal::configFactory()
    ->getEditable('oauth2_login.settings')
    ->set('miniorange_oauth_send_with_body_oauth', $enable_body)
    ->save();
  \Drupal::messenger()
    ->addMessage(t('Configurations saved successfully.'));
}