You are here

abstract class LdapUserMappingBaseForm in Lightweight Directory Access Protocol (LDAP) 8.4

Provides the form to configure user configuration and field mapping.

Hierarchy

Expanded class hierarchy of LdapUserMappingBaseForm

File

ldap_user/src/Form/LdapUserMappingBaseForm.php, line 20

Namespace

Drupal\ldap_user\Form
View source
abstract class LdapUserMappingBaseForm extends ConfigFormBase implements LdapUserAttributesInterface {

  /**
   * Module handler.
   *
   * @var \Drupal\Core\Extension\ModuleHandler
   */
  protected $moduleHandler;

  /**
   * Entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * Field provider.
   *
   * @var \Drupal\ldap_user\FieldProvider
   */
  protected $fieldProvider;

  /**
   * Current config.
   *
   * @var \Drupal\Core\Config\ImmutableConfig
   */
  protected $currentConfig;

  /**
   * Events.
   *
   * @var array
   */
  protected $events;

  /**
   * Direction.
   *
   * @var string
   */
  protected $direction;

  /**
   * Server.
   *
   * @var string
   */
  protected $server;

  /**
   * {@inheritdoc}
   */
  public function __construct(ConfigFactoryInterface $config_factory, ModuleHandler $module_handler, EntityTypeManagerInterface $entity_type_manager, FieldProvider $field_provider) {
    parent::__construct($config_factory);
    $this->moduleHandler = $module_handler;
    $this->entityTypeManager = $entity_type_manager;
    $this->fieldProvider = $field_provider;
    $this->currentConfig = $this
      ->config('ldap_user.settings');
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('config.factory'), $container
      ->get('module_handler'), $container
      ->get('entity_type.manager'), $container
      ->get('ldap_user.field_provider'));
  }

  /**
   * {@inheritdoc}
   */
  public function getEditableConfigNames() : array {
    return [
      'ldap_user.settings',
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) : void {
    $values = $form_state
      ->getValues();
    foreach ($values['mappings'] as $key => $mapping) {
      if (isset($mapping['configured_mapping']) && $mapping['configured_mapping'] == 1) {

        // Check that the source is not empty for the selected field to sync
        // to Drupal.
        if (!empty($mapping['source']) && empty($mapping['target'])) {
          $formElement = $form['mappings'][$key];
          $form_state
            ->setError($formElement, $this
            ->t('Missing attribute'));
        }
      }
    }
    $processed_mappings = $this
      ->syncMappingsFromForm($form_state
      ->getValues());

    // Notify the user if no actual synchronization event is active for a field.
    $this
      ->checkEmptyEvents($processed_mappings);
  }

  /**
   * Warn about fields without associated events.
   *
   * @param array $mappings
   *   Field mappings.
   */
  private function checkEmptyEvents(array $mappings) : void {
    foreach ($mappings as $key => $mapping) {
      if (empty($mapping['prov_events'])) {
        $this
          ->messenger()
          ->addWarning($this
          ->t('No synchronization events checked in %item. This field will not be synchronized until some are checked.', [
          '%item' => $key,
        ]));
      }
    }
  }

  /**
   * Derive synchronization mappings from configuration.
   *
   * @param string $direction
   *   Direction.
   * @param string $sid
   *   Server ID.
   *
   * @return array
   *   Mappings.
   */
  protected function loadAvailableMappings(string $direction, string $sid) : array {
    $attributes = [];
    if ($sid) {
      try {

        /** @var \Drupal\ldap_servers\Entity\Server $ldap_server */
        $ldap_server = $this->entityTypeManager
          ->getStorage('ldap_server')
          ->load($sid);
        $attributes = $this->fieldProvider
          ->loadAttributes($direction, $ldap_server);
      } catch (\Exception $e) {
        $this
          ->logger('ldap_user')
          ->error('Missing server');
      }
    }
    $params = [
      $direction,
      $sid,
    ];
    $this->moduleHandler
      ->alter('ldap_user_attributes', $attributes, $params);
    return $attributes;
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) : void {
    $mappings = $this->currentConfig
      ->get('ldapUserSyncMappings');
    $mappings[$this->direction] = $this
      ->syncMappingsFromForm($form_state
      ->getValues());
    $this->currentConfig
      ->set('ldapUserSyncMappings', $mappings)
      ->save();
    $this
      ->messenger()
      ->addMessage($this
      ->t('User synchronization configuration updated.'));
  }

  /**
   * Extract sync mappings array from mapping table in admin form.
   *
   * @param array $values
   *   Form values.
   *
   * @return array
   *   Returns the relevant mappings.
   */
  protected function syncMappingsFromForm(array $values) : array {
    $mappings = [];
    foreach ($values['mappings'] as $row) {
      if (isset($row['source']) && !empty($row['source']) && $row['configured_mapping'] == TRUE && $row['delete'] == FALSE) {
        $events = [];
        foreach ($this->events as $event) {
          if ($row[$event] == 1) {
            $events[] = $event;
          }
        }
        $mapping = new Mapping($row['target'], (string) $this
          ->t('User defined mapping for @field', [
          '@field' => $row['target'],
        ]), TRUE, TRUE, $events, 'ldap_user', 'ldap_user');
        $mapping
          ->convertBinary((bool) $row['convert']);
        if (!empty($row['user_tokens'])) {
          $mapping
            ->setUserTokens(trim($row['user_tokens']));
        }
        $this
          ->setSpecificMapping($mapping, $row);
        $mappings[$this
          ->sanitizeMachineName($row['target'])] = $mapping
          ->serialize();
      }
    }
    return $mappings;
  }

  /**
   * Sanitize machine name.
   *
   * @param string $string
   *   Field name.
   *
   * @return string
   *   Machine name.
   */
  private function sanitizeMachineName(string $string) : string {

    // Replace periods & square brackets.
    return str_replace([
      '.',
      '[',
      ']',
    ], [
      '-',
      '',
      '',
    ], $string);
  }

  /**
   * Set specific mapping.
   *
   * @param \Drupal\ldap_servers\Mapping $mapping
   *   Mapping.
   * @param array $row
   *   Row.
   */
  protected function setSpecificMapping(Mapping $mapping, array $row) : void {

    // Sub form does its variant here.
  }

  /**
   * Get mapping form row to LDAP user provisioning mapping admin form table.
   *
   * @param \Drupal\ldap_servers\Mapping $mapping
   *   Is current setting for updates or non-configurable items.
   * @param array $target_fields
   *   Attributes of Drupal user target options.
   * @param int $row_id
   *   Only needed for LDAP.
   *
   * @return array
   *   Row.
   */
  protected function getMappingRow(Mapping $mapping, array $target_fields, int $row_id) : array {

    // Sub form does its variant here.
    return [];
  }

  /**
   * Return the server mappings for the fields.
   *
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   Form State.
   *
   * @return array
   *   Returns the mappings
   */
  protected function getServerMappingFields(FormStateInterface $form_state) : array {
    $rows = [];
    $user_attribute_options = [
      '0' => $this
        ->t('Select option'),
    ];
    if (!$this->server) {
      $this
        ->messenger()
        ->addWarning('You do not have a server configured for mapping.');
      return $rows;
    }
    $available_mappings = $this
      ->loadAvailableMappings($this->direction, $this->server);

    // Potential mappings (i.e. fields provided for the user entity) are
    // aggregated so that they can be input for user-defined mappings.
    // The difference being that these available mappings are not *enabled*.
    // Ideally, those would be split into something like a MappingProposal and
    // a MappingRule.

    /** @var \Drupal\ldap_servers\Mapping $mapping */
    foreach ($available_mappings as $target_id => $mapping) {
      if (!empty($mapping
        ->getId()) && $mapping
        ->isConfigurable()) {
        $user_attribute_options[$target_id] = $mapping
          ->getLabel();
      }
    }
    if ($this->direction === self::PROVISION_TO_LDAP) {
      $user_attribute_options['user_tokens'] = '-- user tokens --';
    }
    $index = 0;
    foreach ($available_mappings as $mapping) {
      if ($mapping
        ->isEnabled()) {
        $rows[$index] = $this
          ->getMappingRow($mapping, $user_attribute_options, $index);
        $index++;
      }
    }
    if (empty($form_state
      ->get('row_count'))) {
      $form_state
        ->set('row_count', $index + 1);
    }
    for ($i = $index; $i < $form_state
      ->get('row_count'); $i++) {
      $empty_mapping = new Mapping('', '', TRUE, TRUE, [], 'ldap_user', 'ldap_user');
      $rows[$i] = $this
        ->getMappingRow($empty_mapping, $user_attribute_options, $i);
    }
    return $rows;
  }

  /**
   * Ajax Callback for the form.
   *
   * @param array $form
   *   The form being passed in.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The form state.
   *
   * @return array
   *   The form element we are changing via ajax
   */
  public function mappingsAjaxCallback(array &$form, FormStateInterface $form_state) : array {
    return $form['mappings'];
  }

  /**
   * Functionality for our ajax callback.
   *
   * @param array $form
   *   The form being passed in.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The form state, passed by reference so that we can modify.
   */
  public function mappingsAddAnother(array &$form, FormStateInterface $form_state) : void {
    $form_state
      ->set('row_count', $form_state
      ->get('row_count') + 1);
    $form_state
      ->setRebuild();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigFormBase::buildForm public function Form constructor. Overrides FormInterface::buildForm 26
ConfigFormBaseTrait::config protected function Retrieves a configuration object.
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
FormBase::$configFactory protected property The config factory. 1
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::configFactory protected function Gets the config factory for this form. 1
FormBase::container private function Returns the service container.
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
FormInterface::getFormId public function Returns a unique string identifying the form. 236
LdapUserAttributesInterface::ACCOUNT_CREATION_LDAP_BEHAVIOUR public constant Event config.
LdapUserAttributesInterface::ACCOUNT_CREATION_USER_SETTINGS_FOR_LDAP public constant Config.
LdapUserAttributesInterface::EVENT_CREATE_DRUPAL_USER public constant Event config.
LdapUserAttributesInterface::EVENT_CREATE_LDAP_ENTRY public constant Event config.
LdapUserAttributesInterface::EVENT_LDAP_ASSOCIATE_DRUPAL_USER public constant Event config.
LdapUserAttributesInterface::EVENT_SYNC_TO_DRUPAL_USER public constant Event config.
LdapUserAttributesInterface::EVENT_SYNC_TO_LDAP_ENTRY public constant Event config.
LdapUserAttributesInterface::MANUAL_ACCOUNT_CONFLICT_LDAP_ASSOCIATE public constant Config.
LdapUserAttributesInterface::MANUAL_ACCOUNT_CONFLICT_NO_LDAP_ASSOCIATE public constant Config.
LdapUserAttributesInterface::MANUAL_ACCOUNT_CONFLICT_REJECT public constant Config.
LdapUserAttributesInterface::MANUAL_ACCOUNT_CONFLICT_SHOW_OPTION_ON_FORM public constant Config.
LdapUserAttributesInterface::PROVISION_DRUPAL_USER_ON_USER_AUTHENTICATION public constant Provision config.
LdapUserAttributesInterface::PROVISION_DRUPAL_USER_ON_USER_ON_MANUAL_CREATION public constant Provision config.
LdapUserAttributesInterface::PROVISION_DRUPAL_USER_ON_USER_UPDATE_CREATE public constant Provision config.
LdapUserAttributesInterface::PROVISION_LDAP_ENTRY_ON_USER_ON_USER_AUTHENTICATION public constant Provision config.
LdapUserAttributesInterface::PROVISION_LDAP_ENTRY_ON_USER_ON_USER_DELETE public constant Provision config.
LdapUserAttributesInterface::PROVISION_LDAP_ENTRY_ON_USER_ON_USER_UPDATE_CREATE public constant Provision config.
LdapUserAttributesInterface::PROVISION_TO_ALL constant Provision config.
LdapUserAttributesInterface::PROVISION_TO_DRUPAL public constant Provision config.
LdapUserAttributesInterface::PROVISION_TO_LDAP public constant Provision config.
LdapUserAttributesInterface::PROVISION_TO_NONE public constant Provision config.
LdapUserAttributesInterface::USER_CONFLICT_ATTEMPT_RESOLVE public constant Config.
LdapUserAttributesInterface::USER_CONFLICT_LOG public constant Config.
LdapUserMappingBaseForm::$currentConfig protected property Current config.
LdapUserMappingBaseForm::$direction protected property Direction. 2
LdapUserMappingBaseForm::$entityTypeManager protected property Entity type manager.
LdapUserMappingBaseForm::$events protected property Events. 2
LdapUserMappingBaseForm::$fieldProvider protected property Field provider.
LdapUserMappingBaseForm::$moduleHandler protected property Module handler.
LdapUserMappingBaseForm::$server protected property Server.
LdapUserMappingBaseForm::checkEmptyEvents private function Warn about fields without associated events.
LdapUserMappingBaseForm::create public static function Instantiates a new instance of this class. Overrides ConfigFormBase::create
LdapUserMappingBaseForm::getEditableConfigNames public function Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait::getEditableConfigNames
LdapUserMappingBaseForm::getMappingRow protected function Get mapping form row to LDAP user provisioning mapping admin form table. 2
LdapUserMappingBaseForm::getServerMappingFields protected function Return the server mappings for the fields.
LdapUserMappingBaseForm::loadAvailableMappings protected function Derive synchronization mappings from configuration.
LdapUserMappingBaseForm::mappingsAddAnother public function Functionality for our ajax callback.
LdapUserMappingBaseForm::mappingsAjaxCallback public function Ajax Callback for the form.
LdapUserMappingBaseForm::sanitizeMachineName private function Sanitize machine name.
LdapUserMappingBaseForm::setSpecificMapping protected function Set specific mapping. 2
LdapUserMappingBaseForm::submitForm public function Form submission handler. Overrides ConfigFormBase::submitForm
LdapUserMappingBaseForm::syncMappingsFromForm protected function Extract sync mappings array from mapping table in admin form.
LdapUserMappingBaseForm::validateForm public function Form validation handler. Overrides FormBase::validateForm
LdapUserMappingBaseForm::__construct public function Constructs a \Drupal\system\ConfigFormBase object. Overrides ConfigFormBase::__construct 2
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.