Passed
Pull Request — master (#21)
by Simon
01:42
created

MemberExtension   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 42
dl 0
loc 101
rs 10
c 0
b 0
f 0
wmc 12

5 Methods

Rating   Name   Duplication   Size   Complexity  
A onBeforeWrite() 0 5 3
A updateCMSFields() 0 26 2
A getBackupcodes() 0 3 1
A onAfterWrite() 0 7 2
A isInGracePeriod() 0 16 4
1
<?php
2
3
namespace Firesphere\BootstrapMFA\Extensions;
4
5
use Firesphere\BootstrapMFA\Models\BackupCode;
6
use Firesphere\BootstrapMFA\Providers\BootstrapMFAProvider;
7
use SilverStripe\Control\Controller;
8
use SilverStripe\Core\Config\Configurable;
9
use SilverStripe\Core\Injector\Injector;
10
use SilverStripe\Forms\CheckboxField;
11
use SilverStripe\Forms\FieldList;
12
use SilverStripe\Forms\LiteralField;
13
use SilverStripe\Forms\Tab;
14
use SilverStripe\ORM\DataExtension;
15
use SilverStripe\ORM\DataList;
16
use SilverStripe\SiteConfig\SiteConfig;
17
18
/**
19
 * Class MemberExtension
20
 *
21
 * @package Firesphere\BootstrapMFA
22
 * @property Member|MemberExtension $owner
23
 * @property boolean $MFAEnabled
24
 * @method DataList|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
        $this->updateMFA = 0;
0 ignored issues
show
Documentation Bug introduced by
The property $updateMFA was declared of type boolean, but 0 is of type integer. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
55
        $fields->removeByName(['BackupCodes']);
56
        $session = Controller::curr()->getRequest()->getSession();
57
        $rootTabSet = $fields->fieldByName("Root");
58
        $field = LiteralField::create('tokens', $session->get('tokens'));
59
        $tab = Tab::create(
60
            'MFA',
61
            _t(self::class . '.MFATAB', 'Multi Factor Authentication')
62
        );
63
        $rootTabSet->push(
64
            $tab
65
        );
66
        $fields->addFieldToTab(
67
            'Root.MFA',
68
            $enabled = CheckboxField::create('MFAEnabled', _t(self::class . '.MFAEnabled', 'MFA Enabled'))
69
        );
70
        $fields->addFieldToTab(
71
            'Root.MFA',
72
            CheckboxField::create('updateMFA', _t(self::class . '.RESETMFA', 'Reset MFA codes'))
73
        );
74
75
        if ($session->get('tokens')) {
76
            $fields->addFieldToTab('Root.MFA', $field);
77
            $session->clear('tokens');
78
        }
79
    }
80
81
    /**
82
     *
83
     */
84
    public function onBeforeWrite()
85
    {
86
        if (!$this->owner->MFAEnabled && SiteConfig::current_site_config()->ForceMFA) {
87
            $this->owner->MFAEnabled = true;
88
            $this->owner->updateMFA = true;
89
        }
90
    }
91
92
    /**
93
     *
94
     * @throws \Psr\Container\NotFoundExceptionInterface
95
     */
96
    public function onAfterWrite()
97
    {
98
        parent::onAfterWrite();
99
        if ($this->owner->updateMFA) {
100
            $provider = Injector::inst()->get(BootstrapMFAProvider::class);
101
            $provider->setMember($this->owner);
102
            $provider->updateTokens();
103
        }
104
    }
105
106
    public function getBackupcodes()
107
    {
108
        return $this->owner->BackupCodes();
109
    }
110
111
    public function isInGracePeriod()
112
    {
113
        $member = $this->owner;
114
115
        // If MFA is enabled on the member, we're always using it
116
        if ($member->MFAEnabled) {
117
            return false;
118
        }
119
120
        $config = SiteConfig::current_site_config();
121
        // If MFA is not enforced, we're in an endless grace period
122
        if (!$config->ForceMFA) {
123
            return true;
124
        }
125
126
        $graceStartDay = ($member->Created > $config->ForceMFA) ? $member->Created : $config->ForceMFA;
0 ignored issues
show
Bug Best Practice introduced by
The property Created does not exist on Firesphere\BootstrapMFA\Extensions\MemberExtension. Did you maybe forget to declare it?
Loading history...
Unused Code introduced by
The assignment to $graceStartDay is dead and can be removed.
Loading history...
127
    }
128
}
129