1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LeKoala\Encrypt\Test; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Assets\File; |
6
|
|
|
use SilverStripe\Dev\TestOnly; |
7
|
|
|
use SilverStripe\ORM\DataObject; |
8
|
|
|
use SilverStripe\Security\Member; |
9
|
|
|
use LeKoala\Encrypt\EncryptedFile; |
10
|
|
|
use LeKoala\Encrypt\EncryptedDBJson; |
11
|
|
|
use LeKoala\Encrypt\EncryptedDBText; |
12
|
|
|
use LeKoala\Encrypt\EncryptedDBField; |
13
|
|
|
use LeKoala\Encrypt\EncryptedDBVarchar; |
14
|
|
|
use LeKoala\Encrypt\HasEncryptedFields; |
15
|
|
|
use ParagonIE\CipherSweet\JsonFieldMap; |
16
|
|
|
use LeKoala\Encrypt\EncryptedDBHTMLText; |
17
|
|
|
use LeKoala\Encrypt\EncryptedNumberField; |
18
|
|
|
use LeKoala\Encrypt\EncryptHelper; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* A test model for our encryption |
22
|
|
|
* |
23
|
|
|
* @property string $Name |
24
|
|
|
* @property string $MyText |
25
|
|
|
* @property string $MyHTMLText |
26
|
|
|
* @property string $MyVarchar |
27
|
|
|
* @property string $MyNumber |
28
|
|
|
* @property string $MyVarcharWithIndex |
29
|
|
|
* @property string $MyJson |
30
|
|
|
* @property string $MyEncryptedJson |
31
|
|
|
* @property int $RegularFileID |
32
|
|
|
* @property int $EncryptedFileID |
33
|
|
|
*/ |
34
|
|
|
class Test_EncryptedModel extends DataObject implements TestOnly |
35
|
|
|
{ |
36
|
|
|
use HasEncryptedFields; |
37
|
|
|
|
38
|
|
|
private static $table_name = 'EncryptedModel'; |
|
|
|
|
39
|
|
|
|
40
|
|
|
private static $db = [ |
|
|
|
|
41
|
|
|
"Name" => 'Varchar', |
42
|
|
|
"MyText" => EncryptedDBText::class, |
43
|
|
|
"MyHTMLText" => EncryptedDBHTMLText::class, |
44
|
|
|
"MyVarchar" => EncryptedDBVarchar::class, |
45
|
|
|
"MyNumber" => EncryptedNumberField::class . '(["output_size" => 4, "domain_size" => 10])', |
46
|
|
|
"MyIndexedVarchar" => EncryptedDBField::class, |
47
|
|
|
"MyJson" => EncryptedDBJson::class, |
48
|
|
|
"MyEncryptedJson" => EncryptedDBJson::class . "(['map' => '7551830f{\"fields\":{\"$6e616d65\":\"string\",\"$616374697665\":\"bool\",\"$616765\":\"int\"}}'])", |
49
|
|
|
]; |
50
|
|
|
|
51
|
|
|
private static $has_one = [ |
|
|
|
|
52
|
|
|
"RegularFile" => File::class, |
53
|
|
|
// We use regular File class for encrypted files to keep only one table |
54
|
|
|
"EncryptedFile" => File::class, |
55
|
|
|
"EncryptedFileClass" => EncryptedFile::class, |
56
|
|
|
"Member" => Member::class, |
57
|
|
|
]; |
58
|
|
|
|
59
|
|
|
private static $indexes = [ |
|
|
|
|
60
|
|
|
'MyIndexedVarcharBlindIndex' => true, |
61
|
|
|
'MyNumberBlindIndex' => true, |
62
|
|
|
'MyNumberLastFourBlindIndex' => true, |
63
|
|
|
]; |
64
|
|
|
|
65
|
|
|
protected function writeBaseRecord($baseTable, $now) |
66
|
|
|
{ |
67
|
|
|
parent::writeBaseRecord($baseTable, $now); |
68
|
|
|
// After base record is written, we have an ID and therefore AAD has changed |
69
|
|
|
$this->resetFieldValues(); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function getField($field) |
73
|
|
|
{ |
74
|
|
|
return $this->getEncryptedField($field); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function setField($fieldName, $val) |
78
|
|
|
{ |
79
|
|
|
return $this->setEncryptedField($fieldName, $val); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|