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

ISSUE-1: Started implementing UI to configure replacements #3

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
20 changes: 12 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,23 @@ be GDPR compliant YO!

## Configuration

This module can be configured by editing the `gdpr_dumper.settings.yml` [file](https://github.com/robiningelbrecht/gdpr-dumper/blob/master/config/install/gdpr_dumper.settings.yml).
This module can be configured by navigating to `admin/config/development/gdpr-dumper`.
On this page you can configure the sanitization and anonymization
of every column of every table in your database.

## Events

The module dispatches one event:
* `GdprDumperEvents::GDPR_REPLACEMENTS`

This allows developers to alter the replacements through event subscribers on run-time.
[machbarmacher/gdpr-dump](https://github.com/machbarmacher/gdpr-dump) contains more info about
the **gdpr-expressions** and **gdpr-replacement** options.
the **gdpr-replacement** options.

The provided yml file expects the same structure as explained in the readme above.

## Events
## TODO

The module dispatches two events:
* `GdprDumperEvents::GDPR_EXPRESSIONS`
* `GdprDumperEvents::GDPR_REPLACEMENTS`

This allows developers to alter the expressions and replacements through event subscribers on run-time
* Provide a way to allow to export the structure of a table without the data.

Happy GDPR'ing!
17 changes: 2 additions & 15 deletions config/install/gdpr_dumper.settings.yml
Original file line number Diff line number Diff line change
@@ -1,22 +1,9 @@
gdpr_expressions:
users_field_data:
init: 'uid'
empty_tables: {}
gdpr_replacements:
users_field_data:
name:
formatter: 'username'
mail:
formatter: 'email'
pass:
formatter: 'clear'
drivers:
mysql:
dump_command: 'mysqldump'
oracle:
dump_command: 'mysqldump'
pqsql:
dump_command: 'pg_dump'
sqlite:
dump_command: 'dump'
sqlsrv:
dump_command: 'mysqldump'
formatter: 'clear'
8 changes: 8 additions & 0 deletions gdpr_dumper.libraries.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
settings-form:
version: 1.x
js:
js/gdpr-dumper.js: {}
dependencies:
- core/jquery
- core/drupal.form

5 changes: 5 additions & 0 deletions gdpr_dumper.links.menu.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
gdpr_dumper.settings:
title: 'GDPR dumper'
description: 'Configure GDPR dumper drush command'
parent: system.admin_config_development
route_name: gdpr_dumper.settings
3 changes: 3 additions & 0 deletions gdpr_dumper.permissions.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
administer gdpr dumper:
title: 'Administer gdpr dumper'
description: 'Allow to configure the gdpr dumper drush command'
7 changes: 7 additions & 0 deletions gdpr_dumper.routing.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
gdpr_dumper.settings:
path: '/admin/config/development/gdpr-dumper'
defaults:
_form: '\Drupal\gdpr_dumper\Form\GdprDumperSettingsForm'
_title: 'GDPR dumper'
requirements:
_permission: 'administer gdpr dumper'
4 changes: 4 additions & 0 deletions gdpr_dumper.services.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
services:
gdpr_dumper.database_manager:
class: Drupal\gdpr_dumper\Manager\DatabaseManager
arguments: ['@database']
13 changes: 13 additions & 0 deletions js/gdpr-dumper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
(function($, Drupal) {

Drupal.behaviors.gdprDumperSummary = {
attach: function (context, settings) {
// Display the action in the vertical tab summary.
$(context).find('details[data-table-summary]').drupalSetSummary(function(context) {
return Drupal.checkPlain($(context).data('table-summary'));
});

}
}

})(jQuery, Drupal);
9 changes: 0 additions & 9 deletions src/Event/GdprDumperEvents.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,6 @@
*/
final class GdprDumperEvents {

/**
* Name of the event fired building the GDPR expressions.
*
* @Event
*
* @see \Drupal\gdpr_dumper\Event\GdprExpressionsEvent
*/
const GDPR_EXPRESSIONS = 'gdpr_dumper.expressions';

/**
* Name of the event fired building the GDPR replacements.
*
Expand Down
43 changes: 0 additions & 43 deletions src/Event/GdprExpressionsEvent.php

This file was deleted.

229 changes: 229 additions & 0 deletions src/Form/GdprDumperSettingsForm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,229 @@
<?php

namespace Drupal\gdpr_dumper\Form;

use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Database\Connection;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\gdpr_dumper\GdprDumperEnums;
use Drupal\gdpr_dumper\Manager\DatabaseManager;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
* Class GdprDumperSettingsForm
* @package Drupal\gdpr_dumper\Form
*/
class GdprDumperSettingsForm extends ConfigFormBase {

protected $settings;
protected $connection;
protected $databaseManager;

/**
* GdprDumperSettingsForm constructor.
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* @param \Drupal\Core\Database\Connection $connection
* @param \Drupal\gdpr_dumper\Manager\DatabaseManager $database_manager
*/
public function __construct(ConfigFactoryInterface $config_factory, Connection $connection, DatabaseManager $database_manager) {
parent::__construct($config_factory);
$this->connection = $connection;
$this->databaseManager = $database_manager;
$this->settings = $this->config('gdpr_dumper.settings');
}

/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('config.factory'),
$container->get('database'),
$container->get('gdpr_dumper.database_manager')
);
}

/**
* {@inheritdoc}
*/
public function getFormId() {
return 'gdpr_dumper_settings_form';
}

/**
* {@inheritdoc}
*/
protected function getEditableConfigNames() {
return ['gdpr_dumper.settings'];
}

/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$replacements = $this->settings->get('gdpr_replacements');
$empty_tables = $this->settings->get('empty_tables');
// Add the empty table options to the replacement array, if they don't exist already.
// We need this because if only the "Empty table" option is checked,
// the table won't be available in the replacements array.
foreach (array_filter($empty_tables) as $empty_table => $value) {
if (!isset($replacements[$empty_table])) {
$replacements[$empty_table] = [];
}
}


$database_tables = $this->databaseManager->getTableColumns();
$db_schema = $this->connection->schema();
$schema_handles_db_comments = \is_callable([$db_schema, 'getComment']);

$form['intro'] = [
'#type' => 'item',
'#title' => $this->t('Manage tables and columns that contain sensitive data'),
];

$tables_to_add = array_diff(array_keys($database_tables), array_keys($replacements));

$form['table'] = [
'#type' => 'select',
'#title' => $this->t('Select table'),
'#title_display' => 'invisible',
'#options' => array_combine($tables_to_add, $tables_to_add),
'#empty_value' => '',
'#empty_option' => $this->t('- Select -'),
];

$form['add_table'] = [
'actions' => [
'#type' => 'actions',
'submit' => [
'#type' => 'submit',
'#value' => $this->t('Add table'),
'#submit' => [
[$this, 'submitAddTable']
],
],
],
];

$form['advanced'] = [
'#type' => 'vertical_tabs',
];

$form['replacements'] = [
'#tree' => TRUE,
'#attached' => [
'library' => ['gdpr_dumper/settings-form']
],
];

foreach ($replacements as $table => $columns) {
if (isset($database_tables[$table])) {
$table_summary = $schema_handles_db_comments ? $db_schema->getComment($table) : '-';
$form['replacements'][$table] = [
'#type' => 'details',
'#title' => $table,
'#group' => 'advanced',
'#attributes' => [
'data-table-summary' => $table_summary,
],
];

$form['replacements'][$table]['columns'] = [
'#type' => 'table',
'#caption' => $table_summary,
'#header' => [
['data' => $this->t('Field')],
['data' => $this->t('Type')],
['data' => $this->t('Description')],
['data' => $this->t('Apply anonymization')],
],
];

foreach ($database_tables[$table] as $column_name => $column_properties) {
$form['replacements'][$table]['columns'][$column_name]['field'] = [
'#markup' => '<strong>' . $column_properties['COLUMN_NAME'] . '</strong>',
];
$form['replacements'][$table]['columns'][$column_name]['data_type'] = [
'#markup' => '<strong>' . $column_properties['DATA_TYPE'] . '</strong>',
];
$form['replacements'][$table]['columns'][$column_name]['comment'] = [
'#markup' => '<strong>' . (empty($column_properties['COLUMN_COMMENT']) ? '-' : $column_properties['COLUMN_COMMENT']) . '</strong>',
];
$form['replacements'][$table]['columns'][$column_name]['anonymization'] = [
'#type' => 'select',
'#title' => $this->t('Apply anonymization'),
'#title_display' => 'invisible',
'#options' => GdprDumperEnums::fakerFormatters(),
'#empty_value' => '',
'#empty_option' => $this->t('- No -'),
'#required' => FALSE,
'#default_value' => isset($replacements[$table][$column_name]['formatter']) ? $replacements[$table][$column_name]['formatter'] : FALSE,
];
}

$form['replacements'][$table]['empty'] = [
'#type' => 'checkbox',
'#title' => $this->t('Empty this table'),
'#button_type' => 'secondary',
'#default_value' => isset($empty_tables[$table]) ? $empty_tables[$table] : FALSE,
];
}
}

return parent::buildForm($form, $form_state);
}

/**
* Submit callback to add a table to the list.
*
* @param array $form
* @param \Drupal\Core\Form\FormStateInterface $form_state
*/
public function submitAddTable(array &$form, FormStateInterface $form_state) {
if ($table = $form_state->getValue('table')) {
$replacements = $this->settings->get('gdpr_replacements');

if (!isset($replacements[$table])) {
$replacements[$table] = [];
}

// Order tables alphabetically before saving.
ksort($replacements);

// Update config.
$this->settings->set('gdpr_replacements', $replacements)->save();
$this->messenger()
->addStatus($this->t('The table has been added. You can configure it by selecting the corresponding tab.'));
}
}

/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$settings = [
'gdpr_replacements' => [],
'empty_tables' => [],
];

$replacements = $form_state->getValue('replacements');
// Format the replacement to a suitable config array.
foreach ($replacements as $table_name => $properties) {
foreach ($properties['columns'] as $column_name => $column) {
if (!empty($column['anonymization'])) {
$settings['gdpr_replacements'][$table_name][$column_name]['formatter'] = $column['anonymization'];
}
};
$settings['empty_tables'][$table_name] = $properties['empty'];
}

// Save settings.
$this->settings->set('gdpr_replacements', $settings['gdpr_replacements'])
->set('empty_tables', $settings['empty_tables'])->save();

parent::submitForm($form, $form_state);
}

}
Loading