-
Notifications
You must be signed in to change notification settings - Fork 7
[WIP] By yanniboi: Added GDPR views data export module for tracking data exports. #7
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
Open
yanniboi
wants to merge
3
commits into
brainsum:8.x-1.x
Choose a base branch
from
FreelyGive:feature/gdpr-data-export
base: 8.x-1.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
modules/gdpr_view_export_log/gdpr_view_export_log.info.yml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
138
modules/gdpr_view_export_log/gdpr_view_export_log.module
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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'); | ||
|
|
||
| } | ||
|
|
||
| } |
2 changes: 2 additions & 0 deletions
2
modules/gdpr_view_export_log/gdpr_view_export_log.permissions.yml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
21
modules/gdpr_view_export_log/gdpr_view_export_log.routing.yml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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' |
93 changes: 93 additions & 0 deletions
93
modules/gdpr_view_export_log/src/Controller/ExportAuditController.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
|
|
||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.