Skip to content

Commit

Permalink
IBX-1410: Added UserId, UserEmail & UserLogin Criterions handli…
Browse files Browse the repository at this point in the history
…ng (#224)
  • Loading branch information
Steveb-p authored Nov 12, 2021
1 parent 3c1d6ec commit c670f01
Show file tree
Hide file tree
Showing 7 changed files with 213 additions and 3 deletions.
8 changes: 5 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@
},
"autoload": {
"psr-4": {
"EzSystems\\EzPlatformSolrSearchEngine\\": "lib",
"EzSystems\\EzPlatformSolrSearchEngineBundle\\": "bundle",
"EzSystems\\EzPlatformSolrSearchEngine\\Tests\\SetupFactory\\": "tests/lib/SetupFactory"
"Ibexa\\Solr\\": "src/lib/",
"Ibexa\\Tests\\Solr\\SetupFactory\\": "tests/lib/SetupFactory/",
"EzSystems\\EzPlatformSolrSearchEngine\\": "lib/",
"EzSystems\\EzPlatformSolrSearchEngineBundle\\": "bundle/",
"EzSystems\\EzPlatformSolrSearchEngine\\Tests\\SetupFactory\\": "tests/lib/SetupFactory/"
}
},
"autoload-dev": {
Expand Down
15 changes: 15 additions & 0 deletions lib/Resources/config/container/solr/criterion_visitors.yml
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,21 @@ services:
- {name: ezpublish.search.solr.query.content.criterion_visitor}
- {name: ezpublish.search.solr.query.location.criterion_visitor}

Ibexa\Solr\Query\Common\CriterionVisitor\UserEmailIn:
tags:
- { name: ezpublish.search.solr.query.content.criterion_visitor }
- { name: ezpublish.search.solr.query.location.criterion_visitor }

Ibexa\Solr\Query\Common\CriterionVisitor\UserIdIn:
tags:
- { name: ezpublish.search.solr.query.content.criterion_visitor }
- { name: ezpublish.search.solr.query.location.criterion_visitor }

Ibexa\Solr\Query\Common\CriterionVisitor\UserLoginIn:
tags:
- { name: ezpublish.search.solr.query.content.criterion_visitor }
- { name: ezpublish.search.solr.query.location.criterion_visitor }

ezpublish.search.solr.query.common.criterion_visitor.user_metadata_in:
class: "%ezpublish.search.solr.query.common.criterion_visitor.user_metadata_in.class%"
tags:
Expand Down
4 changes: 4 additions & 0 deletions lib/Resources/config/container/solr/field_mappers.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ services:
tags:
- {name: ezpublish.search.solr.field_mapper.content}

Ibexa\Solr\FieldMapper\ContentFieldMapper\UserDocumentFields:
tags:
- { name: ezpublish.search.solr.field_mapper.content }

ezpublish.search.solr.field_mapper.content.content_document_location_fields:
class: '%ezpublish.search.solr.field_mapper.content.content_document_location_fields.class%'
arguments:
Expand Down
64 changes: 64 additions & 0 deletions src/lib/FieldMapper/ContentFieldMapper/UserDocumentFields.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Solr\FieldMapper\ContentFieldMapper;

use eZ\Publish\SPI\Persistence\Content as SPIContent;
use eZ\Publish\SPI\Search\Field;
use eZ\Publish\SPI\Search\FieldType;
use EzSystems\EzPlatformSolrSearchEngine\FieldMapper\ContentFieldMapper;

final class UserDocumentFields extends ContentFieldMapper
{
/** @internal */
public const HASHING_ALGORITHM = 'sha256';

public function accept(SPIContent $content): bool
{
return $this->getUserField($content) !== null;
}

public function mapFields(SPIContent $content): array
{
$userField = $this->getUserField($content);
if ($userField === null) {
return [];
}

$fields = [];

if (isset($userField->value->externalData['login'])) {
$fields[] = new Field(
'user_login',
hash(self::HASHING_ALGORITHM, $userField->value->externalData['login']),
new FieldType\IdentifierField()
);
}

if (isset($userField->value->externalData['email'])) {
$fields[] = new Field(
'user_email',
hash(self::HASHING_ALGORITHM, $userField->value->externalData['email']),
new FieldType\IdentifierField()
);
}

return $fields;
}

private function getUserField(SPIContent $content): ?SPIContent\Field
{
foreach ($content->fields as $field) {
if ($field->type === 'ezuser') {
return $field;
}
}

return null;
}
}
42 changes: 42 additions & 0 deletions src/lib/Query/Common/CriterionVisitor/UserEmailIn.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Solr\Query\Common\CriterionVisitor;

use eZ\Publish\API\Repository\Values\Content\Query\Criterion;
use eZ\Publish\API\Repository\Values\Content\Query\Criterion\Operator;
use EzSystems\EzPlatformSolrSearchEngine\Query\CriterionVisitor;
use Ibexa\Solr\FieldMapper\ContentFieldMapper\UserDocumentFields;

final class UserEmailIn extends CriterionVisitor
{
public function canVisit(Criterion $criterion): bool
{
if (!$criterion instanceof Criterion\UserEmail) {
return false;
}

return in_array($criterion->operator ?? Operator::IN, [Operator::IN, Operator::EQ], true);
}

public function visit(Criterion $criterion, CriterionVisitor $subVisitor = null): string
{
return sprintf(
'(%s)',
implode(
' OR ',
array_map(
static function (string $value): string {
return 'user_email_id:"' . hash(UserDocumentFields::HASHING_ALGORITHM, $value) . '"';
},
(array) $criterion->value
)
)
);
}
}
41 changes: 41 additions & 0 deletions src/lib/Query/Common/CriterionVisitor/UserIdIn.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Solr\Query\Common\CriterionVisitor;

use eZ\Publish\API\Repository\Values\Content\Query\Criterion;
use eZ\Publish\API\Repository\Values\Content\Query\Criterion\Operator;
use EzSystems\EzPlatformSolrSearchEngine\Query\CriterionVisitor;

final class UserIdIn extends CriterionVisitor
{
public function canVisit(Criterion $criterion): bool
{
if (!$criterion instanceof Criterion\UserId) {
return false;
}

return in_array($criterion->operator ?? Operator::IN, [Operator::IN, Operator::EQ], true);
}

public function visit(Criterion $criterion, CriterionVisitor $subVisitor = null): string
{
return sprintf(
'(%s)',
implode(
' OR ',
array_map(
static function (string $value): string {
return 'content_id_id:"' . $value . '"';
},
(array) $criterion->value
)
)
);
}
}
42 changes: 42 additions & 0 deletions src/lib/Query/Common/CriterionVisitor/UserLoginIn.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Solr\Query\Common\CriterionVisitor;

use eZ\Publish\API\Repository\Values\Content\Query\Criterion;
use eZ\Publish\API\Repository\Values\Content\Query\Criterion\Operator;
use EzSystems\EzPlatformSolrSearchEngine\Query\CriterionVisitor;
use Ibexa\Solr\FieldMapper\ContentFieldMapper\UserDocumentFields;

final class UserLoginIn extends CriterionVisitor
{
public function canVisit(Criterion $criterion): bool
{
if (!$criterion instanceof Criterion\UserLogin) {
return false;
}

return in_array($criterion->operator ?? Operator::IN, [Operator::IN, Operator::EQ], true);
}

public function visit(Criterion $criterion, CriterionVisitor $subVisitor = null): string
{
return sprintf(
'(%s)',
implode(
' OR ',
array_map(
static function (string $value): string {
return 'user_login_id:"' . hash(UserDocumentFields::HASHING_ALGORITHM, $value) . '"';
},
(array) $criterion->value
)
)
);
}
}

0 comments on commit c670f01

Please sign in to comment.