Passed
Push — master ( f08e4a...0370aa )
by Simon
02:02
created

MemberExtension::getBackupcodes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
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\SiteConfig\SiteConfig;
17
18
/**
19
 * Class MemberExtension
20
 *
21
 * @package Firesphere\BootstrapMFA
22
 * @property 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
        $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(self::class . '.MFATAB', 'Multi Factor Authentication')
61
        );
62
        $rootTabSet->push(
63
            $tab
64
        );
65
        $fields->addFieldToTab(
66
            'Root.MFA',
67
            $enabled = CheckboxField::create('MFAEnabled', _t(self::class . '.MFAEnabled', 'MFA Enabled'))
68
        );
69
        $fields->addFieldToTab(
70
            'Root.MFA',
71
            CheckboxField::create('updateMFA', _t(self::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 (!$this->owner->MFAEnabled && SiteConfig::current_site_config()->ForceMFA) {
86
            $this->owner->MFAEnabled = true;
87
            $this->owner->updateMFA = true;
88
        }
89
    }
90
91
    /**
92
     *
93
     * @throws \Psr\Container\NotFoundExceptionInterface
94
     */
95
    public function onAfterWrite()
96
    {
97
        parent::onAfterWrite();
98
        if ($this->owner->updateMFA) {
99
            $provider = Injector::inst()->get(BootstrapMFAProvider::class);
100
            $provider->setMember($this->owner);
101
            $provider->updateTokens();
102
        }
103
    }
104
105
    public function getBackupcodes()
106
    {
107
        return $this->owner->BackupCodes();
108
    }
109
}
110