Completed
Push — master ( 197387...875417 )
by Damian
11s
created

LDAPGroupExtension   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A updateCMSFields() 0 51 2
A onBeforeDelete() 0 6 2
1
<?php
2
3
namespace SilverStripe\ActiveDirectory\Extensions;
4
5
use SilverStripe\Forms\FieldList;
6
use SilverStripe\Forms\ReadonlyField;
7
use SilverStripe\Forms\LiteralField;
8
use SilverStripe\ORM\DataExtension;
9
use SilverStripe\Forms\GridField\GridField;
10
use SilverStripe\Forms\GridField\GridFieldConfig_RecordEditor;
11
12
/**
13
 * Class LDAPGroupExtension
14
 *
15
 * Adds a field to map an LDAP group to a SilverStripe {@link Group}
16
 *
17
 * @package activedirectory
18
 */
19
class LDAPGroupExtension extends DataExtension
20
{
21
    /**
22
     * @var array
23
     */
24
    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...
25
        // Unique user identifier, same field is used by SAMLMemberExtension
26
        'GUID' => 'Varchar(50)',
27
        'DN' => 'Text',
28
        'LastSynced' => 'DBDatetime'
29
    ];
30
31
    /**
32
     * A SilverStripe group can have several mappings to LDAP groups.
33
     * @var array
34
     */
35
    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...
36
        'LDAPGroupMappings' => 'SilverStripe\\ActiveDirectory\\Model\\LDAPGroupMapping'
37
    ];
38
39
    /**
40
     * Add a field to the Group_Members join table so we can keep track
41
     * of Members added to a mapped Group.
42
     *
43
     * See {@link LDAPService::updateMemberFromLDAP()} for more details
44
     * on how this gets used.
45
     *
46
     * @var array
47
     */
48
    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...
49
        'Members' => [
50
            'IsImportedFromLDAP' => 'Boolean'
51
        ]
52
    ];
53
54
    /**
55
     * {@inheritDoc}
56
     * @param FieldList $fields
57
     */
58
    public function updateCMSFields(FieldList $fields)
59
    {
60
        // Add read-only LDAP metadata fields.
61
        $fields->addFieldToTab('Root.LDAP', ReadonlyField::create('GUID'));
62
        $fields->addFieldToTab('Root.LDAP', ReadonlyField::create('DN'));
63
        $fields->addFieldToTab(
64
            'Root.LDAP',
65
            ReadonlyField::create(
66
                'LastSynced',
67
                _t('LDAPGroupExtension.LASTSYNCED', 'Last synced')
68
            )
69
        );
70
71
        if ($this->owner->GUID) {
72
            $fields->replaceField('Title', ReadonlyField::create('Title'));
73
            $fields->replaceField('Description', ReadonlyField::create('Description'));
74
            // Surface the code which is normally hidden from the CMS user.
75
            $fields->addFieldToTab('Root.Members', ReadonlyField::create('Code'), 'Members');
76
77
            $message = _t(
78
                'LDAPGroupExtension.INFOIMPORTED',
79
                'This group is automatically imported from LDAP.'
80
            );
81
            $fields->addFieldToTab(
82
                'Root.Members',
83
                LiteralField::create(
84
                    'Info',
85
                    sprintf('<p class="message warning">%s</p>', $message)
86
                ),
87
                'Title'
88
            );
89
90
            $fields->addFieldToTab('Root.LDAP', ReadonlyField::create(
91
                'LDAPGroupMappingsRO',
92
                _t('LDAPGroupExtension.AUTOMAPPEDGROUPS', 'Automatically mapped LDAP Groups'),
93
                implode('; ', $this->owner->LDAPGroupMappings()->column('DN'))
94
            ));
95
        } else {
96
            $field = GridField::create(
97
                'LDAPGroupMappings',
98
                _t('LDAPGroupExtension.MAPPEDGROUPS', 'Mapped LDAP Groups'),
99
                $this->owner->LDAPGroupMappings()
100
            );
101
            $config = GridFieldConfig_RecordEditor::create();
102
            $config->getComponentByType('GridFieldAddNewButton')
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface SilverStripe\Forms\GridField\GridFieldComponent as the method setButtonName() does only exist in the following implementations of said interface: SilverStripe\Forms\GridField\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...
103
                ->setButtonName(_t('LDAPGroupExtension.ADDMAPPEDGROUP', 'Add LDAP group mapping'));
104
105
            $field->setConfig($config);
106
            $fields->addFieldToTab('Root.LDAP', $field);
107
        }
108
    }
109
110
    /**
111
     * LDAPGroupMappings are inherently relying on groups and can be removed now.
112
     */
113
    public function onBeforeDelete()
114
    {
115
        foreach ($this->owner->LDAPGroupMappings() as $mapping) {
116
            $mapping->delete();
117
        }
118
    }
119
}
120