FEATURE: Add data processor to remove fields for indexing

Add a new processor, with docs and tests, to allow removal of fields
before sending them to search service like elasticsearch.

E.g. remove sensitive information that should not be available.
This commit is contained in:
Daniel Siepmann 2017-11-08 20:20:37 +01:00
parent 43ec410eb5
commit 0159315183
Signed by: Daniel Siepmann
GPG key ID: 33D6629915560EF4
4 changed files with 190 additions and 0 deletions

View file

@ -0,0 +1,44 @@
<?php
namespace Codappix\SearchCore\DataProcessing;
/*
* Copyright (C) 2017 Daniel Siepmann <coding@daniel-siepmann.de>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
use TYPO3\CMS\Core\Utility\GeneralUtility;
/**
* Removes fields from record.
*/
class RemoveProcessor implements ProcessorInterface
{
public function processRecord(array $record, array $configuration) : array
{
if (!isset($configuration['fields'])) {
return $record;
}
foreach (GeneralUtility::trimExplode(',', $configuration['fields'], true) as $field) {
if (isset($record[$field])) {
unset($record[$field]);
}
}
return $record;
}
}

View file

@ -0,0 +1,23 @@
``Codappix\SearchCore\DataProcessing\RemoveProcessor``
======================================================
Will remove fields from record, e.g. if you do not want to sent them to elasticsearch at all.
Possible Options:
``fields``
Comma separated list of fields to remove from record.
Example::
plugin.tx_searchcore.settings.indexing.tt_content.dataProcessing {
1 = Codappix\SearchCore\DataProcessing\RemoveProcessor
1 {
fields = description
}
2 = Codappix\SearchCore\DataProcessing\CopyToProcessor
2 {
fields = description, another_field
}
}

View file

@ -180,6 +180,7 @@ The following Processor are available:
:glob: :glob:
dataProcessing/CopyToProcessor dataProcessing/CopyToProcessor
dataProcessing/RemoveProcessor
dataProcessing/GeoPointProcessor dataProcessing/GeoPointProcessor
The following Processor are planned: The following Processor are planned:

View file

@ -0,0 +1,122 @@
<?php
namespace Codappix\SearchCore\Tests\Unit\DataProcessing;
/*
* Copyright (C) 2017 Daniel Siepmann <coding@daniel-siepmann.de>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
use Codappix\SearchCore\DataProcessing\RemoveProcessor;
use Codappix\SearchCore\Tests\Unit\AbstractUnitTestCase;
class RemoveProcessorTest extends AbstractUnitTestCase
{
/**
* @test
* @dataProvider getPossibleRecordConfigurationCombinations
*/
public function fieldsAreCopiedAsConfigured(array $record, array $configuration, array $expectedRecord)
{
$subject = new RemoveProcessor();
$processedRecord = $subject->processRecord($record, $configuration);
$this->assertSame(
$expectedRecord,
$processedRecord,
'The processor did not return the expected processed record.'
);
}
/**
* @return array
*/
public function getPossibleRecordConfigurationCombinations()
{
return [
'Nothing configured' => [
'record' => [
'field 1' => 'Some content like lorem',
'field with sub2' => [
'Tag 1',
'Tag 2',
],
],
'configuration' => [
],
'expectedRecord' => [
'field 1' => 'Some content like lorem',
'field with sub2' => [
'Tag 1',
'Tag 2',
],
],
],
'Single field configured' => [
'record' => [
'field 1' => 'Some content like lorem',
'field with sub2' => [
'Tag 1',
'Tag 2',
],
],
'configuration' => [
'fields' => 'field with sub2',
'_typoScriptNodeValue' => 'Codappix\SearchCore\DataProcessing\RemoveProcessor',
],
'expectedRecord' => [
'field 1' => 'Some content like lorem',
],
],
'Non existing field configured' => [
'record' => [
'field 1' => 'Some content like lorem',
'field with sub2' => [
'Tag 1',
'Tag 2',
],
],
'configuration' => [
'fields' => 'non existing',
'_typoScriptNodeValue' => 'Codappix\SearchCore\DataProcessing\RemoveProcessor',
],
'expectedRecord' => [
'field 1' => 'Some content like lorem',
'field with sub2' => [
'Tag 1',
'Tag 2',
],
],
],
'Multiple fields configured' => [
'record' => [
'field 1' => 'Some content like lorem',
'field with sub2' => [
'Tag 1',
'Tag 2',
],
'field 3' => 'Some more like lorem',
],
'configuration' => [
'fields' => 'field 3, field with sub2',
'_typoScriptNodeValue' => 'Codappix\SearchCore\DataProcessing\RemoveProcessor',
],
'expectedRecord' => [
'field 1' => 'Some content like lorem',
],
],
];
}
}