|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @author Russell Michell 2018 <[email protected]> |
|
5
|
|
|
* @package silverstripe-verifiable |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace PhpTek\Verifiable\Controller; |
|
9
|
|
|
|
|
10
|
|
|
use SilverStripe\Control\Controller; |
|
11
|
|
|
use SilverStripe\Control\HTTPRequest; |
|
12
|
|
|
use SilverStripe\Versioned\Versioned; |
|
13
|
|
|
use SilverStripe\CMS\Model\SiteTree; |
|
|
|
|
|
|
14
|
|
|
use SilverStripe\ORM\DataObject; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Accepts incoming requests for data verification e.g. from within the CMS |
|
18
|
|
|
* or framework's admin area, and sends them on their way. |
|
19
|
|
|
* |
|
20
|
|
|
* Will proxy validation requests to the currently configured backend for both |
|
21
|
|
|
* {@link SiteTree} and {@link DataObject} subclasses. |
|
22
|
|
|
*/ |
|
23
|
|
|
class VerificationController extends Controller |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* @config |
|
27
|
|
|
* @var array |
|
28
|
|
|
*/ |
|
29
|
|
|
private static $allowed_actions = [ |
|
|
|
|
|
|
30
|
|
|
'page', |
|
31
|
|
|
'model', |
|
32
|
|
|
]; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Verify a page: /verify/page/<ID> by echoing a JSON response for |
|
36
|
|
|
* consumption by client-side logic. |
|
37
|
|
|
* |
|
38
|
|
|
* @param HTTPRequest $request |
|
39
|
|
|
* @return void |
|
40
|
|
|
*/ |
|
41
|
|
|
public function page(HTTPRequest $request) |
|
42
|
|
|
{ |
|
43
|
|
|
$id = $request->param('ID'); |
|
44
|
|
|
|
|
45
|
|
|
if (empty($id) || !is_numeric($id)) { |
|
46
|
|
|
return $this->httpError(400, 'Bad request'); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
if (!$record = $this->getVersionedRecord(SiteTree::class, $id)) { |
|
|
|
|
|
|
50
|
|
|
return false; |
|
|
|
|
|
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
$proof = $record->dbObject('Proof'); |
|
54
|
|
|
$result = json_decode($this->verifiableService->read($proof->getHashIdNode()), true); |
|
|
|
|
|
|
55
|
|
|
|
|
56
|
|
|
echo $this->verificationResponse($record, $result); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Verify a data model: /verify/model/<Class>/<ID> by echoing a JSON response for |
|
61
|
|
|
* consumption by client-side logic. |
|
62
|
|
|
* |
|
63
|
|
|
* @param HTTPRequest $request |
|
64
|
|
|
* @return void |
|
65
|
|
|
*/ |
|
66
|
|
|
public function model(HTTPRequest $request) |
|
67
|
|
|
{ |
|
68
|
|
|
$class = $request->param('Class'); |
|
69
|
|
|
$id = $request->param('ID'); |
|
70
|
|
|
|
|
71
|
|
|
if (empty($id) || !is_numeric($id) || empty($class)) { |
|
72
|
|
|
return $this->httpError(400, 'Bad request'); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
if (!$record = $this->getVersionedRecord($class, $id)) { |
|
|
|
|
|
|
76
|
|
|
return false; |
|
|
|
|
|
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
$proof = $record->dbObject('Proof'); |
|
80
|
|
|
$result = json_decode($this->verifiableService->read($proof->getHashIdNode()), true); |
|
|
|
|
|
|
81
|
|
|
|
|
82
|
|
|
echo $this->verificationResponse($record, $result); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Fetch a record directly from the relevant table, for the given class |
|
87
|
|
|
* and ID. |
|
88
|
|
|
* |
|
89
|
|
|
* @param string $class A fully-qualified PHP class name. |
|
90
|
|
|
* @param int $id The RecordID of the desired Versioned record. |
|
91
|
|
|
* @return DataObject |
|
92
|
|
|
*/ |
|
93
|
|
|
private function getVersionedRecord(string $class, int $id) : DataObject |
|
94
|
|
|
{ |
|
95
|
|
|
return Versioned::get_latest_version($class, $id); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Return an JSON representation of the verification result for internal |
|
100
|
|
|
* use. |
|
101
|
|
|
* |
|
102
|
|
|
* @param DataObject $record |
|
103
|
|
|
* @param string $result |
|
104
|
|
|
* @return string |
|
105
|
|
|
*/ |
|
106
|
|
|
private function verificationResponse($record, $result) |
|
107
|
|
|
{ |
|
108
|
|
|
$isVerified = $record->verify($result, false) ? 'true' : 'false'; |
|
|
|
|
|
|
109
|
|
|
|
|
110
|
|
|
return json_encode([ |
|
111
|
|
|
'ID' => "$record->ID", |
|
112
|
|
|
'Class' => get_class($record), |
|
113
|
|
|
'IsVerified' => $isVerified, |
|
114
|
|
|
], JSON_UNESCAPED_UNICODE); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
} |
|
118
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths