Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sanitize user names on sql-sanitize. #6057

Open
wants to merge 2 commits into
base: 13.x
Choose a base branch
from
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
57 changes: 55 additions & 2 deletions src/Commands/sql/sanitize/SanitizeUserTableCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
use Drupal\Core\Database\Query\SelectInterface;
use Consolidation\AnnotatedCommand\CommandData;
use Consolidation\AnnotatedCommand\Hooks\HookManager;
use Drupal\Core\Database\Query\Update;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\Sql\SqlEntityStorageInterface;
use Drupal\Core\Password\PasswordInterface;
use Drush\Attributes as CLI;
use Drush\Commands\AutowireTrait;
Expand All @@ -27,7 +30,8 @@ final class SanitizeUserTableCommands extends DrushCommands implements SanitizeP
public function __construct(
protected Connection $database,
protected PasswordInterface $passwordHasher,
protected EntityTypeManagerInterface $entityTypeManager
protected EntityTypeManagerInterface $entityTypeManager,
protected EntityFieldManagerInterface $entityFieldManager
) {
parent::__construct();
}
Expand Down Expand Up @@ -80,6 +84,20 @@ public function sanitize($result, CommandData $commandData): void
$messages[] = dt('User emails sanitized.');
}

// Sanitize username.
if ($this->isEnabled($options['sanitize-username'])) {
[$name_table, $name_column] = $this->getFieldTableDetails('user', 'name');
[$uid_table, $uid_column] = $this->getFieldTableDetails('user', 'uid');
assert($uid_table === $name_table);
Comment on lines +89 to +91

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW to my mind this clashes a little with the existing style where the table and column names have been hardcoded. Not sure if that means it's better to stick with the current style or update the others personally (:

If we do keep it, I think we need to catch any exceptions, we don't want to explode the sanitize command as a whole.


// Updates usernames to the pattern user_%uid.
$query
->condition($uid_column, 0, '>')

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That condition's already added further up.

Suggested change
->condition($uid_column, 0, '>')

->expression($name_column, "CONCAT('user_', $uid_column)");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at the email code, it appears we'd need a different format for different DB drivers I think?


$messages[] = dt("Usernames sanitized.");
}

if (!empty($options['ignored-roles'])) {
$roles = explode(',', $options['ignored-roles']);
/** @var SelectInterface $roles_query */
Expand Down Expand Up @@ -108,8 +126,9 @@ public function sanitize($result, CommandData $commandData): void
#[CLI\Hook(type: HookManager::OPTION_HOOK, target: SanitizeCommands::SANITIZE)]
#[CLI\Option(name: 'sanitize-email', description: 'The pattern for test email addresses in the sanitization operation, or <info>no</info> to keep email addresses unchanged. May contain replacement patterns <info>%uid</info>, <info>%mail</info> or <info>%name</info>.')]
#[CLI\Option(name: 'sanitize-password', description: 'By default, passwords are randomized. Specify <info>no</info> to disable that. Specify any other value to set all passwords to that value.')]
#[CLI\Option(name: 'sanitize-username', description: 'Sanitizes usernames replacing the originals with user_UID.')]
#[CLI\Option(name: 'ignored-roles', description: 'A comma delimited list of roles. Users with at least one of the roles will be exempt from sanitization.')]
public function options($options = ['sanitize-email' => 'user+%[email protected]', 'sanitize-password' => null, 'ignored-roles' => null]): void
public function options($options = ['sanitize-email' => 'user+%[email protected]', 'sanitize-password' => null, 'sanitize-username' => 'no', 'ignored-roles' => null]): void
{
}

Expand All @@ -123,11 +142,45 @@ public function messages(&$messages, InputInterface $input): void
if ($this->isEnabled($options['sanitize-email'])) {
$messages[] = dt('Sanitize user emails.');
}
if ($this->isEnabled($options['sanitize-username'])) {
$messages[] = dt('Sanitize usernames.');
}
if (in_array('ignored-roles', $options)) {
$messages[] = dt('Preserve user emails and passwords for the specified roles.');
}
}

/**
* Gets database details for a given field.
*
* It returns the field table name and main property column name.
*
* @param string $entity_type_id
* The entity type ID the field's attached to.
* @param string $field_name
* The field name.
*
* @return array
* An indexed array, containing:
* - the table name;
* - the column name.
*/
protected function getFieldTableDetails(string $entity_type_id, string $field_name): array
{
$storage = $this->entityTypeManager->getStorage($entity_type_id);
if (!$storage instanceof SqlEntityStorageInterface) {
$context = ['!entity_type_id' => $entity_type_id];
throw new \Exception(dt("Unable to get !entity_type_id table mapping details, its storage doesn't implement \Drupal\Core\Entity\Sql\SqlEntityStorageInterface.", $context));
}
$mapping = $storage->getTableMapping();
$table = $mapping->getFieldTableName($field_name);
$columns = $mapping->getColumnNames($field_name);
$definitions = $this->entityFieldManager->getFieldStorageDefinitions($entity_type_id);
$main_property = $definitions[$field_name]->getMainPropertyName();

return [$table, $columns[$main_property]];
}

/**
* Test an option value to see if it is disabled.
*/
Expand Down