Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/Database/Validator/Query/Filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,13 @@ protected function isValidAttribute(string $attribute): bool
*/
protected function isValidAttributeAndValues(string $attribute, array $values, string $method): bool
{
if (
isset($this->schema[$attribute]['filters'])
&& in_array('encrypt', $this->schema[$attribute]['filters'])
) {
Comment thread
abnegate marked this conversation as resolved.
Outdated
$this->message = 'Cannot select encrypted attribute: ' . $attribute;
Comment thread
abnegate marked this conversation as resolved.
Outdated
return false;
}
Comment thread
abnegate marked this conversation as resolved.
Outdated
if (!$this->isValidAttribute($attribute)) {
return false;
}
Expand Down
48 changes: 48 additions & 0 deletions tests/e2e/Adapter/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -2069,6 +2069,7 @@ public function testCreateCollectionValidator(): void

public function testCreateDocument(): Document
{

Comment thread
abnegate marked this conversation as resolved.
Outdated
static::getDatabase()->createCollection('documents');

$this->assertEquals(true, static::getDatabase()->createAttribute('documents', 'string', Database::VAR_STRING, 128, true));
Expand Down Expand Up @@ -2814,6 +2815,53 @@ public function testGetDocument(Document $document): Document
return $document;
}

public function testEncryptAttributes(): void
{
// Add custom encrypt filter
static::getDatabase()->addFilter(
'encrypt',
function (mixed $value) {
return json_encode([
'data' => base64_encode($value),
'method' => 'base64',
'version' => 'v1',
]);
},
function (mixed $value) {
if (is_null($value)) {
return;
}
$value = json_decode($value, true);
return base64_decode($value['data']);
}
);

static::getDatabase()->createCollection('test_documents');
Comment thread
abnegate marked this conversation as resolved.
Outdated

static::getDatabase()->createAttribute('test_documents', 'title', Database::VAR_STRING, 255, true);
static::getDatabase()->createAttribute('test_documents', 'encrypt', Database::VAR_STRING, 128, true, filters: ['encrypt']);

static::getDatabase()->createDocument('test_documents', new Document([
'title' => 'Sample Title',
'encrypt' => 'secret',
]));
// query against encrypt
try {
$queries = [Query::equal('encrypt', ['test'])];
$doc = static::getDatabase()->find('test_documents', $queries);
$this->fail('Queried against encrypt field. Failed to throw exeception.');
} catch (Throwable $e) {
$this->assertTrue($e instanceof QueryException);
}

try {
$queries = [Query::equal('title', ['test'])];
static::getDatabase()->find('test_documents', $queries);
} catch (Throwable $e) {
$this->fail('Should not have thrown error');
}
}

/**
* @depends testCreateDocument
*/
Expand Down