Skip to content

Commit 1e482d0

Browse files
committed
Add support for CMS 6
1 parent e86ead4 commit 1e482d0

17 files changed

+87
-52
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@
77
.test-output
88
/composer.lock
99
*.log
10+
public/

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ This module allows Silverstripe CMS ORM data to be encrypted before being stored
1212

1313

1414
## Requirements
15-
* SilverStripe CMS 5.0
15+
* SilverStripe CMS 6.0
1616

1717
## Installation
1818
Install via Composer:
@@ -32,7 +32,7 @@ For development environments you can set this in your `.env` e.g:
3232
ENCRYPT_AT_REST_KEY="{generated defuse key}"
3333
```
3434

35-
For more information view SilverStripe [Environment Management](https://docs.silverstripe.org/en/4/getting_started/environment_management/).
35+
For more information view SilverStripe [Environment Management](https://docs.silverstripe.org/en/6/getting_started/environment_management/).
3636

3737
## Usage
3838

composer.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@
1616
"source": "https://github.com/madmatt/silverstripe-encrypt-at-rest"
1717
},
1818
"require": {
19-
"php": "^8.0",
20-
"silverstripe/framework": "^5",
19+
"php": "^8.3",
20+
"silverstripe/framework": "^6",
2121
"defuse/php-encryption": "^2.2"
2222
},
2323
"require-dev": {
24-
"phpunit/phpunit": "^9.6"
24+
"phpunit/phpunit": "^11.3"
2525
},
2626
"autoload": {
2727
"psr-4": {

phpunit.xml.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0"?>
2-
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="vendor/silverstripe/cms/tests/bootstrap.php" colors="true" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="vendor/silverstripe/framework/tests/bootstrap.php" colors="true" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
33
<coverage includeUncoveredFiles="true">
44
<include>
55
<directory suffix=".php">src/</directory>

src/Extension/DecryptDataObjectFieldsExtension.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Madmatt\EncryptAtRest\Extension;
44

5-
use SilverStripe\ORM\DataExtension;
5+
use SilverStripe\Core\Extension;
66
use SilverStripe\Core\Config\Config;
77
use SilverStripe\Core\Injector\Injector;
88
use SilverStripe\ORM\DataObject;
@@ -18,7 +18,7 @@
1818
* @package EncryptAtRest\Extension
1919
* @property DecryptDataObjectFieldsExtension|DataObject $owner
2020
*/
21-
class DecryptDataObjectFieldsExtension extends DataExtension
21+
class DecryptDataObjectFieldsExtension extends Extension
2222
{
2323
/**
2424
* During hydration of an existing DataObject retrieved from the database, this extension method will be called. We

src/FieldType/EncryptedDatetime.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
use Exception;
66
use Madmatt\EncryptAtRest\Traits\EncryptedFieldGetValueTrait;
77
use SilverStripe\Core\Injector\Injector;
8+
use SilverStripe\Core\Validation\FieldValidation\DatetimeFieldValidator;
9+
use SilverStripe\Model\ModelData;
810
use SilverStripe\ORM\DB;
911
use SilverStripe\ORM\FieldType\DBDatetime;
1012
use Madmatt\EncryptAtRest\AtRestCryptoService;
@@ -21,6 +23,13 @@ class EncryptedDatetime extends DBDatetime
2123

2224
use EncryptedFieldGetValueTrait;
2325

26+
/**
27+
* Disable validation added in CMS6 but todo in future release
28+
*/
29+
private static array $field_validators = [
30+
DatetimeFieldValidator::class => null,
31+
];
32+
2433
/**
2534
* @var AtRestCryptoService
2635
*/
@@ -29,10 +38,11 @@ class EncryptedDatetime extends DBDatetime
2938
public function __construct($name = null, $options = [])
3039
{
3140
parent::__construct($name, $options);
41+
3242
$this->service = Injector::inst()->get(AtRestCryptoService::class);
3343
}
3444

35-
public function setValue($value, $record = null, $markChanged = true)
45+
public function setValue(mixed $value, null|array|ModelData $record = null, bool $markChanged = true): static
3646
{
3747
if (is_array($record) && array_key_exists($this->name, $record) && $value === null) {
3848
$this->value = $record[$this->name];
@@ -42,6 +52,8 @@ public function setValue($value, $record = null, $markChanged = true)
4252
} else {
4353
$this->value = $value;
4454
}
55+
56+
return $this;
4557
}
4658

4759
public function getDecryptedValue(string $value = '')
@@ -58,7 +70,7 @@ public function getDecryptedValue(string $value = '')
5870
return $value;
5971
}
6072

61-
public function requireField()
73+
public function requireField(): void
6274
{
6375
$values = array(
6476
'type' => 'text',
@@ -72,7 +84,7 @@ public function requireField()
7284
DB::require_field($this->tableName, $this->name, $values);
7385
}
7486

75-
public function prepValueForDB($value)
87+
public function prepValueForDB(mixed $value): mixed
7688
{
7789
$value = parent::prepValueForDB($value);
7890
$ciphertext = $this->service->encrypt($value);

src/FieldType/EncryptedDecimal.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Exception;
66
use Madmatt\EncryptAtRest\Traits\EncryptedFieldGetValueTrait;
77
use SilverStripe\Core\Injector\Injector;
8+
use SilverStripe\Model\ModelData;
89
use SilverStripe\ORM\DB;
910
use SilverStripe\ORM\FieldType\DBDecimal;
1011
use Madmatt\EncryptAtRest\AtRestCryptoService;
@@ -31,7 +32,7 @@ public function __construct($name = null, $wholeSize = 9, $decimalSize = 2, $def
3132
$this->service = Injector::inst()->get(AtRestCryptoService::class);
3233
}
3334

34-
public function setValue($value, $record = null, $markChanged = true)
35+
public function setValue(mixed $value, null|array|ModelData $record = null, bool $markChanged = true): static
3536
{
3637
if (is_array($record) && array_key_exists($this->name, $record) && $value === null) {
3738
$this->value = $record[$this->name];
@@ -41,6 +42,8 @@ public function setValue($value, $record = null, $markChanged = true)
4142
} else {
4243
$this->value = $value;
4344
}
45+
46+
return $this;
4447
}
4548

4649
public function getDecryptedValue(string $value = '')
@@ -57,7 +60,7 @@ public function getDecryptedValue(string $value = '')
5760
return (float)$value;
5861
}
5962

60-
public function requireField()
63+
public function requireField(): void
6164
{
6265
$values = array(
6366
'type' => 'text',
@@ -71,7 +74,7 @@ public function requireField()
7174
DB::require_field($this->tableName, $this->name, $values);
7275
}
7376

74-
public function prepValueForDB($value)
77+
public function prepEncryptedValueForDB(mixed $value): string
7578
{
7679
$value = parent::prepValueForDB($value);
7780
$ciphertext = $this->service->encrypt($value);

src/FieldType/EncryptedEnum.php

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@
55
use Exception;
66
use Madmatt\EncryptAtRest\Traits\EncryptedFieldGetValueTrait;
77
use SilverStripe\Core\Injector\Injector;
8+
use SilverStripe\Core\Validation\FieldValidation\DatetimeFieldValidator;
9+
use SilverStripe\Core\Validation\FieldValidation\OptionFieldValidator;
10+
use SilverStripe\Model\ModelData;
811
use SilverStripe\ORM\DB;
912
use SilverStripe\ORM\FieldType\DBEnum;
10-
use SilverStripe\ORM\ArrayLib;
13+
use SilverStripe\Core\ArrayLib;
1114
use Madmatt\EncryptAtRest\AtRestCryptoService;
1215

1316
/**
@@ -26,13 +29,21 @@ class EncryptedEnum extends DBEnum
2629
*/
2730
protected $service;
2831

32+
/**
33+
* Disable validation added in CMS6 but todo in future release
34+
*/
35+
private static array $field_validators = [
36+
OptionFieldValidator::class => null,
37+
];
38+
2939
public function __construct($name = null, $enum = null, $default = 0, $options = [])
3040
{
3141
parent::__construct($name, $enum, $default, $options);
42+
3243
$this->service = Injector::inst()->get(AtRestCryptoService::class);
3344
}
3445

35-
public function setValue($value, $record = null, $markChanged = true)
46+
public function setValue(mixed $value, null|array|ModelData $record = null, bool $markChanged = true): static
3647
{
3748
if (is_array($record) && array_key_exists($this->name, $record) && $value === null) {
3849
$this->value = $record[$this->name];
@@ -42,6 +53,8 @@ public function setValue($value, $record = null, $markChanged = true)
4253
} else {
4354
$this->value = $value;
4455
}
56+
57+
return $this;
4558
}
4659

4760
public function getDecryptedValue(string $value = '')
@@ -58,7 +71,7 @@ public function getDecryptedValue(string $value = '')
5871
return $value;
5972
}
6073

61-
public function requireField()
74+
public function requireField(): void
6275
{
6376
$values = array(
6477
'type' => 'text',
@@ -72,26 +85,11 @@ public function requireField()
7285
DB::require_field($this->tableName, $this->name, $values);
7386
}
7487

75-
public function prepValueForDB($value)
88+
public function prepValueForDB(mixed $value): array|string|null
7689
{
7790
$value = parent::prepValueForDB($value);
7891
$ciphertext = $this->service->encrypt($value);
7992
$this->value = $ciphertext;
8093
return $ciphertext;
8194
}
82-
83-
/**
84-
* Returns the values of this enum as an array, suitable for insertion into
85-
* a {@link DropdownField}
86-
*
87-
* @param boolean
88-
*
89-
* @return array
90-
*/
91-
public function enumValues($hasEmpty = true) {
92-
$this->enum = array();
93-
return ($hasEmpty)
94-
? array_merge(array('' => ''), ArrayLib::valuekey($this->enum))
95-
: ArrayLib::valuekey($this->enum);
96-
}
9795
}

src/FieldType/EncryptedInt.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Exception;
66
use Madmatt\EncryptAtRest\Traits\EncryptedFieldGetValueTrait;
77
use SilverStripe\Core\Injector\Injector;
8+
use SilverStripe\Model\ModelData;
89
use SilverStripe\ORM\DB;
910
use SilverStripe\ORM\FieldType\DBInt;
1011
use Madmatt\EncryptAtRest\AtRestCryptoService;
@@ -31,7 +32,7 @@ public function __construct($name = null, $defaultVal = 0)
3132
$this->service = Injector::inst()->get(AtRestCryptoService::class);
3233
}
3334

34-
public function setValue($value, $record = null, $markChanged = true)
35+
public function setValue(mixed $value, null|array|ModelData $record = null, bool $markChanged = true): static
3536
{
3637
if (is_array($record) && array_key_exists($this->name, $record) && $value === null) {
3738
$this->value = $record[$this->name];
@@ -41,23 +42,25 @@ public function setValue($value, $record = null, $markChanged = true)
4142
} else {
4243
$this->value = $value;
4344
}
45+
46+
return $this;
4447
}
4548

4649
public function getDecryptedValue(string $value = '')
4750
{
4851
// Test if we're actually an encrypted value;
4952
if (ctype_xdigit($value) && strlen($value) > 130) {
5053
try {
51-
return $this->service->decrypt($value);
54+
return (int)$this->service->decrypt($value);
5255
} catch (Exception $e) {
5356
// We were unable to decrypt. Possibly a false positive, but return the unencrypted value
54-
return $value;
57+
return (int)$value;
5558
}
5659
}
57-
return $value;
60+
return (int)$value;
5861
}
5962

60-
public function requireField()
63+
public function requireField(): void
6164
{
6265
$values = array(
6366
'type' => 'text',
@@ -71,7 +74,7 @@ public function requireField()
7174
DB::require_field($this->tableName, $this->name, $values);
7275
}
7376

74-
public function prepValueForDB($value)
77+
public function prepEncryptedValueForDB(mixed $value): string
7578
{
7679
$value = parent::prepValueForDB($value);
7780
$ciphertext = $this->service->encrypt($value);

src/FieldType/EncryptedText.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Exception;
66
use Madmatt\EncryptAtRest\Traits\EncryptedFieldGetValueTrait;
77
use SilverStripe\Core\Injector\Injector;
8+
use SilverStripe\Model\ModelData;
89
use SilverStripe\ORM\DB;
910
use SilverStripe\ORM\FieldType\DBText;
1011
use Madmatt\EncryptAtRest\AtRestCryptoService;
@@ -24,7 +25,7 @@ public function __construct($name = null, $options = [])
2425
$this->service = Injector::inst()->get(AtRestCryptoService::class);
2526
}
2627

27-
public function setValue($value, $record = null, $markChanged = true)
28+
public function setValue(mixed $value, null|array|ModelData $record = null, bool $markChanged = true): static
2829
{
2930
if (is_array($record) && array_key_exists($this->name, $record) && $value === null) {
3031
$this->value = $record[$this->name];
@@ -34,6 +35,8 @@ public function setValue($value, $record = null, $markChanged = true)
3435
} else {
3536
$this->value = $value;
3637
}
38+
39+
return $this;
3740
}
3841

3942
public function getDecryptedValue(string $value = '')
@@ -50,7 +53,7 @@ public function getDecryptedValue(string $value = '')
5053
return $value;
5154
}
5255

53-
public function requireField()
56+
public function requireField(): void
5457
{
5558
$values = array(
5659
'type' => 'text',
@@ -64,7 +67,7 @@ public function requireField()
6467
DB::require_field($this->tableName, $this->name, $values);
6568
}
6669

67-
public function prepValueForDB($value)
70+
public function prepValueForDB(mixed $value): array|string|null
6871
{
6972
$value = parent::prepValueForDB($value);
7073
$ciphertext = $this->service->encrypt($value);

0 commit comments

Comments
 (0)