Passed
Pull Request — master (#13)
by Simon
03:37 queued 01:36
created

MemberExtension   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 5
c 2
b 0
f 0
dl 0
loc 67
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A onBeforeWrite() 0 6 2
A updateFieldLabels() 0 6 1
A updateCMSFields() 0 15 2
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
        parent::updateFieldLabels($labels);
41
        $labels['YubiAuthEnabled'] = _t('YubikeyAuthenticator.ENABLED', 'Yubikey Authentication Enabled');
42
        $labels['Yubikey'] = _t('YubikeyAuthenticator.YUBIKEY', 'Yubikey code');
43
        $labels['NoYubikeyCount'] = _t('YubikeyAuthenticator.NOYUBIKEYCOUNT', 'Login count without yubikey');
44
    }
45
46
    /**
47
     * @inheritdoc
48
     * @param FieldList $fields
49
     */
50
    public function updateCMSFields(FieldList $fields)
51
    {
52
        $fields->removeByName(['NoYubikeyCount', 'Yubikey', 'YubiAuthEnabled']);
53
        $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

53
        $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...
54
        if ($this->owner->YubiAuthEnabled) {
55
            $yubiCount->setReadonly(true);
56
        }
57
        $yubiAuth = CheckboxField::create('YubiAuthEnabled', $this->owner->fieldLabel('YubiAuthEnabled'));
58
        $yubiAuth->setDescription(
59
            _t(
60
                'YubikeyAuthenticator.ENABLEDDESCRIPTION',
61
                'If the user is new and doesn\'t have a Yubikey yet, you can disable the auth temporarily'
62
            )
63
        );
64
        $fields->addFieldsToTab('Root.Main', [$yubiCount, $yubiAuth], 'DirectGroups');
65
    }
66
67
    /**
68
     * @inheritdoc
69
     */
70
    public function onBeforeWrite()
71
    {
72
        // Empty the yubikey field on member write, if the yubiauth is not required
73
        // Maybe the user lost the key? So a new one will be set next time it's logged in with key
74
        if (!$this->owner->YubiAuthEnabled) {
75
            $this->owner->Yubikey = '';
76
        }
77
    }
78
}