1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Firesphere\BootstrapMFA\Extensions; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Forms\CheckboxField; |
6
|
|
|
use SilverStripe\Forms\FieldList; |
7
|
|
|
use SilverStripe\ORM\DataExtension; |
8
|
|
|
use SilverStripe\ORM\FieldType\DBDatetime; |
9
|
|
|
use SilverStripe\SiteConfig\SiteConfig; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class \Firesphere\BootstrapMFA\Extensions\SiteConfigExtension |
13
|
|
|
* |
14
|
|
|
* @property SiteConfig|SiteConfigExtension $owner |
15
|
|
|
* @property string $ForceMFA |
16
|
|
|
*/ |
17
|
|
|
class SiteConfigExtension extends DataExtension |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var array |
21
|
|
|
*/ |
22
|
|
|
private static $db = [ |
|
|
|
|
23
|
|
|
'ForceMFA' => 'Date' |
24
|
|
|
]; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Add the checkbox and if enabled the date since enforcement |
28
|
|
|
* |
29
|
|
|
* @param FieldList $fields |
30
|
|
|
*/ |
31
|
|
|
public function updateCMSFields(FieldList $fields) |
32
|
|
|
{ |
33
|
|
|
$fields->addFieldToTab( |
34
|
|
|
'Root.MFA', |
35
|
|
|
$checkbox = CheckboxField::create( |
36
|
|
|
'EnforceMFA', |
37
|
|
|
_t(self::class . '.ENFORCEMFA', 'Enforce MFA on all users'), |
38
|
|
|
$this->isMFAEnforced() |
39
|
|
|
) |
40
|
|
|
); |
41
|
|
|
$checkbox->setDescription(null); |
42
|
|
|
|
43
|
|
|
$this->updateCheckboxDescription($fields); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
protected function isMFAEnforced() |
47
|
|
|
{ |
48
|
|
|
// "0000-00-00" provides BC support for version 1.0 of BootstrapMFA where this attribute was stored as a boolean |
49
|
|
|
return $this->owner->ForceMFA !== null && $this->owner->ForceMFA !== '0000-00-00'; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function updateCheckboxDescription(FieldList $fields = null) |
53
|
|
|
{ |
54
|
|
|
if ($this->isMFAEnforced()) { |
55
|
|
|
if (!$fields) { |
56
|
|
|
$fields = $this->owner->getCMSFields(); |
|
|
|
|
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$fields->dataFieldByName('EnforceMFA')->setDescription(_t( |
60
|
|
|
self::class . '.ENFORCEDSINCE', |
61
|
|
|
'MFA enforced since {date}', |
62
|
|
|
['date' => $this->owner->obj('ForceMFA')->Nice()] |
|
|
|
|
63
|
|
|
)); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Magic method, {@see Form::saveInto()} |
69
|
|
|
* |
70
|
|
|
* @param string|null|false $value Date string |
71
|
|
|
*/ |
72
|
|
|
public function saveEnforceMFA($value) |
73
|
|
|
{ |
74
|
|
|
$MFAEnforced = $this->isMFAEnforced(); |
75
|
|
|
|
76
|
|
|
// If the value is truthy and we don't have MFA already enforced |
77
|
|
|
if ($value && !$MFAEnforced) { |
78
|
|
|
$this->owner->ForceMFA = DBDatetime::now()->Format(DBDatetime::ISO_DATE); |
79
|
|
|
// Otherwise if the field indicates MFA should not be enforced but it currently is |
80
|
|
|
} elseif (!$value && $MFAEnforced) { |
81
|
|
|
$this->owner->ForceMFA = null; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$this->updateCheckboxDescription(); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|