Passed
Push — master ( 3c190c...2857c2 )
by Robbie
03:26
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 49 2
A onBeforeDelete() 0 4 2
1
<?php
2
3
namespace SilverStripe\LDAP\Extensions;
4
5
use SilverStripe\Forms\FieldList;
6
use SilverStripe\Forms\GridField\GridField;
7
use SilverStripe\Forms\GridField\GridFieldConfig_RecordEditor;
8
use SilverStripe\Forms\LiteralField;
9
use SilverStripe\Forms\ReadonlyField;
10
use SilverStripe\LDAP\Model\LDAPGroupMapping;
11
use SilverStripe\ORM\DataExtension;
12
13
/**
14
 * Class LDAPGroupExtension
15
 *
16
 * Adds a field to map an LDAP group to a SilverStripe {@link Group}
17
 */
18
class LDAPGroupExtension extends DataExtension
19
{
20
    /**
21
     * @var array
22
     */
23
    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...
24
        // Unique user identifier, same field is used by SAMLMemberExtension
25
        'GUID' => 'Varchar(50)',
26
        'DN' => 'Text',
27
        'LastSynced' => 'DBDatetime'
28
    ];
29
30
    /**
31
     * A SilverStripe group can have several mappings to LDAP groups.
32
     * @var array
33
     */
34
    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...
35
        'LDAPGroupMappings' => LDAPGroupMapping::class,
36
    ];
37
38
    /**
39
     * Add a field to the Group_Members join table so we can keep track
40
     * of Members added to a mapped Group.
41
     *
42
     * See {@link LDAPService::updateMemberFromLDAP()} for more details
43
     * on how this gets used.
44
     *
45
     * @var array
46
     */
47
    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...
48
        'Members' => [
49
            'IsImportedFromLDAP' => 'Boolean'
50
        ]
51
    ];
52
53
    /**
54
     * {@inheritDoc}
55
     * @param FieldList $fields
56
     */
57
    public function updateCMSFields(FieldList $fields)
58
    {
59
        // Add read-only LDAP metadata fields.
60
        $fields->addFieldToTab('Root.LDAP', ReadonlyField::create('GUID'));
0 ignored issues
show
Bug introduced by
'GUID' 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

60
        $fields->addFieldToTab('Root.LDAP', ReadonlyField::create(/** @scrutinizer ignore-type */ 'GUID'));
Loading history...
61
        $fields->addFieldToTab('Root.LDAP', ReadonlyField::create('DN'));
62
        $fields->addFieldToTab(
63
            'Root.LDAP',
64
            ReadonlyField::create(
65
                'LastSynced',
66
                _t(__CLASS__ . '.LASTSYNCED', 'Last synced')
67
            )
68
        );
69
70
        if ($this->owner->GUID) {
71
            $fields->replaceField('Title', ReadonlyField::create('Title'));
72
            $fields->replaceField('Description', ReadonlyField::create('Description'));
73
            // Surface the code which is normally hidden from the CMS user.
74
            $fields->addFieldToTab('Root.Members', ReadonlyField::create('Code'), 'Members');
75
76
            $message = _t(
77
                __CLASS__ . '.INFOIMPORTED',
78
                'This group is automatically imported from LDAP.'
79
            );
80
            $fields->addFieldToTab(
81
                'Root.Members',
82
                LiteralField::create(
83
                    'Info',
84
                    sprintf('<p class="message warning">%s</p>', $message)
85
                ),
86
                'Title'
87
            );
88
89
            $fields->addFieldToTab('Root.LDAP', ReadonlyField::create(
90
                'LDAPGroupMappingsRO',
91
                _t(__CLASS__ . '.AUTOMAPPEDGROUPS', 'Automatically mapped LDAP Groups'),
92
                implode('; ', $this->owner->LDAPGroupMappings()->column('DN'))
93
            ));
94
        } else {
95
            $field = GridField::create(
96
                'LDAPGroupMappings',
97
                _t('LDAPGroupExtension.MAPPEDGROUPS', 'Mapped LDAP Groups'),
98
                $this->owner->LDAPGroupMappings()
99
            );
100
            $config = GridFieldConfig_RecordEditor::create();
101
            $config->getComponentByType('GridFieldAddNewButton')
102
                ->setButtonName(_t(__CLASS__ . '.ADDMAPPEDGROUP', 'Add LDAP group mapping'));
103
104
            $field->setConfig($config);
105
            $fields->addFieldToTab('Root.LDAP', $field);
106
        }
107
    }
108
109
    /**
110
     * LDAPGroupMappings are inherently relying on groups and can be removed now.
111
     */
112
    public function onBeforeDelete()
113
    {
114
        foreach ($this->owner->LDAPGroupMappings() as $mapping) {
115
            $mapping->delete();
116
        }
117
    }
118
}
119