You are here

private function LdapUserAdminForm::getServerMappingFields in Lightweight Directory Access Protocol (LDAP) 8.3

Return the server mappings for the fields.

Parameters

string $direction: The provisioning direction.

Return value

array|bool Returns the mappings.

1 call to LdapUserAdminForm::getServerMappingFields()
LdapUserAdminForm::buildForm in ldap_user/src/Form/LdapUserAdminForm.php
Form constructor.

File

ldap_user/src/Form/LdapUserAdminForm.php, line 756

Class

LdapUserAdminForm
Provides the form to configure user configuration and field mapping.

Namespace

Drupal\ldap_user\Form

Code

private function getServerMappingFields($direction) {
  if ($direction == self::PROVISION_TO_NONE) {
    return FALSE;
  }
  $rows = [];
  $text = $direction == self::PROVISION_TO_DRUPAL ? 'target' : 'source';
  $userAttributeOptions = [
    '0' => $this
      ->t('Select') . ' ' . $text,
  ];
  $syncMappingsHelper = new SyncMappingHelper();
  $syncMappings = $syncMappingsHelper
    ->getAllSyncMappings();
  if (!empty($syncMappings[$direction])) {
    foreach ($syncMappings[$direction] as $target_id => $mapping) {
      if (!isset($mapping['name']) || isset($mapping['exclude_from_mapping_ui']) && $mapping['exclude_from_mapping_ui']) {
        continue;
      }
      if (isset($mapping['configurable_to_drupal']) && $mapping['configurable_to_drupal'] && $direction == self::PROVISION_TO_DRUPAL || isset($mapping['configurable_to_ldap']) && $mapping['configurable_to_ldap'] && $direction == self::PROVISION_TO_LDAP) {
        $userAttributeOptions[$target_id] = $mapping['name'];
      }
    }
  }
  if ($direction != self::PROVISION_TO_DRUPAL) {
    $userAttributeOptions['user_tokens'] = '-- user tokens --';
  }
  $row = 0;

  // 1. non configurable mapping rows.
  foreach ($syncMappings[$direction] as $target_id => $mapping) {
    $rowId = $this
      ->sanitizeMachineName($target_id);
    if (isset($mapping['exclude_from_mapping_ui']) && $mapping['exclude_from_mapping_ui']) {
      continue;
    }

    // Is configurable by ldap_user module (not direction to ldap_user)
    if (!$this
      ->isMappingConfigurable($mapping, 'ldap_user') && ($mapping['direction'] == $direction || $mapping['direction'] == self::PROVISION_TO_ALL)) {
      $rows[$rowId] = $this
        ->getSyncFormRow('nonconfigurable', $direction, $mapping, $userAttributeOptions, $rowId);
      $row++;
    }
  }
  $config = $this
    ->config('ldap_user.settings');

  // 2. existing configurable mappings rows.
  if (!empty($config
    ->get('ldapUserSyncMappings')[$direction])) {

    // Key could be LDAP attribute name or user attribute name.
    foreach ($config
      ->get('ldapUserSyncMappings')[$direction] as $mapping) {
      if ($direction == self::PROVISION_TO_DRUPAL) {
        $mapping_key = $mapping['user_attr'];
      }
      else {
        $mapping_key = $mapping['ldap_attr'];
      }
      if (isset($mapping['enabled']) && $mapping['enabled'] && $this
        ->isMappingConfigurable($syncMappings[$direction][$mapping_key], 'ldap_user')) {
        $rowId = 'row-' . $row;
        $rows[$rowId] = $this
          ->getSyncFormRow('update', $direction, $mapping, $userAttributeOptions, $rowId);
        $row++;
      }
    }
  }

  // 3. leave 4 rows for adding more mappings.
  for ($i = 0; $i < 4; $i++) {
    $rowId = 'custom-' . $i;
    $rows[$rowId] = $this
      ->getSyncFormRow('add', $direction, [], $userAttributeOptions, $rowId);
    $row++;
  }
  return $rows;
}