Commit e04e2aee authored by João Ventura's avatar João Ventura Committed by Joao Ventura
Browse files

Issue #3273764 by g.weston, jcnventura: Sanitize for TFA user data is not run on drush sql-sanitize

parent 8bb58896
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -13,7 +13,7 @@
    "extra": {
        "drush": {
            "services": {
                "drush.services.yml": "^9"
                "drush.services.yml": "^9 || ^10 || ^11"
            }
        }
    }
+1 −0
Original line number Diff line number Diff line
services:
  tfa.commands:
    class: \Drupal\tfa\Commands\TfaCommands
    arguments: ['@database']
    tags:
      - { name: drush.command }
+30 −3
Original line number Diff line number Diff line
@@ -3,6 +3,7 @@
namespace Drupal\tfa\Commands;

use Consolidation\AnnotatedCommand\CommandData;
use Drupal\Core\Database\Connection;
use Drush\Commands\DrushCommands;
use Drush\Drupal\Commands\sql\SanitizePluginInterface;
use Symfony\Component\Console\Input\InputInterface;
@@ -13,20 +14,46 @@ use Symfony\Component\Console\Input\InputInterface;
class TfaCommands extends DrushCommands implements SanitizePluginInterface {

  /**
   * Database service.
   *
   * @var \Drupal\Core\Database\Connection
   */
  protected $database;

  /**
   * TfaCommands constructor.
   *
   * @param \Drupal\Core\Database\Connection $database
   *   The database service.
   */
  public function __construct(Connection $database) {
    parent::__construct();
    $this->database = $database;
  }

  /**
   * Sanitize recovery codes and user-specific TFA data.
   *
   * @hook post-command sql-sanitize
   *
   * {@inheritdoc}
   */
  public function sanitize($result, CommandData $commandData) {
    // DBTNG does not support expressions in delete queries.
    $sql = "DELETE FROM users_data WHERE LEFT(name, 4) = 'tfa_'";
    \Drupal::service('database')->query($sql);
    $this->database->query($sql);
    $this->logger()->success('Removed recovery codes and other user-specific TFA data.');
  }

  /**
   * Display summary to user before confirmation.
   *
   * @hook on-event sql-sanitize-confirms
   *
   * {@inheritdoc}
   */
  public function messages(&$messages, InputInterface $input) {
    return $messages[] = dt('Remove recovery codes and other user-specific TFA data.');
  public function messages(array &$messages, InputInterface $input) {
    $messages[] = dt('Remove recovery codes and other user-specific TFA data.');
  }

}