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

MemberExtension::isInGracePeriod()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 17
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 7
nc 4
nop 0
dl 0
loc 17
rs 10
c 0
b 0
f 0
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\Security\Member;
17
use SilverStripe\SiteConfig\SiteConfig;
18
19
/**
20
 * Class MemberExtension
21
 *
22
 * @package Firesphere\BootstrapMFA
23
 * @property Member|MemberExtension $owner
24
 * @property boolean $MFAEnabled
25
 * @method DataList|BackupCode[] BackupCodes()
26
 */
27
class MemberExtension extends DataExtension
28
{
29
    use Configurable;
30
31
    /**
32
     * @var array
33
     */
34
    private static $db = [
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
35
        'MFAEnabled' => 'Boolean(false)',
36
    ];
37
38
    /**
39
     * @var array
40
     */
41
    private static $has_many = [
0 ignored issues
show
introduced by
The private property $has_many is not used, and could be removed.
Loading history...
42
        'BackupCodes' => BackupCode::class
43
    ];
44
45
    /**
46
     * @var bool
47
     */
48
    protected $updateMFA = false;
49
50
    /**
51
     * @param FieldList $fields
52
     */
53
    public function updateCMSFields(FieldList $fields)
54
    {
55
        $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...
56
        $fields->removeByName(['BackupCodes']);
57
        $session = Controller::curr()->getRequest()->getSession();
58
        $rootTabSet = $fields->fieldByName("Root");
59
        $field = LiteralField::create('tokens', $session->get('tokens'));
60
        $tab = Tab::create(
61
            'MFA',
62
            _t(self::class . '.MFATAB', 'Multi Factor Authentication')
63
        );
64
        $rootTabSet->push(
65
            $tab
66
        );
67
        $fields->addFieldToTab(
68
            'Root.MFA',
69
            $enabled = CheckboxField::create('MFAEnabled', _t(self::class . '.MFAEnabled', 'MFA Enabled'))
70
        );
71
        $fields->addFieldToTab(
72
            'Root.MFA',
73
            CheckboxField::create('updateMFA', _t(self::class . '.RESETMFA', 'Reset MFA codes'))
74
        );
75
76
        if ($session->get('tokens')) {
77
            $fields->addFieldToTab('Root.MFA', $field);
78
            $session->clear('tokens');
79
        }
80
    }
81
82
    /**
83
     * Force enable MFA on the member if needed
84
     */
85
    public function onBeforeWrite()
86
    {
87
        if (!$this->owner->MFAEnabled && SiteConfig::current_site_config()->ForceMFA) {
88
            $this->owner->MFAEnabled = true;
89
            $this->owner->updateMFA = true;
90
        }
91
    }
92
93
    /**
94
     *
95
     * @throws \Psr\Container\NotFoundExceptionInterface
96
     */
97
    public function onAfterWrite()
98
    {
99
        parent::onAfterWrite();
100
        if ($this->owner->updateMFA) {
101
            $provider = Injector::inst()->get(BootstrapMFAProvider::class);
102
            $provider->setMember($this->owner);
103
            $provider->updateTokens();
104
        }
105
    }
106
107
    public function getBackupcodes()
108
    {
109
        return $this->owner->BackupCodes();
110
    }
111
112
    public function isInGracePeriod()
113
    {
114
        /** @var Member $member */
115
        $member = $this->owner;
116
117
        // If MFA is enabled on the member, we're always using it
118
        if ($member->MFAEnabled) {
119
            return false;
120
        }
121
122
        $config = SiteConfig::current_site_config();
123
        // If MFA is not enforced, we're in an endless grace period
124
        if (!$config->ForceMFA) {
125
            return true;
126
        }
127
128
        $graceStartDay = ($member->Created > $config->ForceMFA) ? $member->Created : $config->ForceMFA;
0 ignored issues
show
Unused Code introduced by
The assignment to $graceStartDay is dead and can be removed.
Loading history...
129
    }
130
}
131