Issues (70)

src/Model/LDAPGroupMapping.php (9 issues)

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
The private property $table_name is not used, and could be removed.
Loading history...
23
24
    /**
25
     * @var array
26
     */
27
    private static $db = [
0 ignored issues
show
The private property $db is not used, and could 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
The private property $has_one is not used, and could be removed.
Loading history...
36
        'Group' => Group::class
37
    ];
38
39
    /**
40
     * @var array
41
     */
42
    private static $summary_fields = [
0 ignored issues
show
The private property $summary_fields is not used, and could be removed.
Loading history...
43
        'DN'
44
    ];
45
46
    /**
47
     * @var array
48
     */
49
    private static $dependencies = [
0 ignored issues
show
The private property $dependencies is not used, and could be removed.
Loading history...
50
        'ldapService' => '%$' . LDAPService::class,
51
    ];
52
53
    private static $singular_name = 'LDAP Group Mapping';
0 ignored issues
show
The private property $singular_name is not used, and could be removed.
Loading history...
54
55
    private static $plural_name = 'LDAP Group Mappings';
0 ignored issues
show
The private property $plural_name is not used, and could be removed.
Loading history...
56
57
    /**
58
     * {@inheritDoc}
59
     * @return FieldList
60
     */
61
    public function getCMSFields()
62
    {
63
        $fields = parent::getCMSFields();
64
        $fields->removeByName('DN');
65
66
        $field = DropdownField::create('DN', _t(__CLASS__ . '.LDAPGROUP', 'LDAP Group'));
67
        $field->setEmptyString(_t(__CLASS__ . '.SELECTONE', 'Select one'));
68
        $groups = $this->ldapService->getGroups(true, ['dn', 'name']);
0 ignored issues
show
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...
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

68
        /** @scrutinizer ignore-call */ 
69
        $groups = $this->ldapService->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...
69
        $source = [];
70
        if ($groups) {
71
            foreach ($groups as $dn => $record) {
72
                $source[$dn] = sprintf('%s (%s)', $record['name'], $dn);
73
            }
74
        }
75
        asort($source);
76
        $field->setSource($source);
77
        $fields->addFieldToTab('Root.Main', $field);
78
79
        $fields->removeByName('Scope');
80
        $fields->addFieldToTab(
81
            'Root.Main',
82
            DropdownField::create('Scope', _t(__CLASS__ . '.SCOPE', 'Scope'), [
83
                'Subtree' => _t(
84
                    __CLASS__ . '.SUBTREE_DESCRIPTION',
85
                    'Users within this group and all nested groups within'
86
                ),
87
                'OneLevel' => _t(__CLASS__ . '.ONELEVEL_DESCRIPTION', 'Only users within this group'),
88
            ])
89
        );
90
91
        return $fields;
92
    }
93
}
94