LdapConnectionsTable.php
13.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
<?php
namespace App\Model\Table;
use Cake\ORM\Table;
use Cake\ORM\TableRegistry;
use Cake\Auth\DefaultPasswordHasher;
use Cake\Core\Exception\Exception;
class LdapConnectionsTable extends AppTable
{
public $useTable = false;
private $host;
private $port;
private $baseDn;
private $authenticationType;
private $filter;
private $USE_LDAP = TRUE;
private $fakeLDAPUsers = [];
public function __construct()
{
parent::__construct();
}
// EP
public function useFakeLdap()
{
return ! $this->useLdap();
}
public function useLdap()
{
$this->checkConfiguration();
return $this->USE_LDAP;
}
private function buildFakeLdapUsers()
{
return $this->buildFakeLdapUsersFromDB();
}
private function buildFakeLdapUsersFromDB()
{
$users = TableRegistry::get('Users')->find();
$ldapUsers = [];
foreach ($users as $user) {
$names = explode(" ", $user['nom']);
if (isset($names[1])) {
$ldapUsers[] = [
'sn' => [
$names[0]
],
'mail' => [
$user['email']
],
'givenname' => [
$names[1]
],
$this->authenticationType => [
$user['username']
],
'userpassword' => [
$user['password']
]
];
} else {
$ldapUsers[] = [
'sn' => [
$names[0]
],
'mail' => [
$user['email']
],
'givenname' => " ",
$this->authenticationType => [
$user['username']
],
'userpassword' => [
$user['password']
]
];
}
}
// EP (aout 2017)
// ATTENTION : Utilisateur IMPORTANT.
// Avec cet utilisateur, on simule un utilisateur qui n'est PAS dans la table utilisateurs
// Il devrait donc se voir attribuer un role "Utilisateur" sans pour autant que ça soit écrit dans la table !!!
// login = '_NouvelUtilisateur_username'
// pass = '_NouvelUtilisateur_password'
// $prefix = "_NouvelUtilisateur_";
$ldapUsers[] = [
'sn' => [
'UTILISATEUR'
],
'givenname' => [
'FAKE_LDAP'
],
// 'mail' => [$login.'email'],
'mail' => [
'fakeldapuser@domain.fr'
],
// $this->authenticationType => [$prefix.'username'],
$this->authenticationType => [
$this->getTheFakeLdapUser()['login']
],
// $this->authenticationType => ['usere'],
'userpassword' => [
$this->getTheFakeLdapUser()['pass']
]
// 'userpassword' => ['toto'],
];
return $ldapUsers;
}
private function checkConfiguration()
{
$config = TableRegistry::get('Configurations')->find()
->where([
'id =' => 1
])
->first();
$this->USE_LDAP = $config->use_ldap ? TRUE : FALSE;
if (! $this->USE_LDAP) {
$this->authenticationType = $config->authentificationType_ldap;
if (empty($this->fakeLDAPUsers))
$this->fakeLDAPUsers = $this->buildFakeLdapUsers();
return true;
}
// debug($this->fakeLDAPUsers);
$ldapConfig = $config->toArray();
if (! empty($config->host_ldap) && ! empty($config->port_ldap) && ! empty($config->baseDn_ldap) && ! empty($config->authentificationType_ldap) && ! empty($config->filter_ldap)) {
$this->host = $config->host_ldap;
$this->port = $config->port_ldap;
$this->baseDn = $config->baseDn_ldap;
$this->filter = $config->filter_ldap;
$this->authenticationType = $config->authentificationType_ldap;
return true;
}
throw new Exception('The ldap configuration is not valid : <br />
<ul>
<li>host = ' . @$ldapConfig['host'] . '</li>
<li>port = ' . @$ldapConfig['port'] . '</li>
<li>baseDn = ' . @$ldapConfig['baseDn'] . '</li>
<li>filter = ' . @$ldapConfig['filter'] . '</li>
<li>authenticationType = ' . @$ldapConfig['authenticationType'] . '</li>
</ul>');
}
public function getAllLdapUsers()
{
try {
if ($this->checkConfiguration()) {
// REAL LDAP
if ($this->USE_LDAP) {
$ldapConnection = ldap_connect($this->host, $this->port);
ldap_set_option($ldapConnection, LDAP_OPT_PROTOCOL_VERSION, 3);
$results = ldap_search($ldapConnection, $this->baseDn, $this->filter);
$res = ldap_get_entries($ldapConnection, $results);
} // FAKE LDAP
else {
$res = $this->fakeLDAPUsers;
}
return $res;
}
} catch (Exception $e) {}
return false;
}
// $userName = login
public function getUserAttributes($userName, $LDAP_ANONYMOUS=true, $filter='', $just_these=[])
{
try {
if ($this->checkConfiguration()) {
if ($this->USE_LDAP) {
/*
$ldapConnection = ldap_connect($this->host, $this->port);
ldap_set_option($ldapConnection, LDAP_OPT_PROTOCOL_VERSION, 3);
*/
/* CRAL
*/
//$results = ldap_search($ldapConnection, $this->baseDn, $filter);
$results = ldap_search($ldapConnection, $this->baseDn, $filter, $just_these) or die("Could not search to LDAP server response was: " . ldap_error($ldapconn) );
$info = ldap_get_entries($ldapConnection, $results);
//echo $info["count"]." entries returned\n";
return $info;
} else
return array(
$this->getFakeLdapUser($userName)
);
}
} catch (Exception $e) {}
return false;
}
public function getAuthenticationType()
{
return $this->authenticationType;
}
// EP added
public function getFakeLdapUser($login)
{
foreach ($this->fakeLDAPUsers as $user) {
if ($login == $user[$this->authenticationType][0])
return $user;
}
return FALSE;
}
/**
* Return a list of Users with key = username & value = username
*/
public function getListUsers()
{
$u = $this->getAllLdapUsers();
$utilisateurs = [];
if ($this->USE_LDAP) {
for ($i = 0; $i < $u['count']; $i ++) {
$utilisateurs[$u[$i]['sn'][0] . ' ' . $u[$i]['givenname'][0]] = $u[$i]['sn'][0] . ' ' . $u[$i]['givenname'][0];
}
} else {
for ($i = 0; $i < sizeof($u) - 1; $i ++) {
$utilisateurs[$u[$i]['sn'][0] . ' ' . $u[$i]['givenname'][0]] = $u[$i]['sn'][0] . ' ' . $u[$i]['givenname'][0];
}
}
return $utilisateurs;
}
/**
* Return a list of login ofUsers with key = username & value = login
*/
public function getListLoginUsers()
{
$u = $this->getAllLdapUsers();
$utilisateurs = [];
if ($this->USE_LDAP) {
for ($i = 0; $i < $u['count']; $i ++) {
$utilisateurs[$u[$i]['sn'][0] . ' ' . $u[$i]['givenname'][0]] = $u[$i][$this->authenticationType][0];
}
} else {
for ($i = 0; $i < sizeof($u) - 1; $i ++) {
$utilisateurs[$u[$i]['sn'][0] . ' ' . $u[$i]['givenname'][0]] = $u[$i][$this->authenticationType][0];
}
}
return $utilisateurs;
}
/**
* Return a list of mail of Users with key = username & value = mail
*/
public function getListEmailUsers()
{
$u = $this->getAllLdapUsers();
$utilisateurs = [];
if ($this->USE_LDAP) {
for ($i = 0; $i < $u['count']; $i ++) {
if (isset($u[$i]['mail'][0])) {
$utilisateurs[$u[$i]['sn'][0] . ' ' . $u[$i]['givenname'][0]] = $u[$i]['mail'][0];
} else {
$utilisateurs[$u[$i]['sn'][0] . ' ' . $u[$i]['givenname'][0]] = 'N/A';
}
}
} else {
for ($i = 0; $i < sizeof($u) - 1; $i ++) {
$utilisateurs[$u[$i]['sn'][0] . ' ' . $u[$i]['givenname'][0]] = $u[$i]['mail'][0];
}
}
return $utilisateurs;
}
/**
* Return size of list users
*/
public function getNbUsers()
{
$u = $this->getAllLdapUsers();
if ($this->USE_LDAP) {
$nbUsers = $u['count'];
} else {
$nbUsers = sizeof($u) - 1;
}
return $nbUsers;
}
// Utilisateur du ldap qui n'est pas dans la table utilisateurs
// => il a donc le role "Utilisateur" PAR DEFAUT
private function getTheFakeLdapUser()
{
return [
'login' => '_fake_ldap_user_',
'pass' => '_fake_ldap_user_pass'
];
}
public function ldapAuthentication($user_login, $user_password)
{
try {
if ($this->checkConfiguration()) {
// We are using LDAP
if ($this->USE_LDAP) {
$LDAP_ANONYMOUS = true;
// No connexion allowed without password
if (strlen(trim($user_password)) == 0) return FALSE;
// Set LDAP parameters
// - Anonymous connection (IRAP, IAS, LATMOS)
if ($LDAP_ANONYMOUS) {
//$dn = "ou=users,dc=irap,dc=omp,dc=eu";
//$dn = $this->baseDn;
$binddn = $this->authenticationType . '=' . $user_login;
$ldappass = $user_password;
$filter = '('.$binddn.')';
$just_these = array("cn");
}
// - Authentified connection (CRAL)
else {
//$dn = "dc=univ-lyon1,dc=fr";
//$binddn="CN=svc_ldap_cral,OU=users,OU=27,OU=sim,OU=univ-lyon1,DC=univ-lyon1,DC=fr";
//$binddn = "CN=svc_ldap_cral,OU=users,OU=27,OU=sim,OU=univ-lyon1,".$dn;
$binddn = "CN=svc_ldap_cral,OU=users,OU=27,OU=sim,OU=univ-lyon1";
$ldappass = "lemotdepasse";
$filter = "(&(objectClass=person)(memberOf:1.2.840.113556.1.4.1941:=cn=ucbl.osu.cral,ou=groups,ou=27,ou=sim,ou=univ-lyon1,dc=univ-lyon1,dc=fr))";
//$just_these = array();
$just_these = [];
}
$binddn .= ','.$this->baseDn;
// Connection
$ldapConnection = ldap_connect($this->host, $this->port) or die("Could not connect to $this->host (port $this->port)");
if ($ldapConnection) {
ldap_set_option($ldapConnection, LDAP_OPT_PROTOCOL_VERSION, 3);
// Binding
//if (@ldap_bind($ldapConnection, $this->authenticationType . '=' . $login . ',' . $this->baseDn, $password)) {
$ldapbind = ldap_bind($ldapConnection, $binddn, $ldappass) or die( "Could not bind to LDAP server.". ldap_error($ldapConnection) );
if ($ldapbind) {
//return $this->getUserAttributes($login, $LDAP_ANONYMOUS, $filter, $just_these)[0];
$search = $this->getUserAttributes($user_login, $LDAP_ANONYMOUS, $filter, $just_these) or die("Could not search in LDAP server, response was: " . ldap_error($ldapConnection) );
return $search[0];
/*
* } else {
* return false;
*/
}
}
// We are not using LDAP (use FAKE LDAP instead)
} else {
$user = $this->getFakeLdapUser($user_login);
// debug($user);
if ($user === false)
return FALSE;
// $this->authenticationType peut valoir "uid" ou "cn"... (par défaut "uid" pour le fake ldap, à confirmer...)
// if ($user['uid'][0] == "_NouvelUtilisateur_username" && $user['userpassword'][0] == "_NouvelUtilisateur_password") return $user;
// if ($user[$this->authenticationType][0] == "_NouvelUtilisateur_username" && $user['userpassword'][0] == "_NouvelUtilisateur_password") return $user;
if ($user[$this->authenticationType][0] == $this->getTheFakeLdapUser()['login'] && $user['userpassword'][0] == $this->getTheFakeLdapUser()['pass'])
return $user;
if ((new DefaultPasswordHasher())->check($user_password, $user['userpassword'][0]))
return $user;
// if ($user != false && $user['userpassword'][0] == $password) {
}
}
} catch (Exception $e) {}
return FALSE;
}
}
?>