Passed
Pull Request — master (#13)
by Simon
01:53
created

MemberExtension::updateCMSFields()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 11
nc 2
nop 1
1
<?php
2
3
namespace Firesphere\YubiAuth\Extensions;
4
5
use SilverStripe\Forms\CheckboxField;
6
use SilverStripe\Forms\FieldList;
7
use SilverStripe\Forms\NumericField;
8
use SilverStripe\ORM\DataExtension;
9
10
/**
11
 * Class YubiAuthMemberExtension
12
 *
13
 * Enable yubikey authentication disabling temporarily
14
 *
15
 * @property \Firesphere\YubiAuth\Extensions\MemberExtension $owner
16
 * @property string $Yubikey
17
 * @property int $NoYubikeyCount
18
 */
19
class MemberExtension extends DataExtension
20
{
21
    private static $db = [
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
22
        'Yubikey'        => 'Varchar(255)',
23
        'NoYubikeyCount' => 'Int'
24
    ];
25
26
    private static $indexes = [
0 ignored issues
show
introduced by
The private property $indexes is not used, and could be removed.
Loading history...
27
        'Yubikey' => 'unique("Yubikey")' // The Yubikey Signature is unique for every Yubikey.
28
    ];
29
30
    /**
31
     * @inheritdoc
32
     * @param array $labels
33
     */
34
    public function updateFieldLabels(&$labels)
35
    {
36
        $labels['Yubikey'] = _t('YubikeyAuthenticator.YUBIKEY', 'Yubikey code');
37
        $labels['NoYubikeyCount'] = _t('YubikeyAuthenticator.NOYUBIKEYCOUNT', 'Login count without yubikey');
38
    }
39
40
    /**
41
     * @inheritdoc
42
     * @param FieldList $fields
43
     */
44
    public function updateCMSFields(FieldList $fields)
45
    {
46
        $fields->removeByName(['NoYubikeyCount', 'Yubikey', 'YubiAuthEnabled']);
47
        $yubiCount = NumericField::create('NoYubikeyCount');
48
        if ($this->owner->YubiAuthEnabled) {
0 ignored issues
show
Bug Best Practice introduced by
The property YubiAuthEnabled does not exist on Firesphere\YubiAuth\Extensions\MemberExtension. Did you maybe forget to declare it?
Loading history...
49
            $yubiCount->setReadonly(true);
50
        }
51
        $yubiAuth = CheckboxField::create('YubiAuthEnabled');
52
        $yubiAuth->setDescription(
53
            _t(
54
                'YubikeyAuthenticator.ENABLEDDESCRIPTION',
55
                'If the user is new and doesn\'t have a Yubikey yet, you can disable the auth temporarily'
56
            )
57
        );
58
        $fields->addFieldsToTab('Root.MFA', [$yubiCount, $yubiAuth]);
59
60
        return $fields;
61
    }
62
63
    /**
64
     * @inheritdoc
65
     */
66
    public function onBeforeWrite()
67
    {
68
        // Empty the yubikey field on member write, if the yubiauth is not required
69
        // Maybe the user lost the key? So a new one will be set next time it's logged in with key
70
        if (!$this->owner->MFAEnabled) {
0 ignored issues
show
Bug Best Practice introduced by
The property MFAEnabled does not exist on Firesphere\YubiAuth\Extensions\MemberExtension. Did you maybe forget to declare it?
Loading history...
71
            $this->owner->Yubikey = '';
72
        }
73
    }
74
}
75