-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
IBX-1410: Added
UserId
, UserEmail
& UserLogin
Criterions handli…
…ng (#224)
- Loading branch information
Showing
7 changed files
with
213 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
64 changes: 64 additions & 0 deletions
64
src/lib/FieldMapper/ContentFieldMapper/UserDocumentFields.php
This file contains 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,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; | ||
} | ||
} |
This file contains 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,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 | ||
) | ||
) | ||
); | ||
} | ||
} |
This file contains 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,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 | ||
) | ||
) | ||
); | ||
} | ||
} |
This file contains 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,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 | ||
) | ||
) | ||
); | ||
} | ||
} |