1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @author Russell Michell 2018 <[email protected]> |
5
|
|
|
* @package silverstripe-verifiable |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace PhpTek\Verifiable\Verify; |
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
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* By attaching this extension to any {@link DataObject} subclass and declaring a |
22
|
|
|
* $verifiable_fields array in YML config, all subsequent database writes will |
23
|
|
|
* be passed through here via {@link $this->onBeforeWrite()}; |
24
|
|
|
* |
25
|
|
|
* This {@link DataExtension} also provides a single field to which all verified |
26
|
|
|
* and verifiable chainpoint proofs are stored in a queryable JSON-aware field. |
27
|
|
|
*/ |
28
|
|
|
class VerifiableExtension extends DataExtension |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* Declares a JSON-aware {@link DBField} where all chainpoint proofs are stored. |
32
|
|
|
* |
33
|
|
|
* @var array |
34
|
|
|
* @config |
35
|
|
|
*/ |
36
|
|
|
private static $db = [ |
|
|
|
|
37
|
|
|
'Proof' => ChainpointProof::class, |
38
|
|
|
'Extra' => JSONText::class, |
39
|
|
|
]; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* The field-values that will be hashed and committed to the current backend. |
43
|
|
|
* |
44
|
|
|
* @var array |
45
|
|
|
* @config |
46
|
|
|
*/ |
47
|
|
|
private static $verifiable_fields = []; |
|
|
|
|
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Before each write, data from the $verifiable_fields config is compiled |
51
|
|
|
* into a string, hashed and submitted to the current backend. |
52
|
|
|
* |
53
|
|
|
* @return void |
54
|
|
|
* @todo Implement in onAfterPublish() instead. Minimises HTTP requests to the backend. |
55
|
|
|
*/ |
56
|
|
|
public function onBeforeWrite() |
57
|
|
|
{ |
58
|
|
|
parent::onBeforeWrite(); |
59
|
|
|
|
60
|
|
|
$verifiable = $this->verify(); |
61
|
|
|
$owner = $this->getOwner(); |
62
|
|
|
$this->verifiableService->setExtra(); |
|
|
|
|
63
|
|
|
|
64
|
|
|
if (count($verifiable) && $proofData = $this->verifiableService->call('write', $verifiable)) { |
65
|
|
|
if (is_array($proofData)) { |
66
|
|
|
$proofData = json_encode($proofData); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$owner->setField('Proof', $proofData); |
70
|
|
|
$owner->setField('Extra', json_encode($this->verifiableService->getExtra())); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Call a custom verify() method on all decorated objects, if one exists. |
76
|
|
|
* This provides a flexible public API for hashing and verifying pretty much |
77
|
|
|
* anything. |
78
|
|
|
* |
79
|
|
|
* If no verify method exists, the default is to take the values of the YML |
80
|
|
|
* config "verifiable_fields" array, then hash and submit the values of those |
81
|
|
|
* fields. If no verifiable_fields are found or configured, we just return |
82
|
|
|
* an empty array and stop. |
83
|
|
|
* |
84
|
|
|
* @param DataObject $record |
|
|
|
|
85
|
|
|
* @return array |
86
|
|
|
*/ |
87
|
|
|
public function verify($record = null) : array |
88
|
|
|
{ |
89
|
|
|
$record = $record ?: $this->getOwner(); |
90
|
|
|
$verifiable = []; |
91
|
|
|
|
92
|
|
|
if (method_exists($record, 'verify')) { |
93
|
|
|
$verifiable = (array) $record->verify(); |
94
|
|
|
} else { |
95
|
|
|
$fields = $record->config()->get('verifiable_fields'); |
96
|
|
|
|
97
|
|
|
foreach ($fields as $field) { |
98
|
|
|
if ($field === 'Proof') { |
99
|
|
|
continue; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
$verifiable[] = (string) $record->getField($field); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return $verifiable; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Adds a "Verification" tab to the CMS. |
111
|
|
|
* |
112
|
|
|
* @param FieldList $fields |
113
|
|
|
* @return void |
114
|
|
|
*/ |
115
|
|
|
public function updateCMSFields(FieldList $fields) |
116
|
|
|
{ |
117
|
|
|
parent::updateCMSFields($fields); |
118
|
|
|
|
119
|
|
|
Requirements::javascript('phptek/verifiable: client/verifiable.js'); |
120
|
|
|
|
121
|
|
|
$owner = $this->getOwner(); |
122
|
|
|
$list = []; |
123
|
|
|
$versions = $owner->Versions()->sort('Version'); |
124
|
|
|
|
125
|
|
|
foreach ($versions as $item) { |
126
|
|
|
$list[$item->Version] = sprintf('Version: %s (Created: %s)', $item->Version, $item->Created); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
$fields->addFieldsToTab('Root.Verify', FieldList::create([ |
130
|
|
|
LiteralField::create('Introduction', '<p class="message">Select a version' |
131
|
|
|
. ' whose data you wish to verify, then select the "Verify"' |
132
|
|
|
. ' button. After a few seconds, a verification status will be' |
133
|
|
|
. ' displayed.</p>'), |
134
|
|
|
HiddenField::create('Type', null, get_class($owner)), |
135
|
|
|
DropdownField::create('Version', 'Version', $list) |
136
|
|
|
->setEmptyString('-- Select One --'), |
137
|
|
|
FormAction::create('doVerify', 'Verify') |
138
|
|
|
->setUseButtonTag(true) |
139
|
|
|
->addExtraClass('btn action btn-outline-primary ') |
140
|
|
|
])); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Get the contents of this model's "Extra" field by numeric index. |
145
|
|
|
* |
146
|
|
|
* @param int $num |
147
|
|
|
* @return mixed array | int |
148
|
|
|
*/ |
149
|
|
|
public function getExtraByIndex(int $num = null) |
150
|
|
|
{ |
151
|
|
|
$extra = $this->getOwner()->dbObject('Extra'); |
152
|
|
|
$extra->setReturnType('array'); |
153
|
|
|
|
154
|
|
|
if (!$num) { |
|
|
|
|
155
|
|
|
return $extra->getStoreAsArray(); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
if (!empty($value = $extra->nth($num))) { |
159
|
|
|
return is_array($value) ? $value[0] : $value; // <-- stuuupId. Needs fixing in JSONText |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
return []; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
} |
166
|
|
|
|