Passed
Push — master ( 85617d...001e9d )
by Simon
01:58
created

MemberExtension::updateCMSFields()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 25
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 18
nc 2
nop 1
dl 0
loc 25
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
namespace Firesphere\BootstrapMFA\Extensions;
4
5
use Firesphere\BootstrapMFA\Config\MFAEnabledFields;
0 ignored issues
show
Bug introduced by
The type Firesphere\BootstrapMFA\Config\MFAEnabledFields was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Firesphere\BootstrapMFA\Models\BackupCode;
7
use Firesphere\BootstrapMFA\Providers\BootstrapMFAProvider;
8
use SilverStripe\Control\Controller;
9
use SilverStripe\Core\Config\Configurable;
10
use SilverStripe\Core\Injector\Injector;
11
use SilverStripe\Forms\CheckboxField;
12
use SilverStripe\Forms\FieldList;
13
use SilverStripe\Forms\LiteralField;
14
use SilverStripe\Forms\Tab;
15
use SilverStripe\ORM\DataExtension;
16
use SilverStripe\SiteConfig\SiteConfig;
17
18
/**
19
 * Class MemberExtension
20
 *
21
 * @package Firesphere\BootstrapMFA
22
 * @property \Firesphere\BootstrapMFA\Extensions\MemberExtension $owner
23
 * @property boolean $MFAEnabled
24
 * @method \SilverStripe\ORM\DataList|\Firesphere\BootstrapMFA\Models\BackupCode[] Backupcodes()
25
 */
26
class MemberExtension extends DataExtension
27
{
28
    use Configurable;
29
30
    /**
31
     * @var array
32
     */
33
    private static $db = [
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
34
        'MFAEnabled' => 'Boolean(false)',
35
    ];
36
37
    /**
38
     * @var array
39
     */
40
    private static $has_many = [
0 ignored issues
show
introduced by
The private property $has_many is not used, and could be removed.
Loading history...
41
        'Backupcodes' => BackupCode::class
42
    ];
43
44
    /**
45
     * @var bool
46
     */
47
    protected $updateMFA = false;
48
49
    /**
50
     * @param FieldList $fields
51
     */
52
    public function updateCMSFields(FieldList $fields)
53
    {
54
        $fields->removeByName(['Backupcodes']);
55
        $session = Controller::curr()->getRequest()->getSession();
56
        $rootTabSet = $fields->fieldByName("Root");
57
        $field = LiteralField::create('tokens', $session->get('tokens'));
58
        $tab = Tab::create(
59
            'MFA',
60
            _t(__CLASS__ . '.MFATAB', 'Multi Factor Authentication')
61
        );
62
        $rootTabSet->push(
63
            $tab
64
        );
65
        $fields->addFieldToTab(
66
            'Root.MFA',
67
            $enabled = CheckboxField::create('MFAEnabled', _t(__CLASS__ . '.MFAEnabled', 'MFA Enabled'))
68
        );
69
        $fields->addFieldToTab(
70
            'Root.MFA',
71
            CheckboxField::create('updateMFA', _t(__CLASS__ . '.RESETMFA', 'Reset MFA codes'))
72
        );
73
74
        if ($session->get('tokens')) {
75
            $fields->addFieldToTab('Root.MFA', $field);
76
            $session->clear('tokens');
77
        }
78
    }
79
80
    /**
81
     *
82
     */
83
    public function onBeforeWrite()
84
    {
85
        if (SiteConfig::current_site_config()->ForceMFA && !$this->owner->MFAEnabled) {
86
            $this->owner->MFAEnabled = true;
87
            $this->owner->updateMFA = true;
88
        }
89
    }
90
91
    /**
92
     *
93
     */
94
    public function onAfterWrite()
95
    {
96
        parent::onAfterWrite();
97
        if ($this->owner->updateMFA) {
98
            $provider = Injector::inst()->get(BootstrapMFAProvider::class);
99
            $provider->setMember($this->owner);
100
            $provider->updateTokens();
101
        }
102
    }
103
}
104