forked from UseMuffin/Obfuscate
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObfuscateBehavior.php
More file actions
191 lines (170 loc) · 5.2 KB
/
ObfuscateBehavior.php
File metadata and controls
191 lines (170 loc) · 5.2 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
<?php
declare(strict_types=1);
namespace Muffin\Obfuscate\Model\Behavior;
use ArrayObject;
use Cake\Core\Exception\Exception;
use Cake\Database\Expression\Comparison;
use Cake\Datasource\EntityInterface;
use Cake\Event\EventInterface;
use Cake\ORM\Behavior;
use Cake\ORM\Query;
use Muffin\Obfuscate\Model\Behavior\Strategy\StrategyInterface;
use RuntimeException;
/**
* Class ObfuscateBehavior
*
*/
class ObfuscateBehavior extends Behavior
{
/**
* {@inheritdoc}
*/
protected $_defaultConfig = [
'strategy' => null,
'implementedFinders' => [
'obfuscated' => 'findObfuscated',
'obfuscate' => 'findObfuscate',
],
'implementedMethods' => [
'obfuscate' => 'obfuscate',
'elucidate' => 'elucidate',
],
];
/**
* Initialize behavior
*
* @param array $config Behavior's configuration.
* @return void
*/
public function initialize(array $config): void
{
$this->verifyConfig();
}
/**
* Verify config.
*
* @return void
*/
public function verifyConfig(): void
{
$strategy = $this->getConfig('strategy');
if (empty($strategy)) {
throw new Exception('Missing required obfuscation strategy.');
}
if (!($strategy instanceof StrategyInterface)) {
throw new Exception(
'Strategy must implement the `Muffin\Obfuscate\Model\Behavior\Strategy\StrategyInterface`'
);
}
parent::verifyConfig();
}
/**
* Callback to obfuscate the record(s)' primary key returned after a save operation.
*
* @param \Cake\Event\EventInterface $event EventInterface.
* @param \Cake\Datasource\EntityInterface $entity Entity.
* @param \ArrayObject $options Options.
* @return void
*/
public function afterSave(EventInterface $event, EntityInterface $entity, ArrayObject $options)
{
$pk = $this->_table->getPrimaryKey();
if (is_array($pk)) {
throw new RuntimeException('Composite primary keys are not supported.');
}
$entity->set($pk, $this->obfuscate($entity->{$pk}));
$entity->setDirty($pk, false);
}
/**
* Callback to set the `obfuscated` finder on all associations.
*
* @param \Cake\Event\EventInterface $event EventInterface.
* @param \Cake\ORM\Query $query Query.
* @param \ArrayObject $options Options.
* @param bool $primary True if this is the primary table.
* @return void
*/
public function beforeFind(EventInterface $event, Query $query, ArrayObject $options, $primary)
{
if (empty($options['obfuscate']) || !$primary) {
return;
}
$query->traverseExpressions(function ($expression) {
$pk = $this->_table->getPrimaryKey();
if (is_array($pk)) {
throw new RuntimeException('Composite primary keys are not supported.');
}
if (
$expression instanceof Comparison
&& in_array($expression->getField(), [$pk, $this->_table->aliasField($pk)])
) {
$expression->setValue($this->elucidate($expression->getValue()));
}
return $expression;
});
foreach ($this->_table->associations() as $association) {
if ($association->getTarget()->hasBehavior('Obfuscate') && $association->getFinder() === 'all') {
$association->setFinder('obfuscate');
}
}
}
/**
* Custom finder to search for records using an obfuscated primary key.
*
* @param \Cake\ORM\Query $query Query.
* @param array $options Options.
* @return \Cake\ORM\Query
*/
public function findObfuscated(Query $query, array $options)
{
return $query->applyOptions(['obfuscate' => true]);
}
/**
* Custom finder that obfuscates primary keys in returned result set.
*
* @param \Cake\ORM\Query $query Query.
* @param array $options Options.
* @return \Cake\ORM\Query
*/
public function findObfuscate(Query $query, array $options)
{
$query->applyOptions(['obfuscate' => true]);
$query->formatResults(function ($results) {
return $results->map(function ($row) {
$pk = $this->_table->getPrimaryKey();
$row[$pk] = $this->obfuscate($row[$pk]);
return $row;
});
});
return $query;
}
/**
* Proxy to the obfuscating strategy's `obfuscate()`.
*
* @param string $str String to obfuscate.
* @return string
*/
public function obfuscate($str)
{
return $this->strategy()->obfuscate($str);
}
/**
* Proxy to the obfuscating strategy's `elucidate()`.
*
* @param string $str String to elucidate.
* @return string
*/
public function elucidate($str)
{
return $this->strategy()->elucidate($str);
}
/**
* Get the configured strategy.
*
* @return \Muffin\Obfuscate\Model\Behavior\Strategy\StrategyInterface
*/
public function strategy()
{
return $this->getConfig('strategy');
}
}