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

MemberExtension::updateFieldLabels()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
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 boolean $YubiAuthEnabled
17
 * @property string $Yubikey
18
 * @property int $NoYubikeyCount
19
 */
20
class MemberExtension extends DataExtension
21
{
22
    private static $db = [
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
23
        'YubiAuthEnabled' => 'Boolean(true)',
24
        'Yubikey'         => 'Varchar(255)',
25
        'NoYubikeyCount'  => 'Int'
26
    ];
27
    private static $defaults = [
0 ignored issues
show
introduced by
The private property $defaults is not used, and could be removed.
Loading history...
28
        'YubiAuthEnabled' => true
29
    ];
30
    private static $indexes = [
0 ignored issues
show
introduced by
The private property $indexes is not used, and could be removed.
Loading history...
31
        'Yubikey' => 'unique("Yubikey")' // The Yubikey Signature is unique for every Yubikey.
32
    ];
33
34
    /**
35
     * @inheritdoc
36
     * @param array $labels
37
     */
38
    public function updateFieldLabels(&$labels)
39
    {
40
        $labels['YubiAuthEnabled'] = _t('YubikeyAuthenticator.ENABLED', 'Yubikey Authentication Enabled');
41
        $labels['Yubikey'] = _t('YubikeyAuthenticator.YUBIKEY', 'Yubikey code');
42
        $labels['NoYubikeyCount'] = _t('YubikeyAuthenticator.NOYUBIKEYCOUNT', 'Login count without yubikey');
43
    }
44
45
    /**
46
     * @inheritdoc
47
     * @param FieldList $fields
48
     */
49
    public function updateCMSFields(FieldList $fields)
50
    {
51
        $fields->removeByName(['NoYubikeyCount', 'Yubikey', 'YubiAuthEnabled']);
52
        $yubiCount = NumericField::create('NoYubikeyCount');
53
        if ($this->owner->YubiAuthEnabled) {
54
            $yubiCount->setReadonly(true);
55
        }
56
        $yubiAuth = CheckboxField::create('YubiAuthEnabled');
57
        $yubiAuth->setDescription(
58
            _t(
59
                'YubikeyAuthenticator.ENABLEDDESCRIPTION',
60
                'If the user is new and doesn\'t have a Yubikey yet, you can disable the auth temporarily'
61
            )
62
        );
63
64
        $fields->addFieldsToTab('Root.Main', [$yubiCount, $yubiAuth], 'DirectGroups');
65
66
        return $fields;
67
    }
68
69
    /**
70
     * @inheritdoc
71
     */
72
    public function onBeforeWrite()
73
    {
74
        // Empty the yubikey field on member write, if the yubiauth is not required
75
        // Maybe the user lost the key? So a new one will be set next time it's logged in with key
76
        if (!$this->owner->YubiAuthEnabled) {
77
            $this->owner->Yubikey = '';
78
        }
79
    }
80
}