Passed
Pull Request — master (#13)
by Simon
02:05
created

MemberExtension::updateCMSFields()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 15
nc 2
nop 1
dl 0
loc 24
rs 8.9713
c 0
b 0
f 0
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\Forms\ReadonlyField;
9
use SilverStripe\ORM\DataExtension;
10
11
/**
12
 * Class YubiAuthMemberExtension
13
 *
14
 * Enable yubikey authentication disabling temporarily
15
 *
16
 * @property \Firesphere\YubiAuth\Extensions\MemberExtension|\SilverStripe\Security\Member $owner
17
 * @property boolean $YubiAuthEnabled
18
 * @property string $Yubikey
19
 * @property int $NoYubikeyCount
20
 */
21
class MemberExtension extends DataExtension
22
{
23
    private static $db = [
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
24
        'YubiAuthEnabled' => 'Boolean(true)',
25
        'Yubikey'         => 'Varchar(16)',
26
        'NoYubikeyCount'  => 'Int'
27
    ];
28
29
    private static $defaults = [
0 ignored issues
show
introduced by
The private property $defaults is not used, and could be removed.
Loading history...
30
        'YubiAuthEnabled' => true
31
    ];
32
33
    private static $indexes = [
0 ignored issues
show
introduced by
The private property $indexes is not used, and could be removed.
Loading history...
34
        'Yubikey' => 'unique("Yubikey")' // The Yubikey Signature is unique for every Yubikey.
35
    ];
36
37
    /**
38
     * @inheritdoc
39
     * @param array $labels
40
     */
41
    public function updateFieldLabels(&$labels)
42
    {
43
        parent::updateFieldLabels($labels);
44
        $labels['YubiAuthEnabled'] = _t('YubikeyAuthenticator.ENABLED', 'Yubikey Authentication Enabled');
45
        $labels['Yubikey'] = _t('YubikeyAuthenticator.YUBIKEY', 'Yubikey code');
46
        $labels['NoYubikeyCount'] = _t('YubikeyAuthenticator.NOYUBIKEYCOUNT', 'Login count without yubikey');
47
    }
48
49
    /**
50
     * @inheritdoc
51
     * @param FieldList $fields
52
     */
53
    public function updateCMSFields(FieldList $fields)
54
    {
55
        $fields->removeByName(['NoYubikeyCount', 'Yubikey', 'YubiAuthEnabled']);
56
        $yubiCount = NumericField::create('NoYubikeyCount', $this->owner->fieldLabel('NoYubikeyCount'));
0 ignored issues
show
Bug introduced by
The method fieldLabel() does not exist on Firesphere\YubiAuth\Extensions\MemberExtension. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

56
        $yubiCount = NumericField::create('NoYubikeyCount', $this->owner->/** @scrutinizer ignore-call */ fieldLabel('NoYubikeyCount'));

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
57
        if ($this->owner->YubiAuthEnabled) {
58
            $yubiCount->setReadonly(true);
59
        }
60
        $yubiField = ReadonlyField::create('Yubikey', $this->owner->fieldLabel('Yubikey'));
61
        $yubiField->setDescription(
62
            _t(
63
                'YubikeyAuthenticator.YUBIKEYDESCRIPTION',
64
                'Unique identifier string for the Yubikey. Will reset when the Yubikey Authentication is disabled'
65
            )
66
        );
67
68
        $yubiAuth = CheckboxField::create('YubiAuthEnabled', $this->owner->fieldLabel('YubiAuthEnabled'));
69
        $yubiAuth->setDescription(
70
            _t(
71
                'YubikeyAuthenticator.ENABLEDDESCRIPTION',
72
                'If the user is new and doesn\'t have a Yubikey yet, you can disable the auth temporarily'
73
            )
74
        );
75
76
        $fields->addFieldsToTab('Root.Main', [$yubiCount, $yubiField, $yubiAuth], 'DirectGroups');
77
    }
78
79
    /**
80
     * @inheritdoc
81
     */
82
    public function onBeforeWrite()
83
    {
84
        // Empty the yubikey field on member write, if the yubiauth is not required
85
        // Maybe the user lost the key? So a new one will be set next time it's logged in with key
86
        if (!$this->owner->YubiAuthEnabled) {
87
            $this->owner->Yubikey = '';
88
        }
89
    }
90
}
91