lekoala /
silverstripe-encrypt
| 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 | /** |
||
| 39 | * @var string |
||
| 40 | */ |
||
| 41 | private static $table_name = 'EncryptedModel'; |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 42 | |||
| 43 | /** |
||
| 44 | * @var array<string,string> |
||
| 45 | */ |
||
| 46 | private static $db = [ |
||
|
0 ignored issues
–
show
|
|||
| 47 | "Name" => 'Varchar', |
||
| 48 | "MyText" => EncryptedDBText::class, |
||
| 49 | "MyHTMLText" => EncryptedDBHTMLText::class, |
||
| 50 | "MyVarchar" => EncryptedDBVarchar::class, |
||
| 51 | "MyNumber" => EncryptedNumberField::class . '(["output_size" => 4, "domain_size" => 10])', |
||
| 52 | "MyIndexedVarchar" => EncryptedDBField::class, |
||
| 53 | "MyNullIndexedVarchar" => EncryptedDBField::class, |
||
| 54 | "MyJson" => EncryptedDBJson::class, |
||
| 55 | "MyEncryptedJson" => EncryptedDBJson::class . "(['map' => '7551830f{\"fields\":{\"$6e616d65\":\"string\",\"$616374697665\":\"bool\",\"$616765\":\"int\"}}'])", // phpcs:ignore |
||
| 56 | ]; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var array<string,string> |
||
| 60 | */ |
||
| 61 | private static $has_one = [ |
||
|
0 ignored issues
–
show
|
|||
| 62 | "RegularFile" => File::class, |
||
| 63 | // We use regular File class for encrypted files to keep only one table |
||
| 64 | "EncryptedFile" => File::class, |
||
| 65 | "EncryptedFileClass" => EncryptedFile::class, |
||
| 66 | "Member" => Member::class, |
||
| 67 | ]; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @var array<string,mixed> |
||
| 71 | */ |
||
| 72 | private static $indexes = [ |
||
|
0 ignored issues
–
show
|
|||
| 73 | 'MyIndexedVarcharBlindIndex' => true, |
||
| 74 | 'MyNumberBlindIndex' => true, |
||
| 75 | 'MyNumberLastFourBlindIndex' => true, |
||
| 76 | ]; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @param string $baseTable |
||
| 80 | * @param string $now |
||
| 81 | * @return void |
||
| 82 | */ |
||
| 83 | protected function writeBaseRecord($baseTable, $now) |
||
| 84 | { |
||
| 85 | parent::writeBaseRecord($baseTable, $now); |
||
| 86 | // After base record is written, we have an ID and therefore AAD has changed |
||
| 87 | $this->resetFieldValues(); |
||
| 88 | } |
||
| 89 | |||
| 90 | public function getField(string $field): mixed |
||
| 91 | { |
||
| 92 | return $this->getEncryptedField($field); |
||
| 93 | } |
||
| 94 | |||
| 95 | public function setField(string $fieldName, mixed $value): static |
||
| 96 | { |
||
| 97 | return $this->setEncryptedField($fieldName, $value); |
||
| 98 | } |
||
| 99 | } |
||
| 100 |