1 | <?php |
||
2 | |||
3 | namespace SilverStripe\LDAP\Tests\Model; |
||
4 | |||
5 | use SilverStripe\Dev\TestOnly; |
||
6 | use SilverStripe\LDAP\Model\LDAPGateway; |
||
7 | use Zend\Authentication\Result as AuthenticationResult; |
||
8 | use Zend\Ldap\Ldap; |
||
9 | |||
10 | class LDAPFakeGateway extends LDAPGateway implements TestOnly |
||
11 | { |
||
12 | public function __construct() |
||
13 | { |
||
14 | // thumbnail images are raw JPEG/JFIF files, but that's not important |
||
15 | // for this test, as long as the binary content are the same |
||
16 | self::$data['users']['456']['thumbnailphoto'] = base64_decode(self::$data['users']['456']['thumbnailphoto']); |
||
17 | } |
||
18 | |||
19 | private static $data = [ |
||
20 | 'groups' => [ |
||
21 | 'CN=Users,DC=playpen,DC=local' => [ |
||
22 | ['dn' => 'CN=Group1,CN=Users,DC=playpen,DC=local'], |
||
23 | ['dn' => 'CN=Group2,CN=Users,DC=playpen,DC=local'], |
||
24 | ['dn' => 'CN=Group3,CN=Users,DC=playpen,DC=local'], |
||
25 | ['dn' => 'CN=Group4,CN=Users,DC=playpen,DC=local'], |
||
26 | ['dn' => 'CN=Group5,CN=Users,DC=playpen,DC=local'] |
||
27 | ], |
||
28 | 'CN=Others,DC=playpen,DC=local' => [ |
||
29 | ['dn' => 'CN=Group6,CN=Others,DC=playpen,DC=local'], |
||
30 | ['dn' => 'CN=Group7,CN=Others,DC=playpen,DC=local'], |
||
31 | ['dn' => 'CN=Group8,CN=Others,DC=playpen,DC=local'] |
||
32 | ] |
||
33 | ], |
||
34 | 'users' => [ |
||
35 | '123' => [ |
||
36 | 'distinguishedname' => 'CN=Joe,DC=playpen,DC=local', |
||
37 | 'objectguid' => '123', |
||
38 | 'cn' => 'jbloggs', |
||
39 | 'useraccountcontrol' => '1', |
||
40 | 'givenname' => 'Joe', |
||
41 | 'sn' => 'Bloggs', |
||
42 | 'mail' => '[email protected]', |
||
43 | 'password' => 'mockPassword', |
||
44 | 'canonicalName' => 'mockCanonicalName', |
||
45 | 'userprincipalname' => '[email protected]', |
||
46 | 'samaccountname' => 'joe' |
||
47 | ], |
||
48 | '456' => [ |
||
49 | 'distinguishedname' => 'CN=Appleseed,DC=playpen,DC=local', |
||
50 | 'objectguid' => '456', |
||
51 | 'cn' => 'jappleseed', |
||
52 | 'useraccountcontrol' => '1', |
||
53 | 'givenname' => 'Johnny', |
||
54 | 'sn' => 'Appleseed', |
||
55 | 'mail' => '[email protected]', |
||
56 | 'password' => 'mockPassword1', |
||
57 | 'canonicalName' => 'mockCanonicalName2', |
||
58 | 'userprincipalname' => '[email protected]', |
||
59 | 'samaccountname' => 'john', |
||
60 | 'thumbnailphoto' => 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAACklEQVR4nGMAAQAABQABDQottAAAAABJRU5ErkJggg==', |
||
61 | 'displayname' => 'Johnny Appleseed' |
||
62 | ], |
||
63 | '789' => [ |
||
64 | 'distinguishedname' => 'CN=Appleseed,DC=playpen,DC=local', |
||
65 | 'objectguid' => '456', |
||
66 | 'cn' => 'jappleseed', |
||
67 | 'useraccountcontrol' => '1', |
||
68 | 'givenname' => 'Johnny', |
||
69 | 'sn' => 'Appleseed', |
||
70 | 'mail' => '[email protected]', |
||
71 | 'password' => 'mockPassword1', |
||
72 | 'canonicalName' => 'mockCanonicalName2', |
||
73 | 'userprincipalname' => '[email protected]', |
||
74 | 'samaccountname' => 'john', |
||
75 | 'thumbnailphoto' => 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAACklEQVR4nGMAAQAABQABDQottAAAAABJRU5ErkJggg==', |
||
76 | 'displayname' => 'Johnny Appleseed', |
||
77 | 'memberof' => [ |
||
78 | 'CN=Group1,CN=Users,DC=playpen,DC=local', |
||
79 | 'CN=Group2,CN=Users,DC=playpen,DC=local', |
||
80 | 'CN=Group3,CN=Users,DC=playpen,DC=local', |
||
81 | 'CN=Group4,CN=Users,DC=playpen,DC=local', |
||
82 | ] |
||
83 | ] |
||
84 | ] |
||
85 | ]; |
||
86 | |||
87 | /** |
||
88 | * @inheritdoc |
||
89 | */ |
||
90 | public function authenticate($username, $password) |
||
91 | { |
||
92 | $messages = []; |
||
93 | if (!$user = $this->getUserByEmail($username)) { |
||
94 | $messages[0] = 'Username not found'; |
||
95 | $code = AuthenticationResult::FAILURE; |
||
96 | return new AuthenticationResult($code, $username, $messages); |
||
97 | } |
||
98 | if ($user[0]['password'] == $password) { |
||
99 | $messages[0] = 'OK'; |
||
100 | return new AuthenticationResult(AuthenticationResult::SUCCESS, $username, $messages); |
||
101 | } else { |
||
102 | $messages[0] = 'Password doesn\'t match'; |
||
103 | return new AuthenticationResult(AuthenticationResult::FAILURE, $username, $messages); |
||
104 | } |
||
105 | } |
||
106 | |||
107 | public function getNodes($baseDn = null, $scope = Ldap::SEARCH_SCOPE_SUB, $attributes = [], $sort = '') |
||
108 | { |
||
109 | } |
||
110 | |||
111 | public function getGroups($baseDn = null, $scope = Ldap::SEARCH_SCOPE_SUB, $attributes = [], $sort = '') |
||
112 | { |
||
113 | if (isset($baseDn)) { |
||
114 | return !empty(self::$data['groups'][$baseDn]) ? self::$data['groups'][$baseDn] : null; |
||
115 | } |
||
116 | } |
||
117 | |||
118 | /** |
||
119 | * Return nested groups for a DN. Not currently implemented. |
||
120 | * |
||
121 | * @param string $dn |
||
122 | * @param null $baseDn |
||
0 ignored issues
–
show
Documentation
Bug
introduced
by
![]() |
|||
123 | * @param int $scope |
||
124 | * @param array $attributes |
||
125 | * |
||
126 | * @return array |
||
127 | */ |
||
128 | public function getNestedGroups($dn, $baseDn = null, $scope = Ldap::SEARCH_SCOPE_SUB, $attributes = []) |
||
129 | { |
||
130 | return []; |
||
131 | } |
||
132 | |||
133 | public function getGroupByGUID($guid, $baseDn = null, $scope = Ldap::SEARCH_SCOPE_SUB, $attributes = []) |
||
134 | { |
||
135 | } |
||
136 | |||
137 | public function getUsers($baseDn = null, $scope = Ldap::SEARCH_SCOPE_SUB, $attributes = [], $sort = '') |
||
138 | { |
||
139 | } |
||
140 | |||
141 | public function getUserByGUID($guid, $baseDn = null, $scope = Ldap::SEARCH_SCOPE_SUB, $attributes = []) |
||
142 | { |
||
143 | return [self::$data['users'][$guid]]; |
||
144 | } |
||
145 | |||
146 | public function update($dn, array $attributes) |
||
147 | { |
||
148 | } |
||
149 | |||
150 | public function delete($dn, $recursively = false) |
||
151 | { |
||
152 | } |
||
153 | |||
154 | public function move($fromDn, $toDn, $recursively = false) |
||
155 | { |
||
156 | } |
||
157 | |||
158 | public function add($dn, array $attributes) |
||
159 | { |
||
160 | } |
||
161 | |||
162 | protected function search($filter, $baseDn = null, $scope = Ldap::SEARCH_SCOPE_SUB, $attributes = [], $sort = '') |
||
163 | { |
||
164 | $records = self::$data; |
||
165 | $results = []; |
||
166 | foreach ($records as $record) { |
||
167 | foreach ($record as $attribute => $value) { |
||
168 | // if the value is an array with a single value, e.g. 'samaccountname' => array(0 => 'myusername') |
||
169 | // then make sure it's just set in the results as 'samaccountname' => 'myusername' so that it |
||
170 | // can be used directly by ArrayData |
||
171 | if (is_array($value) && count($value) == 1) { |
||
172 | $value = $value[0]; |
||
173 | } |
||
174 | |||
175 | // ObjectGUID and ObjectSID attributes are in binary, we need to convert those to strings |
||
176 | if ($attribute == 'objectguid') { |
||
177 | $value = LDAPUtil::bin_to_str_guid($value); |
||
0 ignored issues
–
show
The type
SilverStripe\LDAP\Tests\Model\LDAPUtil was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths ![]() |
|||
178 | } |
||
179 | if ($attribute == 'objectsid') { |
||
180 | $value = LDAPUtil::bin_to_str_sid($value); |
||
181 | } |
||
182 | |||
183 | $record[$attribute] = $value; |
||
184 | } |
||
185 | |||
186 | $results[] = $record; |
||
187 | } |
||
188 | |||
189 | return $results; |
||
190 | } |
||
191 | |||
192 | /** |
||
193 | * Mock to search trough dummy $data. |
||
194 | * |
||
195 | * @param string $email |
||
196 | * @param null $baseDn |
||
0 ignored issues
–
show
|
|||
197 | * @param int $scope |
||
198 | * @param array $attributes |
||
199 | * @return array |
||
200 | */ |
||
201 | public function getUserByEmail($email, $baseDn = null, $scope = Ldap::SEARCH_SCOPE_SUB, $attributes = []) |
||
202 | { |
||
203 | $result = []; |
||
204 | foreach (self::$data['users'] as $guid => $info) { |
||
205 | if ($info['mail'] == $email) { |
||
206 | $result[] = $info; |
||
207 | break; |
||
208 | } |
||
209 | } |
||
210 | |||
211 | return $result; |
||
212 | } |
||
213 | |||
214 | /** |
||
215 | * Mock to search trough dummy $data. |
||
216 | * |
||
217 | * @param string $username |
||
218 | * @param null $baseDn |
||
0 ignored issues
–
show
|
|||
219 | * @param int $scope |
||
220 | * @param array $attributes |
||
221 | * @return array |
||
222 | * @internal param string $email |
||
223 | */ |
||
224 | public function getUserByUsername($username, $baseDn = null, $scope = Ldap::SEARCH_SCOPE_SUB, $attributes = []) |
||
225 | { |
||
226 | $result = []; |
||
227 | foreach (self::$data['users'] as $guid => $info) { |
||
228 | if ($info['userprincipalname'] == $username) { |
||
229 | $result[] = $info; |
||
230 | break; |
||
231 | } |
||
232 | } |
||
233 | |||
234 | return $result; |
||
235 | } |
||
236 | } |
||
237 |