Passed
Push — master ( b2198e...e8e6de )
by Robbie
02:50 queued 10s
created

src/Control/LDAPDebugController.php (2 issues)

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