Blame view

src/Model/Table/LdapConnectionsTable.php 6.98 KB
e1f6c5b7   Alexandre   Version: 2.3.0.0
1
2
3
4
5
6
<?php
namespace App\Model\Table;

use Cake\ORM\Table;
use Cake\ORM\TableRegistry;
use Cake\Auth\DefaultPasswordHasher;
c1cbc061   Thibaud Ajas   Bugfixes, correct...
7
use Cake\Core\Exception\Exception;
e1f6c5b7   Alexandre   Version: 2.3.0.0
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

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']],
e1f6c5b7   Alexandre   Version: 2.3.0.0
49
50
51
52
53
54
55
56
57
				'givenname' => [$names[1]],
				$this->authenticationType => [$user['username']],
				'userpassword' => [$user['password']],
				];
			}
			else {
				$ldapUsers[] = [
				'sn' => [$names[0]],
				'mail' => [$user['email']],
e1f6c5b7   Alexandre   Version: 2.3.0.0
58
59
60
61
62
63
64
65
66
67
68
69
				'givenname' => " ",
				$this->authenticationType => [$user['username']],
				'userpassword' => [$user['password']],
				];
			}
		}

		$prefix = "_NouvelUtilisateur_";
		$ldapUsers[] = [
				'sn' => ['NOUVEL'],
				'givenname' => ['UTILISATEUR'],
				'mail' => [$prefix.'email'],
e1f6c5b7   Alexandre   Version: 2.3.0.0
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
				$this->authenticationType => [$prefix.'username'],
				'userpassword' => [$prefix.'password'],
		];
		
		
		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;
		}
		
		$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;
		}

c1cbc061   Thibaud Ajas   Bugfixes, correct...
107
		throw new Exception ('The ldap configuration is not valid : <br />
e1f6c5b7   Alexandre   Version: 2.3.0.0
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
			<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) {
		try {

			if($this->checkConfiguration()) {
				if ($this->USE_LDAP) {
					$ldapConnection = ldap_connect($this->host, $this->port);
					ldap_set_option($ldapConnection, LDAP_OPT_PROTOCOL_VERSION, 3);
c3b1f028   Alexandre   Version: 2.4.3.11
151
					$results = ldap_search($ldapConnection, $this->baseDn, '('.$this->authenticationType . '=' . $userName.')');
e1f6c5b7   Alexandre   Version: 2.3.0.0
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
					return ldap_get_entries($ldapConnection, $results);
				}
				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) {
3a1d23a5   Alexandre   Version: 2.4.3.5
169
			if ($login == $user[$this->authenticationType][0]) return $user;
e1f6c5b7   Alexandre   Version: 2.3.0.0
170
171
172
		}
		return FALSE;
	}
3a1d23a5   Alexandre   Version: 2.4.3.5
173
	 
e1f6c5b7   Alexandre   Version: 2.3.0.0
174
	/**
3f5e058d   Alexandre   Version: 2.4.4.2
175
	 * Return a list of Users with key = username & value = username
e1f6c5b7   Alexandre   Version: 2.3.0.0
176
177
178
179
180
181
182
	 */
	public function getListUsers() {
		$u = $this->getAllLdapUsers();
		$utilisateurs= [];
		
		if($this->USE_LDAP) {
			for($i = 0; $i < $u['count']; $i++) {
3f179b66   Alexandre   Version: 2.5.4.2
183
				$utilisateurs[$u[$i]['sn'][0].' '.$u[$i]['givenname'][0]] = $u[$i]['sn'][0].' '.$u[$i]['givenname'][0];
e1f6c5b7   Alexandre   Version: 2.3.0.0
184
185
186
187
			}
		}
		else {
			for($i = 0; $i < sizeof($u)-1; $i++) {
3f179b66   Alexandre   Version: 2.5.4.2
188
				$utilisateurs[$u[$i]['sn'][0].' '.$u[$i]['givenname'][0]] = $u[$i]['sn'][0].' '.$u[$i]['givenname'][0];
e1f6c5b7   Alexandre   Version: 2.3.0.0
189
190
191
			}
		}
		
1f42188e   Alexandre   Version: 2.4.4.4
192
		return $utilisateurs;
e1f6c5b7   Alexandre   Version: 2.3.0.0
193
194
195
	}
	
	/**
c3b1f028   Alexandre   Version: 2.4.3.11
196
197
198
199
200
201
202
203
	 * 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++) {
3f179b66   Alexandre   Version: 2.5.4.2
204
				$utilisateurs[$u[$i]['sn'][0].' '.$u[$i]['givenname'][0]] = $u[$i][$this->authenticationType][0];
c3b1f028   Alexandre   Version: 2.4.3.11
205
206
207
208
			}
		}
		else {
			for($i = 0; $i < sizeof($u)-1; $i++) {
3f179b66   Alexandre   Version: 2.5.4.2
209
				$utilisateurs[$u[$i]['sn'][0].' '.$u[$i]['givenname'][0]] = $u[$i][$this->authenticationType][0];
c3b1f028   Alexandre   Version: 2.4.3.11
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
			}
		}
	
		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++) {
a0fefb3d   Thibaud Ajas   bugfixes suite au...
225
			    if(isset($u[$i]['mail'][0])) {
3f179b66   Alexandre   Version: 2.5.4.2
226
					$utilisateurs[$u[$i]['sn'][0].' '.$u[$i]['givenname'][0]] = $u[$i]['mail'][0];
c3b1f028   Alexandre   Version: 2.4.3.11
227
228
				}
				else {
3f179b66   Alexandre   Version: 2.5.4.2
229
					$utilisateurs[$u[$i]['sn'][0].' '.$u[$i]['givenname'][0]] = 'N/A';
c3b1f028   Alexandre   Version: 2.4.3.11
230
231
232
233
234
				}
			}
		}
		else {
			for($i = 0; $i < sizeof($u)-1; $i++) {
3f179b66   Alexandre   Version: 2.5.4.2
235
				$utilisateurs[$u[$i]['sn'][0].' '.$u[$i]['givenname'][0]] = $u[$i]['mail'][0];
c3b1f028   Alexandre   Version: 2.4.3.11
236
237
238
239
240
241
242
			}
		}
	
		return $utilisateurs;
	}
	
	/**
e1f6c5b7   Alexandre   Version: 2.3.0.0
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
	 * 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;
	}
	
	public function ldapAuthentication($login, $password) {

		try {
			if($this->checkConfiguration()) {
				
				if ($this->USE_LDAP) {
					if (strlen(trim($password))==0) return FALSE;
e1f6c5b7   Alexandre   Version: 2.3.0.0
264
265
266
267
					$ldapConnection = ldap_connect($this->host, $this->port);
					ldap_set_option($ldapConnection, LDAP_OPT_PROTOCOL_VERSION, 3);				
				    if (@ldap_bind($ldapConnection, $this->authenticationType . '=' . $login . ',' . $this->baseDn, $password)) {
				    	return $this->getUserAttributes($login)[0];
7df3c2ba   Etienne Pallier   schémas BD (png, ...
268
				    /*
e1f6c5b7   Alexandre   Version: 2.3.0.0
269
				    } else {
dddf7ec7   Etienne Pallier   bugfix ldap conne...
270
				    	return false;
7df3c2ba   Etienne Pallier   schémas BD (png, ...
271
				    */
e1f6c5b7   Alexandre   Version: 2.3.0.0
272
				    }
e1f6c5b7   Alexandre   Version: 2.3.0.0
273
274
275
				}
				else {
					$user = $this->getFakeLdapUser($login);
771aa727   Alexandre   Version: 2.3.2.0
276
					if ($user != false && (new DefaultPasswordHasher)->check($password, $user['userpassword'][0])) {
c1cbc061   Thibaud Ajas   Bugfixes, correct...
277
						//if ($user != false && $user['userpassword'][0] == $password) {
e1f6c5b7   Alexandre   Version: 2.3.0.0
278
279
280
281
282
283
						return $user;
					}
				}
			}
		}
		catch (Exception $e) { }
7df3c2ba   Etienne Pallier   schémas BD (png, ...
284
		return FALSE;
e1f6c5b7   Alexandre   Version: 2.3.0.0
285
286
287
288
	}
	
}
?>