Completed
Pull Request — master (#116)
by Franco
01:46
created

LDAPFakeGateway::search()   C

Complexity

Conditions 7
Paths 10

Size

Total Lines 29
Code Lines 14

Duplication

Lines 22
Ratio 75.86 %

Importance

Changes 0
Metric Value
dl 22
loc 29
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 14
nc 10
nop 5
1
<?php
2
3
namespace SilverStripe\ActiveDirectory\Tests\Model;
4
5
use Zend\Authentication\Result as AuthenticationResult;
6
use SilverStripe\ActiveDirectory\Model\LDAPGateway;
7
use SilverStripe\Dev\TestOnly;
8
use Zend\Ldap\Ldap;
9
10
/**
11
 * @package activedirectory
12
 */
13
class LDAPFakeGateway extends LDAPGateway implements TestOnly
14
{
15
    public function __construct()
16
    {
17
        // do nothing
18
    }
19
20
    private static $data = [
21
        'groups' => [
22
            'CN=Users,DC=playpen,DC=local' => [
23
                ['dn' => 'CN=Group1,CN=Users,DC=playpen,DC=local'],
24
                ['dn' => 'CN=Group2,CN=Users,DC=playpen,DC=local'],
25
                ['dn' => 'CN=Group3,CN=Users,DC=playpen,DC=local'],
26
                ['dn' => 'CN=Group4,CN=Users,DC=playpen,DC=local'],
27
                ['dn' => 'CN=Group5,CN=Users,DC=playpen,DC=local']
28
            ],
29
            'CN=Others,DC=playpen,DC=local' => [
30
                ['dn' => 'CN=Group6,CN=Others,DC=playpen,DC=local'],
31
                ['dn' => 'CN=Group7,CN=Others,DC=playpen,DC=local'],
32
                ['dn' => 'CN=Group8,CN=Others,DC=playpen,DC=local']
33
            ]
34
        ],
35
        'users' => [
36
            '123' => [
37
                'distinguishedname' => 'CN=Joe,DC=playpen,DC=local',
38
                'objectguid' => '123',
39
                'cn' => 'jbloggs',
40
                'useraccountcontrol' => '1',
41
                'givenname' => 'Joe',
42
                'sn' => 'Bloggs',
43
                'mail' => '[email protected]',
44
                'password' => 'mockPassword',
45
                'canonicalName'=>'mockCanonicalName',
46
                'userprincipalname' => '[email protected]'
47
            ]
48
        ]
49
    ];
50
51
    /**
52
     * @inheritdoc
53
     */
54
    public function authenticate($username, $password)
55
    {
56
        $messages = [];
57
        if (!$user = $this->getUserByEmail($username)) {
58
            $messages[0] = 'Username not found';
59
            $code = AuthenticationResult::FAILURE;
60
            return new AuthenticationResult($code, $username, $messages);
61
        }
62
        if ($user[0]['password'] == $password) {
63
            $messages[0] = 'OK';
64
            return new AuthenticationResult(AuthenticationResult::SUCCESS, $username, $messages);
65
        } else {
66
            $messages[0] = 'Password doesn\'t match';
67
            return new AuthenticationResult(AuthenticationResult::FAILURE, $username, $messages);
68
        }
69
    }
70
71
    public function getNodes($baseDn = null, $scope = Ldap::SEARCH_SCOPE_SUB, $attributes = [], $sort = '')
72
    {
73
    }
74
75
    public function getGroups($baseDn = null, $scope = Ldap::SEARCH_SCOPE_SUB, $attributes = [], $sort = '')
76
    {
77
        if (isset($baseDn)) {
78
            return !empty(self::$data['groups'][$baseDn]) ? self::$data['groups'][$baseDn] : null;
79
        }
80
    }
81
82
    public function getNestedGroups($dn, $baseDn = null, $scope = Ldap::SEARCH_SCOPE_SUB, $attributes = [])
83
    {
84
    }
85
86
    public function getGroupByGUID($guid, $baseDn = null, $scope = Ldap::SEARCH_SCOPE_SUB, $attributes = [])
87
    {
88
    }
89
90
    public function getUsers($baseDn = null, $scope = Ldap::SEARCH_SCOPE_SUB, $attributes = [], $sort = '')
91
    {
92
    }
93
94
    public function getUserByGUID($guid, $baseDn = null, $scope = Ldap::SEARCH_SCOPE_SUB, $attributes = [])
95
    {
96
        return [self::$data['users'][$guid]];
97
    }
98
99
    public function update($dn, array $attributes)
100
    {
101
    }
102
103
    public function delete($dn, $recursively = false)
104
    {
105
    }
106
107
    public function move($fromDn, $toDn, $recursively = false)
108
    {
109
    }
110
111
    public function add($dn, array $attributes)
112
    {
113
    }
114
115
    protected function search($filter, $baseDn = null, $scope = Ldap::SEARCH_SCOPE_SUB, $attributes = [], $sort = '')
116
    {
117
        $records = self::$data;
118
        $results = [];
119 View Code Duplication
        foreach ($records as $record) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
120
            foreach ($record as $attribute => $value) {
121
                // if the value is an array with a single value, e.g. 'samaccountname' => array(0 => 'myusername')
122
                // then make sure it's just set in the results as 'samaccountname' => 'myusername' so that it
123
                // can be used directly by ArrayData
124
                if (is_array($value) && count($value) == 1) {
125
                    $value = $value[0];
126
                }
127
128
                // ObjectGUID and ObjectSID attributes are in binary, we need to convert those to strings
129
                if ($attribute == 'objectguid') {
130
                    $value = LDAPUtil::bin_to_str_guid($value);
131
                }
132
                if ($attribute == 'objectsid') {
133
                    $value = LDAPUtil::bin_to_str_sid($value);
134
                }
135
136
                $record[$attribute] = $value;
137
            }
138
139
            $results[] = $record;
140
        }
141
142
        return $results;
143
    }
144
145
    /**
146
     * Mock to search trough dummy $data.
147
     *
148
     * @param string $email
149
     * @param null $baseDn
150
     * @param int $scope
151
     * @param array $attributes
152
     * @return array
153
     */
154 View Code Duplication
    public function getUserByEmail($email, $baseDn = null, $scope = Ldap::SEARCH_SCOPE_SUB, $attributes = [])
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...
155
    {
156
        $result = [];
157
        foreach (self::$data['users'] as $guid => $info) {
158
            if ($info['mail'] == $email) {
159
                $result[] = $info;
160
                break;
161
            }
162
        }
163
164
        return $result;
165
    }
166
167
    /**
168
     * Mock to search trough dummy $data.
169
     *
170
     * @param string $username
171
     * @param null $baseDn
172
     * @param int $scope
173
     * @param array $attributes
174
     * @return array
175
     * @internal param string $email
176
     */
177 View Code Duplication
    public function getUserByUsername($username, $baseDn = null, $scope = Ldap::SEARCH_SCOPE_SUB, $attributes = [])
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...
178
    {
179
        $result = [];
180
        foreach (self::$data['users'] as $guid => $info) {
181
            if ($info['userprincipalname'] == $username) {
182
                $result[] = $info;
183
                break;
184
            }
185
        }
186
187
        return $result;
188
    }
189
}
190