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 = [ |
||||||
0 ignored issues
–
show
introduced
by
![]() |
|||||||
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(); |
||||||
0 ignored issues
–
show
The method
getCMSFields() does not exist on Firesphere\BootstrapMFA\...ons\SiteConfigExtension .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. ![]() |
|||||||
57 | } |
||||||
58 | |||||||
59 | $fields->dataFieldByName('EnforceMFA')->setDescription(_t( |
||||||
60 | self::class . '.ENFORCEDSINCE', |
||||||
61 | 'MFA enforced since {date}', |
||||||
62 | ['date' => $this->owner->obj('ForceMFA')->Nice()] |
||||||
0 ignored issues
–
show
The method
obj() does not exist on Firesphere\BootstrapMFA\...ons\SiteConfigExtension .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. ![]() |
|||||||
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 |