View source
<?php
declare (strict_types=1);
namespace Drupal\ldap_user\Form;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Extension\ModuleHandler;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\ldap_servers\LdapUserAttributesInterface;
use Drupal\ldap_servers\Mapping;
use Drupal\ldap_user\FieldProvider;
use Symfony\Component\DependencyInjection\ContainerInterface;
abstract class LdapUserMappingBaseForm extends ConfigFormBase implements LdapUserAttributesInterface {
protected $moduleHandler;
protected $entityTypeManager;
protected $fieldProvider;
protected $currentConfig;
protected $events;
protected $direction;
protected $server;
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');
}
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'));
}
public function getEditableConfigNames() : array {
return [
'ldap_user.settings',
];
}
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) {
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());
$this
->checkEmptyEvents($processed_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,
]));
}
}
}
protected function loadAvailableMappings(string $direction, string $sid) : array {
$attributes = [];
if ($sid) {
try {
$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;
}
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.'));
}
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;
}
private function sanitizeMachineName(string $string) : string {
return str_replace([
'.',
'[',
']',
], [
'-',
'',
'',
], $string);
}
protected function setSpecificMapping(Mapping $mapping, array $row) : void {
}
protected function getMappingRow(Mapping $mapping, array $target_fields, int $row_id) : array {
return [];
}
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);
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;
}
public function mappingsAjaxCallback(array &$form, FormStateInterface $form_state) : array {
return $form['mappings'];
}
public function mappingsAddAnother(array &$form, FormStateInterface $form_state) : void {
$form_state
->set('row_count', $form_state
->get('row_count') + 1);
$form_state
->setRebuild();
}
}