1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @author Russell Michell 2018 <[email protected]> |
5
|
|
|
* @package silverstripe-verifiable |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace PhpTek\Verifiable\Model; |
9
|
|
|
|
10
|
|
|
use SilverStripe\ORM\DataExtension; |
11
|
|
|
use PhpTek\Verifiable\ORM\Fieldtype\ChainpointProof; |
12
|
|
|
use SilverStripe\Forms\FieldList; |
13
|
|
|
use SilverStripe\Forms\FormAction; |
14
|
|
|
use SilverStripe\Forms\LiteralField; |
15
|
|
|
use SilverStripe\Forms\DropdownField; |
16
|
|
|
use SilverStripe\Forms\HiddenField; |
17
|
|
|
use PhpTek\JSONText\ORM\FieldType\JSONText; |
18
|
|
|
use SilverStripe\Versioned\Versioned; |
19
|
|
|
use SilverStripe\ORM\DB; |
20
|
|
|
use SilverStripe\ORM\DataObject; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* By attaching this extension to any {@link DataObject} subclass, it will therefore |
24
|
|
|
* be "verifiable aware". Declaring a `verify()` method on it, will automatically |
25
|
|
|
* make whatever the method returns, into that which is hashed and anchored to |
26
|
|
|
* the backend. |
27
|
|
|
* |
28
|
|
|
* If no `verify()` method is detected, the fallback is to assume that selected |
29
|
|
|
* fields on your data model should be combined and hashed. For this to work, |
30
|
|
|
* declare a `verifiable_fields` array in YML config. All subsequent publish actions |
31
|
|
|
* will be passed through here via {@link $this->onBeforeWrite()}. |
32
|
|
|
* |
33
|
|
|
* This {@link DataExtension} also provides a single field to which all verified |
34
|
|
|
* and verifiable chainpoint proofs are stored in a queryable JSON-aware field. |
35
|
|
|
*/ |
36
|
|
|
class VerifiableExtension extends DataExtension |
37
|
|
|
{ |
38
|
|
|
// This data-model is using the default "verifiable_fields" mode |
39
|
|
|
const SOURCE_MODE_FIELD = 1; |
40
|
|
|
// This data-model is using the custom "verify" function mode |
41
|
|
|
const SOURCE_MODE_FUNC = 2; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Declares a JSON-aware {@link DBField} where all chainpoint proofs are stored. |
45
|
|
|
* |
46
|
|
|
* @var array |
47
|
|
|
* @config |
48
|
|
|
*/ |
49
|
|
|
private static $db = [ |
|
|
|
|
50
|
|
|
'Proof' => ChainpointProof::class, |
51
|
|
|
'Extra' => JSONText::class, |
52
|
|
|
'VerifiableFields' => JSONText::class, |
53
|
|
|
]; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* When no `verify()` method is found on decorated objects, this is the list |
57
|
|
|
* of fields who's values will be hashed and committed to the current backend. |
58
|
|
|
* |
59
|
|
|
* @var array |
60
|
|
|
* @config |
61
|
|
|
*/ |
62
|
|
|
private static $verifiable_fields = []; |
|
|
|
|
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Which source mode are we using? |
66
|
|
|
* |
67
|
|
|
* @return int |
68
|
|
|
*/ |
69
|
|
|
public function sourceMode() |
70
|
|
|
{ |
71
|
|
|
if (method_exists($this->getOwner(), 'verify')) { |
72
|
|
|
return self::SOURCE_MODE_FUNC; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return self::SOURCE_MODE_FIELD; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* After each publish action, userland data coming from either a custom `verify()` |
80
|
|
|
* method or `$verifiable_fields` config, is compiled into a string, hashed and |
81
|
|
|
* submitted to the current backend. |
82
|
|
|
* |
83
|
|
|
* Note: We update the versions table manually to avoid double publish problem |
84
|
|
|
* where a DO is marked internally as "changed". |
85
|
|
|
* |
86
|
|
|
* @return void |
87
|
|
|
*/ |
88
|
|
|
public function onAfterPublish() |
89
|
|
|
{ |
90
|
|
|
$owner = $this->getOwner(); |
91
|
|
|
$latest = Versioned::get_latest_version(get_class($owner), $owner->ID); |
92
|
|
|
$table = sprintf('%s_Versions', $latest->baseTable()); |
93
|
|
|
|
94
|
|
|
// Save the verifiable_fields to the xxx_Versioned table _before_ calling |
95
|
|
|
// source() which itself, makes use of this data |
96
|
|
|
DB::query(sprintf('' |
97
|
|
|
. ' UPDATE "%s"' |
98
|
|
|
. ' SET "VerifiableFields" = \'%s\'' |
99
|
|
|
. ' WHERE "RecordID" = %d AND "Version" = %d', |
100
|
|
|
$table, |
101
|
|
|
json_encode($owner->config()->get('verifiable_fields')), |
102
|
|
|
$latest->ID, |
103
|
|
|
$latest->Version |
104
|
|
|
)); |
105
|
|
|
|
106
|
|
|
$this->service->setExtra(); |
|
|
|
|
107
|
|
|
$verifiable = $this->source(); |
108
|
|
|
$doAnchor = (count($verifiable) && $owner->exists()); |
109
|
|
|
|
110
|
|
|
if ($doAnchor && $proofData = $this->service->call('write', $verifiable)) { |
111
|
|
|
if (is_array($proofData)) { |
112
|
|
|
$proofData = json_encode($proofData); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
DB::query(sprintf('' |
116
|
|
|
. ' UPDATE "%s"' |
117
|
|
|
. ' SET "Proof" = \'%s\',' |
118
|
|
|
. ' "Extra" = \'%s\'' |
119
|
|
|
. ' WHERE "RecordID" = %d AND "Version" = %d', |
120
|
|
|
$table, |
121
|
|
|
$proofData, |
122
|
|
|
json_encode($this->service->getExtra()), |
123
|
|
|
$latest->ID, |
124
|
|
|
$latest->Version |
125
|
|
|
)); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Source the data that will end-up hashed and submitted. This method will |
131
|
|
|
* call a custom verify() method on all decorated objects if one is defined. |
132
|
|
|
* This provides a flexible public API for hashing and verifying pretty much |
133
|
|
|
* anything. But if no such method exists, the default is to take the value |
134
|
|
|
* of the YML config "verifiable_fields" array, hash and submit the values |
135
|
|
|
* of those DB fields. If no verifiable_fields are found or configured, |
136
|
|
|
* we just return an empty array and just stop. |
137
|
|
|
* |
138
|
|
|
* @param DataObject $record |
139
|
|
|
* @return array |
140
|
|
|
*/ |
141
|
|
|
public function source($record = null) : array |
142
|
|
|
{ |
143
|
|
|
$record = $record ?: $this->getOwner(); |
144
|
|
|
$verifiable = []; |
145
|
|
|
|
146
|
|
|
if ($this->sourceMode() === self::SOURCE_MODE_FUNC) { |
147
|
|
|
$verifiable = (array) $record->verify(); |
148
|
|
|
} else { |
149
|
|
|
// If the "VerifiableFields" DB field is not empty, it contains a cached |
150
|
|
|
// list of the field-names who's content should be sent for hashing. |
151
|
|
|
// This means the list of fields to be verified is now _relative_ to |
152
|
|
|
// the current version, thus any change made to YML config, will only |
153
|
|
|
// affect versions created _after_ that change. |
154
|
|
|
$verifiableFields = $record->config()->get('verifiable_fields'); |
155
|
|
|
|
156
|
|
|
if ($cachedFields = $record->dbObject('VerifiableFields')->getStoreAsArray()) { |
157
|
|
|
$verifiableFields = $cachedFields; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
foreach ($verifiableFields as $field) { |
161
|
|
|
if ($field === 'Proof') { |
162
|
|
|
continue; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
$verifiable[] = strip_tags((string) $record->getField($field)); |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
return $verifiable; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Adds a "Verification" tab to {@link SiteTree} objects in the framework UI. |
174
|
|
|
* |
175
|
|
|
* @param FieldList $fields |
176
|
|
|
* @return void |
177
|
|
|
*/ |
178
|
|
|
public function updateCMSFields(FieldList $fields) |
179
|
|
|
{ |
180
|
|
|
parent::updateCMSFields($fields); |
181
|
|
|
|
182
|
|
|
$this->updateAdminForm($fields); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Adds a "Verification" tab to {@link File} objects in the framework UI. |
187
|
|
|
* |
188
|
|
|
* @param FieldList $fields |
189
|
|
|
* @return void |
190
|
|
|
*/ |
191
|
|
|
public function updateFormFields(FieldList $fields, $controller, $formName, $record) |
|
|
|
|
192
|
|
|
{ |
193
|
|
|
$this->updateAdminForm($fields, $record); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Get the contents of this model's "Extra" field by numeric index. |
198
|
|
|
* |
199
|
|
|
* @param int $num |
200
|
|
|
* @return mixed array | int |
201
|
|
|
*/ |
202
|
|
|
public function getExtraByIndex(int $num = null) |
203
|
|
|
{ |
204
|
|
|
$extra = $this->getOwner()->dbObject('Extra'); |
205
|
|
|
$extra->setReturnType('array'); |
206
|
|
|
|
207
|
|
|
if (!$num) { |
|
|
|
|
208
|
|
|
return $extra->getStoreAsArray(); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
if (!empty($value = $extra->nth($num))) { |
212
|
|
|
return is_array($value) ? $value[0] : $value; // <-- stuuupId. Needs fixing in JSONText |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
return []; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* @param FieldList $fields |
220
|
|
|
* @param array $record If passed, the data-model is likely a {@link File} |
221
|
|
|
* subclass, meaning that $this->getOwner() is not |
222
|
|
|
* going to be a {@link DataObject} subclass. |
223
|
|
|
* @return void |
224
|
|
|
*/ |
225
|
|
|
private function updateAdminForm(FieldList $fields, array $record = null) |
226
|
|
|
{ |
227
|
|
|
$owner = $record ? $record['Record'] : $this->getOwner(); |
228
|
|
|
$tabRootName = $record ? 'Editor' : 'Root'; |
229
|
|
|
$list = $disabled = []; |
230
|
|
|
$versions = $owner->Versions()->sort('Version'); |
231
|
|
|
|
232
|
|
|
// Build the menu of versioned objects |
233
|
|
|
foreach ($versions as $item) { |
234
|
|
|
if ($item->Version == 1) { |
235
|
|
|
$disabled[] = $item->Version; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
$list[$item->Version] = sprintf('Version: %s (Created: %s)', $item->Version, $item->Created); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
$fields->addFieldsToTab($tabRootName . '.Verify', FieldList::create([ |
242
|
|
|
LiteralField::create('Introduction', '<p class="message intro">Select a version' |
243
|
|
|
. ' whose data you wish to verify, then select the "Verify"' |
244
|
|
|
. ' button. After a few seconds, a verification status will be' |
245
|
|
|
. ' displayed.</p>'), |
246
|
|
|
HiddenField::create('Type', null, get_class($owner)), |
247
|
|
|
DropdownField::create('Version', 'Version', $list) |
248
|
|
|
->setEmptyString('-- Select One --') |
249
|
|
|
->setDisabledItems($disabled), |
250
|
|
|
FormAction::create('doVerify', 'Verify') |
251
|
|
|
->setUseButtonTag(true) |
252
|
|
|
->addExtraClass('btn action btn-outline-primary ') |
253
|
|
|
])); |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
} |
257
|
|
|
|