|
1
|
|
|
<?php |
|
2
|
|
|
namespace Carnage\EncryptedColumn\Dbal; |
|
3
|
|
|
|
|
4
|
|
|
use Carnage\EncryptedColumn\Service\EncryptionService; |
|
5
|
|
|
use Carnage\EncryptedColumn\ValueObject\ValueHolder; |
|
6
|
|
|
use Doctrine\DBAL\Platforms\AbstractPlatform; |
|
7
|
|
|
use Doctrine\DBAL\Types\ConversionException; |
|
8
|
|
|
use Doctrine\DBAL\Types\Type; |
|
9
|
|
|
use Carnage\EncryptedColumn\ValueObject\EncryptedColumn as EncryptedColumnVO; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Object type for reading from legacy encrypted column extensions and converting to a better format |
|
13
|
|
|
* |
|
14
|
|
|
* This type is a drop in replacement for the EncryptedColumn class but drops some strictness to allow for |
|
15
|
|
|
* reading data that is not in an expected format. You should only use this if you have existing data in |
|
16
|
|
|
* your database you wish to convert |
|
17
|
|
|
* |
|
18
|
|
|
* Class EncryptedColumn |
|
19
|
|
|
*/ |
|
20
|
|
|
class EncryptedColumnLegacySupport extends Type |
|
21
|
|
|
{ |
|
22
|
|
|
const ENCRYPTED = 'encrypted'; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var EncryptionService |
|
26
|
|
|
*/ |
|
27
|
|
|
private $encryptionService; |
|
28
|
|
|
|
|
29
|
1 |
|
public static function create(EncryptionService $encryptionService) |
|
30
|
|
|
{ |
|
31
|
1 |
|
Type::addType(EncryptedColumnLegacySupport::ENCRYPTED, EncryptedColumnLegacySupport::class); |
|
32
|
|
|
/** @var EncryptedColumn $instance */ |
|
33
|
1 |
|
$instance = Type::getType(EncryptedColumnLegacySupport::ENCRYPTED); |
|
34
|
1 |
|
$instance->encryptionService = $encryptionService; |
|
|
|
|
|
|
35
|
1 |
|
} |
|
36
|
|
|
|
|
37
|
1 |
|
public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform) |
|
38
|
|
|
{ |
|
39
|
1 |
|
return $platform->getClobTypeDeclarationSQL($fieldDeclaration); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
1 |
|
public function requiresSQLCommentHint(AbstractPlatform $platform) |
|
43
|
|
|
{ |
|
44
|
1 |
|
return true; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
1 |
|
public function getName() |
|
48
|
|
|
{ |
|
49
|
1 |
|
return self::ENCRYPTED; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
2 |
|
public function convertToPHPValue($value, AbstractPlatform $platform) |
|
53
|
|
|
{ |
|
54
|
2 |
|
if ($value === null) { |
|
55
|
|
|
return null; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
try { |
|
59
|
2 |
|
$decoded = $this->decodeJson($value); |
|
60
|
1 |
|
} catch (ConversionException $e) { |
|
61
|
|
|
//The data wasn't in the format we expected, assume it is legacy data which needs converting |
|
62
|
|
|
//Drop in some defaults to allow the library to handle it. |
|
63
|
|
|
$decoded = [ |
|
64
|
1 |
|
'data' => $value, |
|
65
|
|
|
'classname' => ValueHolder::class, |
|
66
|
1 |
|
'serializer' => 'legacy', |
|
67
|
1 |
|
'encryptor' => 'legacy' |
|
68
|
|
|
]; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
2 |
|
return $this->encryptionService->decryptField(EncryptedColumnVO::fromArray($decoded)); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
1 |
View Code Duplication |
public function convertToDatabaseValue($value, AbstractPlatform $platform) |
|
|
|
|
|
|
75
|
|
|
{ |
|
76
|
1 |
|
if ($value === null) { |
|
77
|
|
|
return null; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
1 |
|
return json_encode($this->encryptionService->encryptField($value)); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Based on: https://github.com/schmittjoh/serializer/blob/master/src/JMS/Serializer/JsonDeserializationVisitor.php |
|
85
|
|
|
* |
|
86
|
|
|
* @param $value |
|
87
|
|
|
* @return mixed |
|
88
|
|
|
* @throws ConversionException |
|
89
|
|
|
*/ |
|
90
|
2 |
View Code Duplication |
private function decodeJson($value) |
|
|
|
|
|
|
91
|
|
|
{ |
|
92
|
2 |
|
$decoded = json_decode($value, true); |
|
93
|
|
|
|
|
94
|
2 |
|
switch (json_last_error()) { |
|
95
|
2 |
|
case JSON_ERROR_NONE: |
|
96
|
1 |
|
if (!is_array($decoded)) { |
|
97
|
|
|
throw ConversionException::conversionFailed($value, 'Json was not an array'); |
|
98
|
|
|
} |
|
99
|
1 |
|
return $decoded; |
|
100
|
1 |
|
case JSON_ERROR_DEPTH: |
|
101
|
|
|
throw ConversionException::conversionFailed($value, 'Could not decode JSON, maximum stack depth exceeded.'); |
|
102
|
1 |
|
case JSON_ERROR_STATE_MISMATCH: |
|
103
|
|
|
throw ConversionException::conversionFailed($value, 'Could not decode JSON, underflow or the nodes mismatch.'); |
|
104
|
1 |
|
case JSON_ERROR_CTRL_CHAR: |
|
105
|
|
|
throw ConversionException::conversionFailed($value, 'Could not decode JSON, unexpected control character found.'); |
|
106
|
1 |
|
case JSON_ERROR_SYNTAX: |
|
107
|
1 |
|
throw ConversionException::conversionFailed($value, 'Could not decode JSON, syntax error - malformed JSON.'); |
|
108
|
|
|
case JSON_ERROR_UTF8: |
|
109
|
|
|
throw ConversionException::conversionFailed($value, 'Could not decode JSON, malformed UTF-8 characters (incorrectly encoded?)'); |
|
110
|
|
|
default: |
|
111
|
|
|
throw ConversionException::conversionFailed($value, 'Could not decode Json'); |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|
This check looks for access to properties that are not accessible from the current context.
If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.