You are here

public function Anonymizer::run in General Data Protection Regulation 7

Runs anonymization routines against a user.

Parameters

GDPRTask $task: The current task being executed.

Return value

array Returns array containing any error messages.

File

modules/gdpr_tasks/src/Anonymizer.php, line 40

Class

Anonymizer
Anonymizes or removes field values for GDPR.

Code

public function run(GDPRTask $task) {

  // Make sure we load a fresh copy of the entity (bypassing the cache)
  // so we don't end up affecting any other references to the entity.
  $user = $task
    ->getOwner();
  $log = array();
  if (!$this
    ->checkExportDirectoryExists()) {
    $this->errors[] = 'An export directory has not been set. Please set this under Configuration -> GDPR -> Right to be Forgotten';
  }
  foreach (gdpr_tasks_collect_rtf_data($user, TRUE) as $data) {
    $mode = $data['rtf'];
    $entity_type = $data['entity_type'];
    $entity_id = $data['entity_id'];
    $entity = $data['entity'];
    $wrapper = entity_metadata_wrapper($entity_type, $entity_id);
    $entity_bundle = $wrapper
      ->type();
    $entity_success = TRUE;
    $success = TRUE;
    $msg = NULL;
    $sanitizer = '';
    if ($mode == 'anonymise') {
      list($success, $msg, $sanitizer) = $this
        ->anonymize($data, $entity);
    }
    elseif ($mode == 'remove') {
      list($success, $msg) = $this
        ->remove($data, $entity);
    }
    if ($success === TRUE) {
      $log[] = 'success';
      $log[] = array(
        'entity_id' => $entity_id,
        'entity_type' => $entity_type . '.' . $entity_bundle,
        'field_name' => $data['plugin']->property_name,
        'action' => $mode,
        'sanitizer' => $sanitizer,
      );
    }
    else {

      // Could not anonymize/remove field. Record to errors list.
      // Prevent entity from being saved.
      $entity_success = FALSE;
      $this->errors[] = $msg;
      $log[] = 'error';
      $log[] = array(
        'error' => $msg,
        'entity_id' => $entity_id,
        'entity_type' => $entity_type . '.' . $entity_bundle,
        'field_name' => $data['plugin']->property_name,
        'action' => $mode,
        'sanitizer' => $sanitizer,
      );
    }
    if ($entity_success) {
      $this->successes[$entity_type][$entity_id] = $entity;
    }
    else {
      $this->failures[] = $entity;
    }
  }

  // @todo Better log field.
  $task
    ->wrapper()->gdpr_tasks_removal_log = json_encode($log);
  $this
    ->complete($task);
  return $this->errors;
}