Completed
Push — master ( 04483f...a4b252 )
by Robbie
11s
created

SiteConfigExtension::updateCMSFields()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 39
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 24
nc 1
nop 1
dl 0
loc 39
rs 9.536
c 0
b 0
f 0
1
<?php
2
3
namespace SilverStripe\MFA\Extension;
4
5
use SilverStripe\Forms\CheckboxField;
6
use SilverStripe\Forms\CompositeField;
7
use SilverStripe\Forms\DateField;
8
use SilverStripe\Forms\DatetimeField;
9
use SilverStripe\Forms\FieldList;
10
use SilverStripe\Forms\OptionsetField;
11
use SilverStripe\Forms\TextField;
12
use SilverStripe\ORM\DataExtension;
13
use SilverStripe\ORM\FieldType\DBDate;
14
use SilverStripe\ORM\FieldType\DBDatetime;
15
use SilverStripe\ORM\FieldType\DBField;
16
use SilverStripe\View\Requirements;
17
18
/**
19
 * Adds multi factor authentication related settings to the SiteConfig "Access" tab
20
 */
21
class SiteConfigExtension extends DataExtension
22
{
23
    /**
24
     * A URL that will help CMS users find out more information about multi factor authentication
25
     *
26
     * @config
27
     * @var string
28
     */
29
    private static $mfa_help_link = 'https://userhelp.silverstripe.org/en/4/';
0 ignored issues
show
introduced by
The private property $mfa_help_link is not used, and could be removed.
Loading history...
30
31
    private static $db = [
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
32
        'MFAEnabled' => 'Boolean',
33
        'MFARequired' => 'Boolean',
34
        'MFAGracePeriodExpires' => 'Date',
35
    ];
36
37
    private static $defaults = [
0 ignored issues
show
introduced by
The private property $defaults is not used, and could be removed.
Loading history...
38
        'MFARequired' => false,
39
    ];
40
41
    public function updateCMSFields(FieldList $fields)
42
    {
43
        Requirements::javascript('silverstripe/mfa: client/dist/js/bundle-cms.js');
44
        Requirements::css('silverstripe/mfa: client/dist/styles/bundle-cms.css');
45
46
        $mfaEnabled = CheckboxField::create(
47
            'MFAEnabled',
48
            _t(__CLASS__ . '.MFA_ENABLED', 'Enable MFA for CMS access')
49
        );
50
        $mfaEnabled->addExtraClass('mfa-settings__enabled');
51
52
        $mfaOptions = OptionsetField::create(
53
            'MFARequired',
54
            '',
55
            [
56
                false => _t(__CLASS__ . '.MFA_OPTIONAL', 'MFA is optional for everyone'),
57
                true => _t(__CLASS__ . '.MFA_REQUIRED', 'MFA is required for everyone'),
58
59
            ]
60
        );
61
        $mfaOptions->addExtraClass('mfa-settings__required mfa-settings--hidden');
62
63
        $mfaGraceEnd = DateField::create(
64
            'MFAGracePeriodExpires',
65
            'Optional: grace period end date when MFA is enforced'
66
        );
67
        $mfaGraceEnd
68
            ->addExtraClass('mfa-settings__grace-period')
69
            // Don't allow users to set the date to anything earlier than now
70
            ->setMinDate(DBDatetime::now()->Format(DBDate::ISO_DATE));
71
72
        $mfaOptions = CompositeField::create($mfaEnabled, $mfaOptions, $mfaGraceEnd)
73
            ->setTitle(DBField::create_field(
74
                'HTMLFragment',
75
                _t(__CLASS__ . '.MULTI_FACTOR_AUTHENTICATION', 'Multi Factor Authentication (MFA)')
76
                . $this->getHelpLink()
77
            ));
78
79
        $fields->addFieldToTab('Root.Access', $mfaOptions);
80
    }
81
82
    /**
83
     * Gets an anchor tag for CMS users to click to find out more about MFA in the SilverStripe CMS
84
     *
85
     * @return string
86
     */
87
    protected function getHelpLink()
88
    {
89
        $link = $this->owner->config()->get('mfa_help_link');
90
        if (!$link) {
91
            return '';
92
        }
93
94
        return sprintf(
95
            '<a class="d-block mfa-settings__help-link" target="blank" rel="noopener" href="%s">%s</a>',
96
            $link,
97
            _t(__CLASS__ . '.FIND_OUT_MORE', 'Find out more')
98
        );
99
    }
100
}
101