Issues (102)

src/Extension/SiteConfigExtension.php (3 issues)

Severity
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\FieldList;
9
use SilverStripe\Forms\OptionsetField;
10
use SilverStripe\ORM\DataExtension;
11
use SilverStripe\ORM\FieldType\DBField;
12
use SilverStripe\View\Requirements;
13
14
/**
15
 * Adds multi-factor authentication related settings to the SiteConfig "Access" tab
16
 *
17
 * @property bool MFARequired
18
 * @property string MFAGracePeriodExpires
19
 */
20
class SiteConfigExtension extends DataExtension
21
{
22
    /**
23
     * A URL that will help CMS users find out more information about multi-factor authentication
24
     *
25
     * @config
26
     * @var string
27
     */
28
    // phpcs:disable
29
    private static $mfa_help_link = 'https://userhelp.silverstripe.org/en/4/optional_features/multi-factor_authentication/';
0 ignored issues
show
The private property $mfa_help_link is not used, and could be removed.
Loading history...
30
    // phpcs:enable
31
32
    private static $db = [
0 ignored issues
show
The private property $db is not used, and could be removed.
Loading history...
33
        'MFARequired' => 'Boolean',
34
        'MFAGracePeriodExpires' => 'Date',
35
    ];
36
37
    private static $defaults = [
0 ignored issues
show
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
        $mfaOptions = OptionsetField::create(
47
            'MFARequired',
48
            '',
49
            [
50
                false => _t(__CLASS__ . '.MFA_OPTIONAL', 'MFA is optional for everyone'),
51
                true => _t(__CLASS__ . '.MFA_REQUIRED', 'MFA is required for everyone'),
52
            ]
53
        );
54
        $mfaOptions->addExtraClass('mfa-settings__required');
55
56
        $mfaGraceEnd = DateField::create(
57
            'MFAGracePeriodExpires',
58
            _t(__CLASS__ . '.MFA_GRACE_TITLE', 'MFA will be required from (optional)')
59
        );
60
        $mfaGraceEnd->setDescription(_t(
61
            __CLASS__ . '.MFA_GRACE_DESCRIPTION',
62
            'MFA setup will be optional prior to this date'
63
        ));
64
        $mfaGraceEnd->addExtraClass('mfa-settings__grace-period');
65
66
        $mfaOptions = CompositeField::create($mfaOptions, $mfaGraceEnd)
67
            ->setTitle(DBField::create_field(
68
                'HTMLFragment',
69
                _t(__CLASS__ . '.MULTI_FACTOR_AUTHENTICATION', 'Multi-factor authentication (MFA)')
70
                . $this->getHelpLink()
71
            ));
72
73
        $fields->addFieldToTab('Root.Access', $mfaOptions);
74
    }
75
76
    /**
77
     * Gets an anchor tag for CMS users to click to find out more about MFA in the SilverStripe CMS
78
     *
79
     * @return string
80
     */
81
    protected function getHelpLink()
82
    {
83
        $link = $this->owner->config()->get('mfa_help_link');
84
        if (!$link) {
85
            return '';
86
        }
87
88
        return sprintf(
89
            '<a class="d-block mfa-settings__help-link" target="blank" rel="noopener" href="%s">%s</a>',
90
            $link,
91
            _t(__CLASS__ . '.MFA_LEARN_MORE', 'Learn about MFA')
92
        );
93
    }
94
}
95