-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathEmailAnonymizer.php
More file actions
56 lines (47 loc) · 1.68 KB
/
EmailAnonymizer.php
File metadata and controls
56 lines (47 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?php
declare(strict_types=1);
namespace MakinaCorpus\DbToolsBundle\Anonymization\Anonymizer\Core;
use MakinaCorpus\DbToolsBundle\Anonymization\Anonymizer\AbstractSingleColumnAnonymizer;
use MakinaCorpus\DbToolsBundle\Attribute\AsAnonymizer;
use MakinaCorpus\QueryBuilder\Expression;
use MakinaCorpus\QueryBuilder\Vendor;
use MakinaCorpus\QueryBuilder\Query\Update;
#[AsAnonymizer(
name: 'email',
pack: 'core',
description: <<<TXT
Anonymize email addresses. You can choose a domain and a tld with option 'domain'.
Values are salted to prevent reversing the hash with option 'use_salt' (default: true).
TXT
)]
class EmailAnonymizer extends AbstractSingleColumnAnonymizer
{
#[\Override]
protected function validateOptions(): void
{
$this->options->getString('domain', 'example.com', true);
$this->options->getBool('use_salt', false);
}
#[\Override]
public function createAnonymizeExpression(Update $update): Expression
{
$expr = $update->expression();
if ($this->databaseSession->vendorIs(Vendor::SQLITE)) {
$emailHashExpr = $this->getJoinColumn();
} else {
$userExpr = $expr->column($this->columnName, $this->tableName);
if ($this->options->getBool('use_salt', true)) {
$userExpr = $expr->concat($userExpr, $expr->value($this->context->salt));
}
$emailHashExpr = $expr->md5($userExpr);
}
return $this->getSetIfNotNullExpression(
$expr->concat(
'anon-',
$emailHashExpr,
'@',
$this->options->get('domain', 'example.com'),
)
);
}
}