|
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\View\Requirements; |
|
19
|
|
|
use SilverStripe\Versioned\Versioned; |
|
20
|
|
|
use SilverStripe\ORM\DB; |
|
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
|
|
|
/** |
|
39
|
|
|
* Declares a JSON-aware {@link DBField} where all chainpoint proofs are stored. |
|
40
|
|
|
* |
|
41
|
|
|
* @var array |
|
42
|
|
|
* @config |
|
43
|
|
|
*/ |
|
44
|
|
|
private static $db = [ |
|
|
|
|
|
|
45
|
|
|
'Proof' => ChainpointProof::class, |
|
46
|
|
|
'Extra' => JSONText::class, |
|
47
|
|
|
]; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* When no `verify()` method is found on decorated objects, this is the list |
|
51
|
|
|
* of fields who's values will be hashed and committed to the current backend. |
|
52
|
|
|
* |
|
53
|
|
|
* @var array |
|
54
|
|
|
* @config |
|
55
|
|
|
*/ |
|
56
|
|
|
private static $verifiable_fields = []; |
|
|
|
|
|
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* After each publish action, userland data coming from either a custom `verify()` |
|
60
|
|
|
* method or `$verifiable_fields` config, is compiled into a string, hashed and |
|
61
|
|
|
* submitted to the current backend. |
|
62
|
|
|
* |
|
63
|
|
|
* @return void |
|
64
|
|
|
*/ |
|
65
|
|
|
public function onAfterPublish() |
|
66
|
|
|
{ |
|
67
|
|
|
$verifiable = $this->source(); |
|
68
|
|
|
$owner = $this->getOwner(); |
|
69
|
|
|
$this->service->setExtra(); |
|
|
|
|
|
|
70
|
|
|
$doAnchor = (count($verifiable) && $owner->exists()); |
|
71
|
|
|
|
|
72
|
|
|
if ($doAnchor && $proofData = $this->service->call('write', $verifiable)) { |
|
73
|
|
|
if (is_array($proofData)) { |
|
74
|
|
|
$proofData = json_encode($proofData); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
// Update the versions table manually to avoid double publish problem |
|
78
|
|
|
// when a DO is marked as "changed" |
|
79
|
|
|
$latest = Versioned::get_latest_version(get_class($owner), $owner->ID); |
|
80
|
|
|
$table = sprintf('%s_Versions', $latest->config()->get('table_name')); |
|
81
|
|
|
DB::query(sprintf( |
|
82
|
|
|
'UPDATE "%s" SET "Proof" = \'%s\', Extra = \'%s\' WHERE "RecordID" = %d AND "Version" = %d', |
|
83
|
|
|
$table, |
|
84
|
|
|
$proofData, |
|
85
|
|
|
json_encode($this->service->getExtra()), |
|
86
|
|
|
$latest->ID, |
|
87
|
|
|
$latest->Version |
|
88
|
|
|
)); |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Source the data that will end-up hashed and submitted. This method will |
|
94
|
|
|
* call a custom verify() method on all decorated objects, if one is defined. |
|
95
|
|
|
* This provides a flexible public API for hashing and verifying pretty much |
|
96
|
|
|
* anything. |
|
97
|
|
|
* |
|
98
|
|
|
* If no such method exists, the default is to take the values of the YML |
|
99
|
|
|
* config "verifiable_fields" array, then hash and submit the values of those |
|
100
|
|
|
* fields. If no verifiable_fields are found or configured, we just return |
|
101
|
|
|
* an empty array and just stop. |
|
102
|
|
|
* |
|
103
|
|
|
* @param DataObject $record |
|
|
|
|
|
|
104
|
|
|
* @return array |
|
105
|
|
|
*/ |
|
106
|
|
|
public function source($record = null) : array |
|
107
|
|
|
{ |
|
108
|
|
|
$record = $record ?: $this->getOwner(); |
|
109
|
|
|
$verifiable = []; |
|
110
|
|
|
|
|
111
|
|
|
if (method_exists($record, 'verify')) { |
|
112
|
|
|
$verifiable = (array) $record->verify(); |
|
113
|
|
|
} else { |
|
114
|
|
|
$fields = $record->config()->get('verifiable_fields'); |
|
115
|
|
|
|
|
116
|
|
|
foreach ($fields as $field) { |
|
117
|
|
|
if ($field === 'Proof') { |
|
118
|
|
|
continue; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
$verifiable[] = (string) $record->getField($field); |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
return $verifiable; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Adds a "Verification" tab to the CMS. |
|
130
|
|
|
* |
|
131
|
|
|
* @param FieldList $fields |
|
132
|
|
|
* @return void |
|
133
|
|
|
*/ |
|
134
|
|
|
public function updateCMSFields(FieldList $fields) |
|
135
|
|
|
{ |
|
136
|
|
|
parent::updateCMSFields($fields); |
|
137
|
|
|
|
|
138
|
|
|
Requirements::javascript('phptek/verifiable: client/verifiable.js'); |
|
139
|
|
|
|
|
140
|
|
|
$owner = $this->getOwner(); |
|
141
|
|
|
$list = $disabled = []; |
|
142
|
|
|
$versions = $owner->Versions()->sort('Version'); |
|
143
|
|
|
|
|
144
|
|
|
foreach ($versions as $item) { |
|
145
|
|
|
if ($item->Version == 1) { |
|
146
|
|
|
$disabled[] = $item->Version; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
$list[$item->Version] = sprintf('Version: %s (Created: %s)', $item->Version, $item->Created); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
$fields->addFieldsToTab('Root.Verify', FieldList::create([ |
|
153
|
|
|
LiteralField::create('Introduction', '<p class="message">Select a version' |
|
154
|
|
|
. ' whose data you wish to verify, then select the "Verify"' |
|
155
|
|
|
. ' button. After a few seconds, a verification status will be' |
|
156
|
|
|
. ' displayed.</p>'), |
|
157
|
|
|
HiddenField::create('Type', null, get_class($owner)), |
|
158
|
|
|
DropdownField::create('Version', 'Version', $list) |
|
159
|
|
|
->setEmptyString('-- Select One --') |
|
160
|
|
|
->setDisabledItems($disabled), |
|
161
|
|
|
FormAction::create('doVerify', 'Verify') |
|
162
|
|
|
->setUseButtonTag(true) |
|
163
|
|
|
->addExtraClass('btn action btn-outline-primary ') |
|
164
|
|
|
])); |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* Get the contents of this model's "Extra" field by numeric index. |
|
169
|
|
|
* |
|
170
|
|
|
* @param int $num |
|
171
|
|
|
* @return mixed array | int |
|
172
|
|
|
*/ |
|
173
|
|
|
public function getExtraByIndex(int $num = null) |
|
174
|
|
|
{ |
|
175
|
|
|
$extra = $this->getOwner()->dbObject('Extra'); |
|
176
|
|
|
$extra->setReturnType('array'); |
|
177
|
|
|
|
|
178
|
|
|
if (!$num) { |
|
|
|
|
|
|
179
|
|
|
return $extra->getStoreAsArray(); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
if (!empty($value = $extra->nth($num))) { |
|
183
|
|
|
return is_array($value) ? $value[0] : $value; // <-- stuuupId. Needs fixing in JSONText |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
return []; |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
} |
|
190
|
|
|
|