Skip to content
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
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
}
],
"require": {
"drupal/checklistapi": "^1.0"
"drupal/checklistapi": "^1.0",
"drupal/views_data_export": "^1.0@alpha"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The rules module is a dependency in the info.yml, but it's not required here.

}
}
10 changes: 10 additions & 0 deletions modules/gdpr_view_export_log/gdpr_view_export_log.info.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
name: General Data Protection Regulation (GDPR) - Log View Exports
description: Logs view exports
core: 8.x
type: module
package: General Data Protection Regulation

dependencies:
- gdpr:gdpr
- views_data_export:views_data_export
- rules:rules
74 changes: 74 additions & 0 deletions modules/gdpr_view_export_log/gdpr_view_export_log.install
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

/**
* @file
* Install file for the GDPR Views Data Export module.
*/

/**
* Implements hook_install().
*/
function gdpr_view_export_log_install() {
$config = \Drupal::service('config.factory')->getEditable('views.settings');
$display_extenders = $config->get('display_extenders') ?: [];
$display_extenders[] = 'gdpr_view_export_logging';
$config->set('display_extenders', $display_extenders);
$config->save();
}

/**
* Implements hook_uninstall().
*/
function gdpr_view_export_log_uninstall() {
$config = \Drupal::service('config.factory')->getEditable('views.settings');
$display_extenders = $config->get('display_extenders') ?: [];
$key = array_search('gdpr_view_export_logging', $display_extenders);
if ($key !== FALSE) {
unset($display_extenders[$key]);
$config->set('display_extenders', $display_extenders);
$config->save();
}
}

/**
* Implements hook_schema().
*
* User IDs are stored in a custom table rather than an entity field.
* This is so that they can be lazily loaded for performance reasons,
* as we don't want to be loading thousands of users when looking up a log.
*/
function gdpr_view_export_log_schema() {
$schema['gdpr_view_export_audit_user_ids'] = [
'description' => 'Stores when Merge Processes are happening on an entity.',
'fields' => [
'id' => [
'type' => 'serial',
'description' => 'Unique Identifier.',
'unsigned' => TRUE,
'not null' => TRUE,
],
'log_id' => [
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'description' => 'The entity id of the corresponding log entry.',
],
'user_id' => [
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'description' => 'The ID of the user.',
],
'name' => [
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => '',
'description' => 'Cache of the user\'s name',
],
],
'primary key' => ['id'],
];

return $schema;
}
138 changes: 138 additions & 0 deletions modules/gdpr_view_export_log/gdpr_view_export_log.module
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
<?php

/**
* @file
* Module file for the GDPR Views Data Export module.
*/

use Drupal\Core\Url;
use Drupal\gdpr_view_export_log\Entity\ExportAudit;
use Drupal\gdpr_view_export_log\Plugin\views\display_extender\GdprExportLogDisplayExtender;
use Drupal\views\ViewExecutable;
use Symfony\Component\HttpFoundation\RedirectResponse;

/**
* Implements hook_toolbar_alter().
*/
function gdpr_view_export_log_toolbar_alter(&$items) {
$user = \Drupal::currentUser();

if ($user->hasPermission('create gdpr export audits')) {
$items['gdpr']['tray']['links']['#links']['exports'] = [
'title' => t('Exports'),
'url' => Url::fromRoute('entity.gdpr_view_export_audit.collection'),
'attributes' => [
'title' => t('Exports'),
],
'weight' => 100,
];
}
}

/**
* Implements hook_views_post_build().
*/
function gdpr_view_export_log_views_post_build(ViewExecutable $view) {
if (GdprExportLogDisplayExtender::isLoggingEnabled($view)) {
// Logging is enabled for this view.
// Instead of just letting it render, redirect to the audit page,
// if we haven't been there already.
$already_audited = \Drupal::request()
->getSession()
->get('gdpr_audit_id') > 0;

if (!$already_audited) {

// @todo: Do not run in preview mode.
$session = \Drupal::request()->getSession();

$session->set('gdpr_export_audit_file', $view->display_handler->options["filename"]);
$session->set('gdpr_export_audit_view', $view->id());
$session->set('gdpr_export_audit_continue', \Drupal::request()
->getRequestUri());

$url = Url::fromRoute('entity.gdpr_view_export_audit.add_form')
->toString();
$response = new RedirectResponse($url);
$response->send();
}
}
}

/**
* Implements hook_views_post_execute().
*/
function gdpr_view_export_log_views_post_execute(ViewExecutable $view) {
// After the view is executed, if there has been an audit entry recorded,
// Modify the audit to store any user IDs included in the output.
if (GdprExportLogDisplayExtender::isLoggingEnabled($view)) {
$session = \Drupal::request()->getSession();
$audit_entry_id = $session->get('gdpr_audit_id');

$value_accessors = [];

if ($audit_entry_id > 0) {
$audit_entry = ExportAudit::load($audit_entry_id);

// fieldDefinition is unfortunately protected.
// Use reflection to get it for now.
// @todo look at not using reflection in future
$r = new ReflectionMethod('Drupal\views\Plugin\views\field\EntityField', 'getFieldDefinition');
$r->setAccessible(TRUE);

// Are any of the fields defined against user?
foreach ($view->field as $field_id => $field) {
// Only process entity fields.
if ($field->definition['class'] == 'Drupal\views\Plugin\views\field\EntityField') {

// If the field is directly defined on the user, log the ID.
if ($field->definition['entity_type'] == 'user') {
$value_accessors[] = function ($row) use ($field) {
return $field->getEntity($row)->id();
};
}

$field_definition = $r->invoke($field);

// If the field is a reference to the user, log the id.
if ($field_definition->getType() == 'entity_reference' && $field_definition->getSetting('target_type') == 'user') {
$value_accessors[] = function ($row) use ($field) {
return $field->getValue($row);
};
}
}
}

$ids = [];

foreach ($view->result as $row) {
foreach ($value_accessors as $accessor) {
$id = $accessor($row);
if (!in_array($id, $ids)) {
$ids[] = $id;
}
}
}

if (count($ids) > 0) {
$audit_entry->save();
// User IDs are stored in a custom table rather than an entity field.
// This is so that they can be lazily loaded for performance reasons,
// as we don't want to be loading thousands of users
// when looking up a log entry.
foreach ($ids as $id) {
// @todo cache the user's name in here too?
\Drupal::database()->insert('gdpr_view_export_audit_user_ids')
->fields([
'log_id' => $audit_entry->id(),
'user_id' => $id,
])->execute();
}
}
}

$session->remove('gdpr_audit_id');

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
create gdpr export audits:
title: 'Create audit entries for exporting views'
21 changes: 21 additions & 0 deletions modules/gdpr_view_export_log/gdpr_view_export_log.routing.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
gdpr_view_export_log.view_users:
path: '/admin/gdpr/export/{id}/users'
defaults:
_controller: '\Drupal\gdpr_view_export_log\Controller\ExportAuditController::viewUsers'
requirements:
_permission: 'create gdpr export audits'

gdpr_view_export_log.delete_user:
path: '/admin/gdpr/export/{id}/users/{user_id}/remove'
defaults:
_form: '\Drupal\gdpr_view_export_log\Form\RemoveUserFromExportForm'
requirements:
_permission: 'create gdpr export audits'

gdpr_view_export_log.exports_containing_user:
path: '/admin/gdpr/exports_containing_user/{user_id}'
defaults:
#_controller: '\Drupal\gdpr_view_export_log\Controller\ExportAuditController::viewExports'
_entity_list: 'gdpr_view_export_audit'
requirements:
_permission: 'create gdpr export audits'
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

namespace Drupal\gdpr_view_export_log\Controller;

use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Url;

/**
* Class ExportAuditController.
*
* @package Drupal\gdpr_view_export_log\Controller
*/
class ExportAuditController extends ControllerBase {

/**
* Views the users that were part of an export audit.
*
* @param string $id
* The audit id.
*
* @return array
* Render array
*/
public function viewUsers($id) {
$query = \Drupal::database()->select('gdpr_view_export_audit_user_ids', 'g');

$count_query = clone $query;
$count_query->addExpression('count(g.id)');

$paged_query = $query->extend('Drupal\Core\Database\Query\PagerSelectExtender');
$paged_query->limit(50);
$paged_query->setCountQuery($count_query);
$paged_query->addJoin('left', 'users_field_data', 'u', 'g.user_id = u.uid');

$results = $paged_query->fields('g', ['user_id'])
->fields('u', ['name'])
->condition('g.log_id', $id)
->execute()
->fetchAll(\PDO::FETCH_ASSOC);

$output = [
'back' => [
'#type' => 'link',
'#url' => Url::fromRoute('entity.gdpr_view_export_audit.collection'),
'#title' => $this->t('Back to export list'),
],

'table' => [
'#type' => 'table',
'#header' => ['ID', 'Username', ''],
'#empty' => 'Could not locate any users in this export.',
],

'pager' => [
'#theme' => 'pager',
'#weight' => 5,
'#element' => 0,
'#parameters' => [],
'#quantity' => 9,
'#route_name' => '<none>',
'#tags' => '',
],
];

foreach ($results as $result) {
$output['table'][$result['user_id']]['userid'] = [
'#markup' => $result['user_id'],
];

$output['table'][$result['user_id']]['username'] = [
'#type' => 'link',
'#url' => Url::fromRoute('entity.user.canonical', ['user' => $result['user_id']]),
'#title' => $result['name'],
];

$output['table'][$result['user_id']]['operations'] = [
'#type' => 'operations',
'#links' => [
'remove' => [
'title' => $this->t('Remove'),
'url' => Url::fromRoute('gdpr_view_export_log.delete_user', [
'id' => $id,
'user_id' => $result['user_id'],
]),
],
],
];
}

return $output;
}

}
Loading