Passed
Push — master ( 3c190c...2857c2 )
by Robbie
03:26
created

LDAPGroupMapping::getCMSFields()   B

Complexity

Conditions 3
Paths 2

Size

Total Lines 30
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 20
nc 2
nop 0
dl 0
loc 30
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
namespace SilverStripe\LDAP\Model;
4
5
use SilverStripe\Forms\DropdownField;
6
use SilverStripe\Forms\FieldList;
7
use SilverStripe\LDAP\Services\LDAPService;
8
use SilverStripe\ORM\DataObject;
9
use SilverStripe\Security\Group;
10
11
/**
12
 * Class LDAPGroupMapping
13
 *
14
 * An individual mapping of an LDAP group to a SilverStripe {@link Group}
15
 */
16
class LDAPGroupMapping extends DataObject
17
{
18
    /**
19
     * {@inheritDoc}
20
     * @var string
21
     */
22
    private static $table_name = 'LDAPGroupMapping';
0 ignored issues
show
Unused Code introduced by
The property $table_name 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...
23
24
    /**
25
     * @var array
26
     */
27
    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...
28
        'DN' => 'Text', // the DN value of the LDAP object in AD, e.g. CN=Users,DN=playpen,DN=local
29
        'Scope' => 'Enum("Subtree,OneLevel","Subtree")' // the scope of the mapping
30
    ];
31
32
    /**
33
     * @var array
34
     */
35
    private static $has_one = [
0 ignored issues
show
Unused Code introduced by
The property $has_one 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
        'Group' => Group::class
37
    ];
38
39
    /**
40
     * @var array
41
     */
42
    private static $summary_fields = [
0 ignored issues
show
Unused Code introduced by
The property $summary_fields 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...
43
        'DN'
44
    ];
45
46
    /**
47
     * @var array
48
     */
49
    private static $dependencies = [
0 ignored issues
show
Unused Code introduced by
The property $dependencies 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...
50
        'ldapService' => '%$' . LDAPService::class,
51
    ];
52
53
    /**
54
     * {@inheritDoc}
55
     * @return FieldList
56
     */
57
    public function getCMSFields()
58
    {
59
        $fields = parent::getCMSFields();
60
        $fields->removeByName('DN');
61
62
        $field = DropdownField::create('DN', _t(__CLASS__ . '.LDAPGROUP', 'LDAP Group'));
0 ignored issues
show
Bug introduced by
'DN' 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

62
        $field = DropdownField::create(/** @scrutinizer ignore-type */ 'DN', _t(__CLASS__ . '.LDAPGROUP', 'LDAP Group'));
Loading history...
63
        $field->setEmptyString(_t(__CLASS__ . '.SELECTONE', 'Select one'));
64
        $groups = $this->ldapService->getGroups(true, ['dn', 'name']);
0 ignored issues
show
Bug introduced by
The method getGroups() does not exist on null. ( Ignorable by Annotation )

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

64
        $groups = $this->ldapService->/** @scrutinizer ignore-call */ getGroups(true, ['dn', 'name']);

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...
Bug Best Practice introduced by
The property ldapService does not exist on SilverStripe\LDAP\Model\LDAPGroupMapping. Since you implemented __get, consider adding a @property annotation.
Loading history...
65
        if ($groups) {
66
            foreach ($groups as $dn => $record) {
67
                $source[$dn] = sprintf('%s (%s)', $record['name'], $dn);
68
            }
69
        }
70
        asort($source);
71
        $field->setSource($source);
72
        $fields->addFieldToTab('Root.Main', $field);
73
74
        $fields->removeByName('Scope');
75
        $fields->addFieldToTab(
76
            'Root.Main',
77
            DropdownField::create('Scope', _t(__CLASS__ . '.SCOPE', 'Scope'), [
78
                'Subtree' => _t(
79
                    __CLASS__ . '.SUBTREE_DESCRIPTION',
80
                    'Users within this group and all nested groups within'
81
                ),
82
                'OneLevel' => _t(__CLASS__ . '.ONELEVEL_DESCRIPTION', 'Only users within this group'),
83
            ])
84
        );
85
86
        return $fields;
87
    }
88
}
89