LDAPGroupExtension   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 6
dl 0
loc 94
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A updateCMSFields() 0 48 2
A onBeforeDelete() 0 6 2
1
<?php
2
/**
3
 * Class LDAPGroupExtension
4
 *
5
 * Adds a field to map an LDAP group to a SilverStripe {@link Group}
6
 */
7
class LDAPGroupExtension extends DataExtension
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
8
{
9
    /**
10
     * @var array
11
     */
12
    private static $db = [
0 ignored issues
show
Unused Code introduced by
The property $db is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
13
        // Unique user identifier, same field is used by SAMLMemberExtension
14
        'GUID' => 'Varchar(50)',
15
        'DN' => 'Text',
16
        'LastSynced' => 'SS_Datetime'
17
    ];
18
19
    /**
20
     * A SilverStripe group can have several mappings to LDAP groups.
21
     * @var array
22
     */
23
    private static $has_many = [
0 ignored issues
show
Unused Code introduced by
The property $has_many is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
24
        'LDAPGroupMappings' => 'LDAPGroupMapping'
25
    ];
26
27
    /**
28
     * Add a field to the Group_Members join table so we can keep track
29
     * of Members added to a mapped Group.
30
     *
31
     * See {@link LDAPService::updateMemberFromLDAP()} for more details
32
     * on how this gets used.
33
     *
34
     * @var array
35
     */
36
    private static $many_many_extraFields = [
0 ignored issues
show
Unused Code introduced by
The property $many_many_extraFields is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
37
        'Members' => [
38
            'IsImportedFromLDAP' => 'Boolean'
39
        ]
40
    ];
41
42
    public function updateCMSFields(FieldList $fields)
43
    {
44
        // Add read-only LDAP metadata fields.
45
        $fields->addFieldToTab('Root.LDAP', new ReadonlyField('GUID'));
46
        $fields->addFieldToTab('Root.LDAP', new ReadonlyField('DN'));
47
        $fields->addFieldToTab('Root.LDAP', new ReadonlyField(
48
            'LastSynced',
49
            _t('LDAPGroupExtension.LASTSYNCED', 'Last synced'))
50
        );
51
52
        if ($this->owner->GUID) {
0 ignored issues
show
Bug introduced by
The property GUID does not seem to exist in SS_Object.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
53
            $fields->replaceField('Title', new ReadonlyField('Title'));
54
            $fields->replaceField('Description', new ReadonlyField('Description'));
55
            // Surface the code which is normally hidden from the CMS user.
56
            $fields->addFieldToTab('Root.Members', new ReadonlyField('Code'), 'Members');
57
58
            $message = _t(
59
                'LDAPGroupExtension.INFOIMPORTED',
60
                'This group is automatically imported from LDAP.'
61
            );
62
            $fields->addFieldToTab(
63
                'Root.Members',
64
                new LiteralField(
65
                    'Info',
66
                    sprintf('<p class="message warning">%s</p>', $message)
67
                ),
68
                'Title'
69
            );
70
71
            $fields->addFieldToTab('Root.LDAP', new ReadonlyField(
72
                'LDAPGroupMappingsRO',
73
                _t('LDAPGroupExtension.AUTOMAPPEDGROUPS', 'Automatically mapped LDAP Groups'),
74
                implode('; ', $this->owner->LDAPGroupMappings()->column('DN'))
75
            ));
76
        } else {
77
            $field = GridField::create(
78
                'LDAPGroupMappings',
79
                _t('LDAPGroupExtension.MAPPEDGROUPS', 'Mapped LDAP Groups'),
80
                $this->owner->LDAPGroupMappings()
81
            );
82
            $config = GridFieldConfig_RecordEditor::create();
83
            $config->getComponentByType('GridFieldAddNewButton')
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface GridFieldComponent as the method setButtonName() does only exist in the following implementations of said interface: GridFieldAddNewButton.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
84
                ->setButtonName(_t('LDAPGroupExtension.ADDMAPPEDGROUP', 'Add LDAP group mapping'));
85
86
            $field->setConfig($config);
87
            $fields->addFieldToTab('Root.LDAP', $field);
88
        }
89
    }
90
91
    /**
92
     * LDAPGroupMappings are inherently relying on groups and can be removed now.
93
     */
94
    public function onBeforeDelete()
95
    {
96
        foreach ($this->owner->LDAPGroupMappings() as $mapping) {
97
            $mapping->delete();
98
        }
99
    }
100
}
101