1
|
|
|
<?php |
2
|
|
|
namespace SilverStripe\MFA\Model; |
3
|
|
|
|
4
|
|
|
use SilverStripe\Core\Injector\Injector; |
5
|
|
|
use SilverStripe\MFA\AuthenticationMethod\AuthenticatorInterface; |
6
|
|
|
use SilverStripe\MFA\AuthenticationMethodInterface; |
7
|
|
|
use SilverStripe\ORM\DataObject; |
8
|
|
|
use SilverStripe\Security\Member; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @package SilverStripe\MFA\Model |
12
|
|
|
* |
13
|
|
|
* @property string MethodClassName |
14
|
|
|
* @property array Data |
15
|
|
|
*/ |
16
|
|
|
class AuthenticationMethod extends DataObject |
17
|
|
|
{ |
18
|
|
|
private static $table_name = 'MFAAuthenticationMethod'; |
|
|
|
|
19
|
|
|
|
20
|
|
|
private static $db = [ |
|
|
|
|
21
|
|
|
// The class name of the AuthenticationMethodInterface that this record refers to |
22
|
|
|
'MethodClassName' => 'Varchar', |
23
|
|
|
// Data stored as a JSON blob that may contain detail specific to this registration of the authenticator |
24
|
|
|
'Data' => 'Text', |
25
|
|
|
]; |
26
|
|
|
|
27
|
|
|
private static $has_one = [ |
|
|
|
|
28
|
|
|
'Member' => Member::class, |
29
|
|
|
]; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var AuthenticationMethodInterface |
33
|
|
|
*/ |
34
|
|
|
protected $method; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @return AuthenticatorInterface |
38
|
|
|
*/ |
39
|
|
|
public function getAuthenticator() |
40
|
|
|
{ |
41
|
|
|
return $this->getMethod()->getAuthenticator(); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @return mixed |
46
|
|
|
*/ |
47
|
|
|
public function getRegistrar() |
48
|
|
|
{ |
49
|
|
|
return $this->getMethod()->getRegistrar(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Accessor for Data field to ensure it's presented as an array instead of a JSON blob |
54
|
|
|
* |
55
|
|
|
* @return array |
56
|
|
|
*/ |
57
|
|
|
public function getData() |
58
|
|
|
{ |
59
|
|
|
return (array) json_decode($this->getField('Data'), true); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Setter for the Data field to ensure it's saved as a JSON blob |
64
|
|
|
* |
65
|
|
|
* @param mixed $data |
66
|
|
|
*/ |
67
|
|
|
public function setData($data) |
68
|
|
|
{ |
69
|
|
|
$this->setField('Data', json_encode($data)); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @return AuthenticationMethodInterface |
74
|
|
|
*/ |
75
|
|
|
protected function getMethod() |
76
|
|
|
{ |
77
|
|
|
if (!$this->method) { |
78
|
|
|
$this->method = Injector::inst()->create($this->MethodClassName); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return $this->method; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|