Passed
Push — master ( 9ca738...8837ac )
by Simon
02:54
created

MemberExtension::updateCMSFields()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 1
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
1
<?php
2
3
namespace Firesphere\GraphQLJWT\Extensions;
4
5
use SilverStripe\Core\Convert;
6
use SilverStripe\Forms\CheckboxField;
7
use SilverStripe\Forms\FieldList;
8
use SilverStripe\ORM\DataExtension;
9
use SilverStripe\Security\Member;
10
use stdClass;
11
12
/**
13
 * Class MemberExtension
14
 * Add a unique token to the Member for extra validation
15
 */
16
class MemberExtension extends DataExtension
17
{
18
    private static $db = [
19
        'JWTUniqueID' => 'Varchar(255)',
20
    ];
21
22
    private static $indexes = [
23
        'JWTUniqueID' => 'unique'
24
    ];
25
26
    public function updateCMSFields(FieldList $fields)
27
    {
28
        parent::updateCMSFields($fields);
29
        $fields->removeByName(['JWTUniqueID']);
30
        if ($this->owner->JWTUniqueID) {
31
            $fields->addFieldsToTab(
32
                'Root.Main',
33
                [
34
                    CheckboxField::create('reset', 'Reset the Token ID to disable this user\'s remote login')
0 ignored issues
show
Bug introduced by
'reset' of type string is incompatible with the type array expected by parameter $args of SilverStripe\View\ViewableData::create(). ( Ignorable by Annotation )

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

34
                    CheckboxField::create(/** @scrutinizer ignore-type */ 'reset', 'Reset the Token ID to disable this user\'s remote login')
Loading history...
35
                ]
36
            );
37
        }
38
    }
39
40
    public function onBeforeWrite()
41
    {
42
        parent::onBeforeWrite();
43
        if ($this->owner->reset) {
44
            $this->owner->JWTUniqueID = null;
45
        }
46
    }
47
48
    /**
49
     * Option to add data to the JWT Subject
50
     *
51
     * @return string
52
     */
53
    public function getJWTData()
54
    {
55
        $data = new stdClass();
56
        if ($this->owner->exists()) {
57
            $identifier = Member::config()->get('unique_identifier_field');
58
            $extraFields = Member::config()->get('jwt_subject_fields');
59
            $data->id = $this->owner->ID;
60
            $data->userName = $this->owner->$identifier;
61
            if (is_array($extraFields)) {
62
                foreach ($extraFields as $field) {
63
                    $data->$field = $this->owner->$field;
64
                }
65
            }
66
        }
67
68
        return Convert::raw2json($data);
69
    }
70
}
71