You are here

public function LdapUserTestForm::submitForm in Lightweight Directory Access Protocol (LDAP) 8.3

Same name and namespace in other branches
  1. 8.4 ldap_user/src/Form/LdapUserTestForm.php \Drupal\ldap_user\Form\LdapUserTestForm::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

ldap_user/src/Form/LdapUserTestForm.php, line 110

Class

LdapUserTestForm
A form to allow the administrator to query LDAP.

Namespace

Drupal\ldap_user\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  $username = $form_state
    ->getValue([
    'testing_drupal_username',
  ]);
  $selected_action = $form_state
    ->getValue([
    'action',
  ]);
  $config = $this
    ->configFactory()
    ->get('ldap_user.settings')
    ->get();
  $processor = new DrupalUserProcessor();
  $ldapProcessor = new LdapUserProcessor();
  $user_ldap_entry = FALSE;
  if ($config['drupalAcctProvisionServer']) {
    $user_ldap_entry = $this->serverFactory
      ->getUserDataFromServerByIdentifier($username, $config['drupalAcctProvisionServer']);
  }
  if ($config['ldapEntryProvisionServer']) {
    if (!$user_ldap_entry) {
      $user_ldap_entry = $this->serverFactory
        ->getUserDataFromServerByIdentifier($username, $config['ldapEntryProvisionServer']);
    }
  }
  $results = [];
  $results['username'] = $username;
  $results['related LDAP entry (before provisioning or syncing)'] = $user_ldap_entry;

  /** @var \Drupal\user\Entity\User $account */
  $existingAccount = $this->entityTypeManager
    ->getStorage('user')
    ->loadByProperties([
    'name' => $username,
  ]);
  $existingAccount = $existingAccount ? reset($existingAccount) : FALSE;
  if ($existingAccount) {
    $results['user entity (before provisioning or syncing)'] = $existingAccount
      ->toArray();
    $results['User Authmap'] = ExternalAuthenticationHelper::getUserIdentifierFromMap($existingAccount
      ->id());
  }
  else {
    $results['User Authmap'] = 'No authmaps available.  Authmaps only shown if user account exists beforehand';
  }
  $account = [
    'name' => $username,
  ];
  $sync_trigger_description = self::$syncTriggerOptions[$selected_action];
  foreach ([
    self::PROVISION_TO_DRUPAL,
    self::PROVISION_TO_LDAP,
  ] as $direction) {
    if ($this
      ->provisionEnabled($direction, $selected_action)) {
      if ($direction == self::PROVISION_TO_DRUPAL) {
        $processor
          ->provisionDrupalAccount($account);
        $results['provisionDrupalAccount method results']["context = {$sync_trigger_description}"]['proposed'] = $account;
      }
      else {
        $provision_result = $ldapProcessor
          ->provisionLdapEntry($username, NULL);
        $results['provisionLdapEntry method results']["context = {$sync_trigger_description}"] = $provision_result;
      }
    }
    else {
      if ($direction == self::PROVISION_TO_DRUPAL) {
        $results['provisionDrupalAccount method results']["context = {$sync_trigger_description}"] = 'Not enabled.';
      }
      else {
        $results['provisionLdapEntry method results']["context = {$sync_trigger_description}"] = 'Not enabled.';
      }
    }
  }
  if (function_exists('dpm')) {
    dpm($results);
  }
  else {
    drupal_set_message($this
      ->t('This form will not display results unless the devel module is enabled.'), 'warning');
  }
  $params = [
    'action' => $selected_action,
    'username' => $username,
  ];
  $form_state
    ->setRedirectUrl(Url::fromRoute('ldap_user.test_form', $params));
}