You are here

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

Same name and namespace in other branches
  1. 8.3 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 166

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) : void {
  $username = $form_state
    ->getValue([
    'testing_drupal_username',
  ]);
  $selected_action = $form_state
    ->getValue([
    'action',
  ]);
  $config = $this
    ->configFactory()
    ->get('ldap_user.settings')
    ->get();
  $user_ldap_entry = FALSE;
  if ($config['drupalAcctProvisionServer']) {
    $this->ldapUserManager
      ->setServerById($config['drupalAcctProvisionServer']);
    $user_ldap_entry = $this->ldapUserManager
      ->getUserDataByIdentifier($username);
  }
  if ($config['ldapEntryProvisionServer'] && !$user_ldap_entry) {
    $this->ldapUserManager
      ->setServerById($config['ldapEntryProvisionServer']);
    $user_ldap_entry = $this->ldapUserManager
      ->getUserDataByIdentifier($username);
  }
  $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'] = $this->externalAuth
      ->get($existingAccount
      ->id(), 'ldap_user');
  }
  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) {
        $this->drupalUserProcessor
          ->createDrupalUserFromLdapEntry($account);
        $results['createDrupalUserFromLdapEntry method results']["context = {$sync_trigger_description}"]['proposed'] = $account;
      }
      else {

        // @FIXME
        // This is not testing all supported event,
        // only the new user created event.
        // The form needs to be restructured in general for those!
        // $event = new LdapNewUserCreatedEvent($account);
        // /** @var EventDispatcher $dispatcher */
        // $dispatcher = \Drupal::service('event_dispatcher');
        // $dispatcher->dispatch($event, LdapNewUserCreatedEvent::EVENT_NAME);
        // @FIXME.
        $results['provisionLdapEntry method results']["context = {$sync_trigger_description}"] = 'Test not ported';
      }
    }
    else {
      if ($direction === self::PROVISION_TO_DRUPAL) {
        $results['createDrupalUserFromLdapEntry method results']["context = {$sync_trigger_description}"] = 'Not enabled.';
      }
      else {
        $results['provisionLdapEntry method results']["context = {$sync_trigger_description}"] = 'Not enabled.';
      }
    }
  }
  if (\function_exists('kint')) {

    // @phpcs:ignore
    kint($results);
  }
  else {
    $this
      ->messenger()
      ->addWarning($this
      ->t('This form will not display results unless the devel and kint module is enabled.'));
  }
  $params = [
    'action' => $selected_action,
    'username' => $username,
  ];
  $form_state
    ->setRedirectUrl(Url::fromRoute('ldap_user.test_form', $params));
}