Get collections by private fields & filter collections by user rights

This commit is contained in:
Mathieu Darse
2015-06-29 15:25:36 +02:00
committed by Benoît Burnichon
parent 801314bf59
commit 4157d14d1b
5 changed files with 75 additions and 25 deletions

View File

@@ -326,28 +326,19 @@ class ElasticSearchEngine implements SearchEngineInterface
*/ */
private function getAllowedPrivateFields(SearchEngineOptions $options) private function getAllowedPrivateFields(SearchEngineOptions $options)
{ {
$fields = array_keys($this->structure->getPrivateFields()); // Get structure data and cross it with user rights (from options object)
$allowed_collections = [];
$allowed = array_fill_keys($fields, []); foreach ($options->getBusinessFieldsOn() as $collection) {
foreach ($options->getDataboxes() as $databox) { $allowed_collections[] = $collection->get_base_id();
$databoxFields = $databox->get_meta_structure();
foreach ($fields as $field) {
if ($databoxFields->get_element_by_name($field)) {
$allowed[$field] += $databox->get_collection_unique_ids();
}
}
} }
$businessCollections = array_map(function (\collection $collection) { $map = $this->structure->getCollectionsUsedByPrivateFields();
return $collection->get_base_id();
}, $options->getBusinessFieldsOn());
// Remove collections base_id which access is restricted. // Remove collections base_id which access is restricted.
foreach ($allowed as $name => &$collections) { foreach ($map as $_ => &$collections) {
$collections = array_diff($collections, $businessCollections); $collections = array_intersect($collections, $allowed_collections);
} }
return $allowed; return $map;
} }
/** /**

View File

@@ -98,9 +98,9 @@ class Field
return $this->type; return $this->type;
} }
public function getCollections() public function getDependantCollections()
{ {
return $this->collections; return $this->used_by_collections;
} }
public function isSearchable() public function isSearchable()
@@ -169,10 +169,15 @@ class Field
); );
} }
$used_by_collections = array_unique(array_merge( $used_by_collections = array_values(
$this->used_by_collections, array_unique(
$other->used_by_collections array_merge(
), SORT_REGULAR); $this->used_by_collections,
$other->used_by_collections
),
SORT_REGULAR
)
);
return new self($this->name, $this->type, [ return new self($this->name, $this->type, [
'searchable' => $this->is_searchable, 'searchable' => $this->is_searchable,

View File

@@ -103,4 +103,22 @@ class Structure
throw new DomainException(sprintf('Unknown field "%s".', $name)); throw new DomainException(sprintf('Unknown field "%s".', $name));
} }
/**
* Returns an array of collections indexed by field name.
*
* [
* "FieldName" => [1, 4, 5],
* "OtherFieldName" => [4],
* ]
*/
public function getCollectionsUsedByPrivateFields()
{
$map = [];
foreach ($this->private as $name => $field) {
$map[$name] = $field->getDependantCollections();
}
return $map;
}
} }

View File

@@ -26,7 +26,7 @@ class FieldTest extends \PHPUnit_Framework_TestCase
$this->assertFalse($merged->isPrivate()); $this->assertFalse($merged->isPrivate());
$this->assertFalse($merged->isFacet()); $this->assertFalse($merged->isFacet());
$this->assertNull($merged->getThesaurusRoots()); $this->assertNull($merged->getThesaurusRoots());
$this->assertEquals(['1', '2', '3', '4'], $merged->getCollections()); $this->assertEquals(['1', '2', '3', '4'], $merged->getDependantCollections());
} }
/** /**
@@ -106,4 +106,16 @@ class FieldTest extends \PHPUnit_Framework_TestCase
$merged = $field->mergeWith($other); $merged = $field->mergeWith($other);
$this->assertEquals([$foo, $bar], $merged->getThesaurusRoots()); $this->assertEquals([$foo, $bar], $merged->getThesaurusRoots());
} }
public function testMergeWithDependantCollections()
{
$field = new Field('foo', Mapping::TYPE_STRING, [
'used_by_collections' => [1, 2]
]);
$other = new Field('foo', Mapping::TYPE_STRING, [
'used_by_collections' => [2, 3]
]);
$merged = $field->mergeWith($other);
$this->assertEquals([1, 2, 3], $merged->getDependantCollections());
}
} }

View File

@@ -34,7 +34,7 @@ class StructureTest extends \PHPUnit_Framework_TestCase
$field->isPrivate()->willReturn(false); $field->isPrivate()->willReturn(false);
$field->isFacet()->willReturn(false); $field->isFacet()->willReturn(false);
$field->hasConceptInference()->willReturn(false); $field->hasConceptInference()->willReturn(false);
$field->getCollections()->willReturn(['1']); $field->getDependantCollections()->willReturn(['1']);
$structure->add($field->reveal()); $structure->add($field->reveal());
$this->assertCount(1, $structure->getAllFields()); $this->assertCount(1, $structure->getAllFields());
@@ -173,4 +173,28 @@ class StructureTest extends \PHPUnit_Framework_TestCase
$structure = new Structure(); $structure = new Structure();
$structure->isPrivate('foo'); $structure->isPrivate('foo');
} }
public function testCollectionsUsedByPrivateFields()
{
$structure = new Structure();
$structure->add($foo = (new Field('foo', Mapping::TYPE_STRING, [
'private' => true,
'used_by_collections' => [1, 2]
])));
$structure->add(new Field('foo', Mapping::TYPE_STRING, [
'private' => true,
'used_by_collections' => [2, 3]
]));
$structure->add(new Field('bar', Mapping::TYPE_STRING, [
'private' => true,
'used_by_collections' => [2, 3]
]));
$structure->add(new Field('baz', Mapping::TYPE_STRING, ['private' => false]));
$this->assertEquals([1, 2], $foo->getDependantCollections());
static $expected = [
'foo' => [1, 2, 3],
'bar' => [2, 3]
];
$this->assertEquals($expected, $structure->getCollectionsUsedByPrivateFields());
}
} }