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

LDAPDebugController::Users()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace SilverStripe\ActiveDirectory\Control;
4
5
use SilverStripe\ActiveDirectory\Model\LDAPGroupMapping;
6
use SilverStripe\CMS\Controllers\ContentController;
7
use SilverStripe\Control\HTTPRequest;
8
use SilverStripe\Core\Config\Config;
9
use SilverStripe\ORM\ArrayList;
10
use SilverStripe\Security\Group;
11
use SilverStripe\Security\Permission;
12
use SilverStripe\Security\Security;
13
use SilverStripe\View\ArrayData;
14
15
/**
16
 * Class LDAPDebugController
17
 *
18
 * This controller is used to debug the LDAP connection.
19
 *
20
 * @package activedirectory
21
 */
22
class LDAPDebugController extends ContentController
23
{
24
    /**
25
     * @var array
26
     */
27
    private static $allowed_actions = [
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...
28
        'index',
29
    ];
30
31
    /**
32
     * @var array
33
     */
34
    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...
35
        'ldapService' => '%$SilverStripe\\ActiveDirectory\\Services\\LDAPService'
36
    ];
37
38
    /**
39
     * @var LDAPService
40
     */
41
    public $ldapService;
42
43
    public function init()
44
    {
45
        parent::init();
46
47
        if (!Permission::check('ADMIN')) {
48
            Security::permissionFailure();
49
        }
50
    }
51
52
    /**
53
     * @param HTTPRequest $request
54
     *
55
     * @return string
56
     */
57
    public function index(HTTPRequest $request)
58
    {
59
        return $this->renderWith(['SilverStripe\\ActiveDirectory\\Control\\LDAPDebugController']);
60
    }
61
62
    public function Options()
63
    {
64
        $list = new ArrayList();
65
        foreach (Config::inst()->get('SilverStripe\\ActiveDirectory\\Model\\LDAPGateway', 'options') as $field => $value) {
66
            if ($field === 'password') {
67
                $value = '***';
68
            }
69
70
            $list->push(new ArrayData([
71
                'Name' => $field,
72
                'Value' => $value
73
            ]));
74
        }
75
        return $list;
76
    }
77
78 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...
79
    {
80
        $locations = Config::inst()->get('SilverStripe\\ActiveDirectory\\Services\\LDAPService', 'users_search_locations');
81
        $list = new ArrayList();
82
        if ($locations) {
83
            foreach ($locations as $location) {
84
                $list->push(new ArrayData([
85
                    'Value' => $location
86
                ]));
87
            }
88
        } else {
89
            $list->push($this->Options()->find('Name', 'baseDn'));
90
        }
91
92
        return $list;
93
    }
94
95 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...
96
    {
97
        $locations = Config::inst()->get('SilverStripe\\ActiveDirectory\\Services\\LDAPService', 'groups_search_locations');
98
        $list = new ArrayList();
99
        if ($locations) {
100
            foreach ($locations as $location) {
101
                $list->push(new ArrayData([
102
                    'Value' => $location
103
                ]));
104
            }
105
        } else {
106
            $list->push($this->Options()->find('Name', 'baseDn'));
107
        }
108
109
        return $list;
110
    }
111
112
    public function DefaultGroup()
113
    {
114
        $code = Config::inst()->get('SilverStripe\\ActiveDirectory\\Services\\LDAPService', 'default_group');
115
        if ($code) {
116
            $group = Group::get()->filter('Code', $code)->limit(1)->first();
117
            if (!($group && $group->exists())) {
118
                return sprintf(
119
                    'WARNING: LDAPService.default_group configured with \'%s\' but there is no Group with that Code in the database!',
120
                    $code
121
                );
122
            } else {
123
                return sprintf('%s (Code: %s)', $group->Title, $group->Code);
124
            }
125
        }
126
127
        return null;
128
    }
129
130
    public function MappedGroups()
131
    {
132
        return LDAPGroupMapping::get();
133
    }
134
135 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...
136
    {
137
        $groups = $this->ldapService->getNodes(false);
138
        $list = new ArrayList();
139
        foreach ($groups as $record) {
140
            $list->push(new ArrayData([
141
                'DN' => $record['dn']
142
            ]));
143
        }
144
        return $list;
145
    }
146
147 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...
148
    {
149
        $groups = $this->ldapService->getGroups(false);
150
        $list = new ArrayList();
151
        foreach ($groups as $record) {
152
            $list->push(new ArrayData([
153
                'DN' => $record['dn']
154
            ]));
155
        }
156
        return $list;
157
    }
158
159
    public function Users()
160
    {
161
        return count($this->ldapService->getUsers());
162
    }
163
}
164