Completed
Push — master ( 273314...2e0424 )
by Sean
03:53 queued 01:38
created

LDAPDebugController::Users()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 * Class LDAPDebugController
4
 *
5
 * This controller is used to debug the LDAP connection.
6
 */
7
class LDAPDebugController extends ContentController
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
    /**
11
     * @var array
12
     */
13
    private static $allowed_actions = array(
0 ignored issues
show
Unused Code introduced by
The property $allowed_actions 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...
14
        'index',
15
    );
16
17
    /**
18
     * @var array
19
     */
20
    private static $dependencies = array(
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...
21
        'ldapService' => '%$LDAPService'
22
    );
23
24
    /**
25
     * @var LDAPService
26
     */
27
    public $ldapService;
28
29
    public function init()
30
    {
31
        parent::init();
32
33
        if (!Permission::check('ADMIN')) {
34
            Security::permissionFailure();
35
        }
36
    }
37
38
    /**
39
     * @param SS_HTTPRequest $request
40
     *
41
     * @return string
42
     */
43
    public function index(\SS_HTTPRequest $request) {
44
        return $this->renderWith(['LDAPDebugController']);
45
    }
46
47
    public function Options()
48
    {
49
        $list = new ArrayList();
50
        foreach (Config::inst()->get('LDAPGateway', 'options') as $field => $value) {
0 ignored issues
show
Bug introduced by
The expression \Config::inst()->get('LDAPGateway', 'options') of type array|integer|double|string|boolean is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
51
            $list->push(new ArrayData(array(
52
                'Name' => $field,
53
                'Value' => $value
54
            )));
55
        }
56
        return $list;
57
    }
58
59 View Code Duplication
    public function UsersSearchLocations()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
60
    {
61
        $locations = Config::inst()->get('LDAPService', 'users_search_locations');
62
        $list = new ArrayList();
63
        if ($locations) {
64
            foreach ($locations as $location) {
0 ignored issues
show
Bug introduced by
The expression $locations of type array|integer|double|string|boolean is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
65
                $list->push(new ArrayData(array(
66
                    'Value' => $location
67
                )));
68
            }
69
        } else {
70
            $list->push($this->Options()->find('Name', 'baseDn'));
71
        }
72
73
        return $list;
74
    }
75
76 View Code Duplication
    public function GroupsSearchLocations()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
77
    {
78
        $locations = Config::inst()->get('LDAPService', 'groups_search_locations');
79
        $list = new ArrayList();
80
        if ($locations) {
81
            foreach ($locations as $location) {
0 ignored issues
show
Bug introduced by
The expression $locations of type array|integer|double|string|boolean is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
82
                $list->push(new ArrayData(array(
83
                    'Value' => $location
84
                )));
85
            }
86
        } else {
87
            $list->push($this->Options()->find('Name', 'baseDn'));
88
        }
89
90
        return $list;
91
    }
92
93
    public function DefaultGroup()
94
    {
95
        $code = Config::inst()->get('LDAPService', 'default_group');
96
        if ($code) {
97
            $group = Group::get()->filter('Code', $code)->limit(1)->first();
98
            if (!($group && $group->exists())) {
99
                return sprintf(
100
                    'WARNING: LDAPService.default_group configured with \'%s\' but there is no Group with that Code in the database!',
101
                    $code
102
                );
103
            } else {
104
                return sprintf('%s (Code: %s)', $group->Title, $group->Code);
105
            }
106
        }
107
108
        return null;
109
    }
110
111
    public function MappedGroups()
112
    {
113
        return LDAPGroupMapping::get();
114
    }
115
116 View Code Duplication
    public function Nodes()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
117
    {
118
        $groups = $this->ldapService->getNodes(false);
119
        $list = new ArrayList();
120
        foreach ($groups as $record) {
121
            $list->push(new ArrayData(array(
122
                'DN' => $record['dn']
123
            )));
124
        }
125
        return $list;
126
    }
127
128 View Code Duplication
    public function Groups()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
129
    {
130
        $groups = $this->ldapService->getGroups(false);
131
        $list = new ArrayList();
132
        foreach ($groups as $record) {
133
            $list->push(new ArrayData(array(
134
                'DN' => $record['dn']
135
            )));
136
        }
137
        return $list;
138
    }
139
140
    public function Users()
141
    {
142
        return count($this->ldapService->getUsers());
143
    }
144
}
145