Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/Drupal/Commands/sql/SanitizeUserFieldsCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public function getEntityFieldManager()
public function sanitize($result, CommandData $commandData): void
{
$options = $commandData->options();
$restricted_length_fields = explode(',', $options['restricted-length-fields']);
$conn = $this->getDatabase();
$field_definitions = $this->getEntityFieldManager()->getFieldDefinitions('user', 'user');
$field_storage = $this->getEntityFieldManager()->getFieldStorageDefinitions('user');
Expand All @@ -68,6 +69,10 @@ public function sanitize($result, CommandData $commandData): void
$field_type_class = \Drupal::service('plugin.manager.field.field_type')->getPluginClass($def->getType());
$supported_field_types = ['email', 'string', 'string_long', 'telephone', 'text', 'text_long', 'text_with_summary'];
if (in_array($def->getType(), $supported_field_types)) {
if (in_array($def->getName(), $restricted_length_fields)) {
// Pretend this field is a short (single word) field.
$def->setSetting('max_length', 15);
}
$value_array = $field_type_class::generateSampleValue($def);
$value = $value_array['value'];
}
Expand Down Expand Up @@ -123,11 +128,16 @@ public function sanitize($result, CommandData $commandData): void
public function messages(&$messages, InputInterface $input): void
{
$messages[] = dt('Sanitize text fields associated with users.');
if ($restricted_length_fields = $input->getOption('restricted-length-fields')) {
$restricted_length_fields = explode(',', $restricted_length_fields);
$messages[] = dt('Treat ' . implode(', ', $restricted_length_fields) . ' as single word text fields.');
}
}

#[CLI\Hook(type: HookManager::OPTION_HOOK, target: SanitizeCommands::SANITIZE)]
#[CLI\Option(name: 'allowlist-fields', description: 'A comma delimited list of fields exempt from sanitization.')]
public function options($options = ['allowlist-fields' => '']): void
#[CLI\Option(name: 'restricted-length-fields', description: 'A comma delimited list of fields which should be considered restricted in length.')]
public function options($options = ['allowlist-fields' => '', 'restricted-length-fields' => '']): void
{
}
}