Completed
Push — master ( 0e58f5...d2de37 )
by
unknown
13s
created

SiteConfigExtension::updateCheckboxDescription()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 3
nop 1
dl 0
loc 11
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Firesphere\BootstrapMFA\Extensions;
4
5
use SilverStripe\Forms\CheckboxField;
6
use SilverStripe\Forms\FieldList;
7
use SilverStripe\Forms\Form;
8
use SilverStripe\Forms\ReadonlyField;
9
use SilverStripe\ORM\DataExtension;
10
use SilverStripe\ORM\FieldType\DBDate;
11
use SilverStripe\ORM\FieldType\DBDatetime;
12
use SilverStripe\SiteConfig\SiteConfig;
13
14
/**
15
 * Class \Firesphere\BootstrapMFA\Extensions\SiteConfigExtension
16
 *
17
 * @property SiteConfig|SiteConfigExtension $owner
18
 * @property string|DBDatetime $ForceMFA
19
 */
20
class SiteConfigExtension extends DataExtension
21
{
22
    /**
23
     * @var array
24
     */
25
    private static $db = [
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
26
        'ForceMFA' => 'Date'
27
    ];
28
29
    /**
30
     * Add the checkbox and if enabled the date since enforcement
31
     *
32
     * @param FieldList $fields
33
     */
34
    public function updateCMSFields(FieldList $fields)
35
    {
36
        $fields->addFieldToTab(
37
            'Root.MFA',
38
            $checkbox = CheckboxField::create(
39
                'EnforceMFA',
40
                _t(self::class . '.ENFORCEMFA', 'Enforce MFA on all users'),
41
                $this->isMFAEnforced()
42
            )
43
        );
44
        $checkbox->setDescription(null);
45
46
        $this->updateCheckboxDescription($fields);
47
    }
48
49
    public function saveEnforceMFA($value)
50
    {
51
        $MFAEnforced = $this->isMFAEnforced();
52
53
        // If the value is truthy and we don't have MFA already enforced
54
        if ($value && !$MFAEnforced) {
55
            $this->owner->ForceMFA = DBDatetime::now()->format(DBDatetime::ISO_DATE);
56
            // Otherwise if the field indicates MFA should not be enforced but it currently is
57
        } elseif (!$value && $MFAEnforced) {
58
            $this->owner->ForceMFA = null;
59
        }
60
61
        $this->updateCheckboxDescription();
62
    }
63
64
    public function updateCheckboxDescription(FieldList $fields = null)
65
    {
66
        if ($this->isMFAEnforced()) {
67
            if (!$fields) {
68
                $fields = $this->owner->getCMSFields();
0 ignored issues
show
Bug introduced by
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 ignore-call  annotation

68
                /** @scrutinizer ignore-call */ 
69
                $fields = $this->owner->getCMSFields();

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.

Loading history...
69
            }
70
71
            $fields->fieldByName('Root.MFA.EnforceMFA')->setDescription(_t(
72
                self::class . '.ENFORCEDSINCE',
73
                'MFA enforced since {date}',
74
                ['date' => $this->owner->obj('ForceMFA')->Nice()]
0 ignored issues
show
Bug introduced by
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 ignore-call  annotation

74
                ['date' => $this->owner->/** @scrutinizer ignore-call */ obj('ForceMFA')->Nice()]

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.

Loading history...
75
            ));
76
        }
77
    }
78
79
    protected function isMFAEnforced()
80
    {
81
        // "0000-00-00" provides BC support for version 1.0 of BootstrapMFA where this attribute was stored as a boolean
82
        return $this->owner->ForceMFA !== null && $this->owner->ForceMFA !== '0000-00-00';
83
    }
84
}
85