Passed
Push — master ( 7e81b0...7eb007 )
by Robbie
12:39 queued 11s
created

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
    private static $mfa_help_link = 'https://userhelp.silverstripe.org/en/4/';
0 ignored issues
show
The private property $mfa_help_link is not used, and could be removed.
Loading history...
29
30
    private static $db = [
0 ignored issues
show
The private property $db is not used, and could be removed.
Loading history...
31
        'MFARequired' => 'Boolean',
32
        'MFAGracePeriodExpires' => 'Date',
33
    ];
34
35
    private static $defaults = [
0 ignored issues
show
The private property $defaults is not used, and could be removed.
Loading history...
36
        'MFARequired' => false,
37
    ];
38
39
    public function updateCMSFields(FieldList $fields)
40
    {
41
        Requirements::javascript('silverstripe/mfa: client/dist/js/bundle-cms.js');
42
        Requirements::css('silverstripe/mfa: client/dist/styles/bundle-cms.css');
43
44
        $mfaOptions = OptionsetField::create(
45
            'MFARequired',
46
            '',
47
            [
48
                false => _t(__CLASS__ . '.MFA_OPTIONAL', 'MFA is optional for everyone'),
49
                true => _t(__CLASS__ . '.MFA_REQUIRED', 'MFA is required for everyone'),
50
            ]
51
        );
52
        $mfaOptions->addExtraClass('mfa-settings__required');
53
54
        $mfaGraceEnd = DateField::create(
55
            'MFAGracePeriodExpires',
56
            _t(__CLASS__ . '.MFA_GRACE_TITLE', 'MFA will be required from (optional)')
57
        );
58
        $mfaGraceEnd->setDescription(_t(
59
            __CLASS__ . '.MFA_GRACE_DESCRIPTION',
60
            'MFA setup will be optional prior to this date'
61
        ));
62
        $mfaGraceEnd->addExtraClass('mfa-settings__grace-period');
63
64
        $mfaOptions = CompositeField::create($mfaOptions, $mfaGraceEnd)
65
            ->setTitle(DBField::create_field(
66
                'HTMLFragment',
67
                _t(__CLASS__ . '.MULTI_FACTOR_AUTHENTICATION', 'Multi-factor authentication (MFA)')
68
                . $this->getHelpLink()
69
            ));
70
71
        $fields->addFieldToTab('Root.Access', $mfaOptions);
72
    }
73
74
    /**
75
     * Gets an anchor tag for CMS users to click to find out more about MFA in the SilverStripe CMS
76
     *
77
     * @return string
78
     */
79
    protected function getHelpLink()
80
    {
81
        $link = $this->owner->config()->get('mfa_help_link');
82
        if (!$link) {
83
            return '';
84
        }
85
86
        return sprintf(
87
            '<a class="d-block mfa-settings__help-link" target="blank" rel="noopener" href="%s">%s</a>',
88
            $link,
89
            _t(__CLASS__ . '.MFA_LEARN_MORE', 'Learn about MFA')
90
        );
91
    }
92
}
93