1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @author Russell Michell 2018 <[email protected]> |
5
|
|
|
* @package silverstripe-verifiable |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace PhpTek\Verifiable; |
9
|
|
|
|
10
|
|
|
use SilverStripe\Core\Injector\Injector; |
11
|
|
|
use SilverStripe\Core\Injector\Injectable; |
12
|
|
|
use SilverStripe\Core\Config\Configurable; |
13
|
|
|
use SilverStripe\ORM\DataObject; |
14
|
|
|
use SilverStripe\Core\ClassInfo; |
15
|
|
|
use PhpTek\Verifiable\Exception\VerifiableBackendException; |
16
|
|
|
use SilverStripe\ORM\DataObjectSchema; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Does all the connect/read/write heavy-lifting. |
20
|
|
|
*/ |
21
|
|
|
class Verifiable |
22
|
|
|
{ |
23
|
|
|
use Injectable; |
24
|
|
|
use Configurable; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* The hashing function to use. |
28
|
|
|
* |
29
|
|
|
* @var string |
30
|
|
|
* @see {@link $this->hash()} |
31
|
|
|
* @config |
32
|
|
|
*/ |
33
|
|
|
private static $hash_func = 'sha1'; |
|
|
|
|
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var BackendProvider |
|
|
|
|
37
|
|
|
*/ |
38
|
|
|
protected $backend; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var DataObjectProvider |
|
|
|
|
42
|
|
|
*/ |
43
|
|
|
protected $model; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @return void |
47
|
|
|
* @throws VerifiableBackendException |
48
|
|
|
*/ |
49
|
|
|
public function __construct() |
50
|
|
|
{ |
51
|
|
|
if (!$this->backend = $this->backend()) { |
52
|
|
|
throw new VerifiableBackendException('Backend not found or not specified.'); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Write a hash of data as per the "verifiable_fields" confif static on each |
58
|
|
|
* {@link DataObject}. |
59
|
|
|
* |
60
|
|
|
* @return boolean True if the write went through OK. False otherwise. |
61
|
|
|
*/ |
62
|
|
|
public function write() : boolean |
|
|
|
|
63
|
|
|
{ |
64
|
|
|
$fields = $this->model->config()->get('verifiable_fields'); |
65
|
|
|
$hash = $this->hash($fields); |
66
|
|
|
|
67
|
|
|
return $this->backend->write($hash); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Get and instantiate a new backend |
72
|
|
|
* |
73
|
|
|
* @return mixed null | BackendProvider |
74
|
|
|
*/ |
75
|
|
|
public function backend() |
76
|
|
|
{ |
77
|
|
|
$namedBackend = $this->config()->get('backend'); |
78
|
|
|
$backends = ClassInfo::implementorsOf('BackendProvider'); |
79
|
|
|
|
80
|
|
|
foreach ($backends as $backend) { |
81
|
|
|
if (singleton($backend)->name() == $namedBackend) { |
82
|
|
|
return Injector::inst()->create($backend); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return null; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Hashes the data found in all the fields of the current Data Model. |
91
|
|
|
* |
92
|
|
|
* @param array $fields The fields on the current {@link DataObject} subclass |
93
|
|
|
* who's values should be hashed. |
94
|
|
|
* @return string |
95
|
|
|
* @todo Take use input in the form of a digital signature |
96
|
|
|
*/ |
97
|
|
|
public function hash(array $fields) : string |
98
|
|
|
{ |
99
|
|
|
$text = ''; |
100
|
|
|
$class = get_class($this->model); |
101
|
|
|
$func = $this->config()->get('hash_func'); |
102
|
|
|
$specs = array_keys( |
103
|
|
|
$this->model->getSchema()->fielSpecs($class, DataObjectSchema::UNINHERITED) |
104
|
|
|
); |
105
|
|
|
|
106
|
|
|
foreach ($specs as $name) { |
107
|
|
|
if (isset($fields[$name])) { |
108
|
|
|
$text .= $this->model->getField($name); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return $func($test); |
|
|
|
|
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param DataObject $model |
117
|
|
|
* @return Verifiable |
118
|
|
|
*/ |
119
|
|
|
public function setModel(DataObject $model) : Verifiable |
120
|
|
|
{ |
121
|
|
|
$this->model = $model; |
|
|
|
|
122
|
|
|
|
123
|
|
|
return $this; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Return an array of the fetched hash, a timestamp and everything else the current backend |
128
|
|
|
* gives us. |
129
|
|
|
* |
130
|
|
|
* @return array |
131
|
|
|
*/ |
132
|
|
|
public function read(string $hash) : array |
|
|
|
|
133
|
|
|
{ |
134
|
|
|
} |
|
|
|
|
135
|
|
|
|
136
|
|
|
} |
137
|
|
|
|