Skip to content

Commit 282e2d2

Browse files
author
David Coutadeur
committed
refactor isAccountValid, getStartDate and getEndDate functions (#68)
1 parent 984c1d8 commit 282e2d2

File tree

4 files changed

+55
-172
lines changed

4 files changed

+55
-172
lines changed

src/Ltb/Directory.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,15 +99,15 @@ public function getDnAttribute() : string;
9999
/*
100100
* Is account valid? Relies on start and end validity dates
101101
*/
102-
public function isAccountValid($ldap, $dn) : bool;
102+
public function isAccountValid($entry, $pwdPolicyConfiguration) : bool;
103103

104104
/*
105105
* Get validity start date
106106
*/
107-
public function getStartDate($ldap, $dn) : ?DateTime;
107+
public function getStartDate($entry, $pwdPolicyConfiguration) : ?DateTime;
108108

109109
/*
110110
* Get validity end date
111111
*/
112-
public function getEndDate($ldap, $dn) : ?DateTime;
112+
public function getEndDate($entry, $pwdPolicyConfiguration) : ?DateTime;
113113
}

src/Ltb/Directory/ActiveDirectory.php

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -305,11 +305,11 @@ public function getDnAttribute() : string {
305305
return "distinguishedName";
306306
}
307307

308-
public function isAccountValid($ldap, $dn) : bool {
308+
public function isAccountValid($entry, $pwdPolicyConfiguration) : bool {
309309

310310
$time = time();
311-
$startdate = $this->getStartDate($ldap, $dn);
312-
$enddate = $this->getEndDate($ldap, $dn);
311+
$startdate = $this->getStartDate($entry, $pwdPolicyConfiguration);
312+
$enddate = $this->getEndDate($entry, $pwdPolicyConfiguration);
313313

314314
if ( isset($startdate) ) {
315315
if ( $time <= $startdate->getTimestamp() ) {
@@ -326,29 +326,19 @@ public function isAccountValid($ldap, $dn) : bool {
326326
return true;
327327
}
328328

329-
public function getStartDate($ldap, $dn) : ?DateTime {
329+
public function getStartDate($entry, $pwdPolicyConfiguration) : ?DateTime {
330330

331331
// No start date in AD
332332
return null;
333333
}
334334

335-
public function getEndDate($ldap, $dn) : ?DateTime {
335+
public function getEndDate($entry, $pwdPolicyConfiguration) : ?DateTime {
336336

337-
$search = \Ltb\PhpLDAP::ldap_read($ldap, $dn, "(objectClass=*)", array('accountExpires'));
338-
$errno = \Ltb\PhpLDAP::ldap_errno($ldap);
339-
340-
if ( $errno ) {
341-
error_log("LDAP - Search error $errno (".ldap_error($ldap).")");
342-
return null;
343-
} else {
344-
$entry = \Ltb\PhpLDAP::ldap_get_entries($ldap, $search);
345-
}
346-
347-
if (!isset($entry[0]['accountexpires']) or ($entry[0]['accountexpires'][0] == 0) or ($entry[0]['accountexpires'][0] == 9223372036854775807)) {
337+
if (!isset($entry['accountexpires']) or ($entry['accountexpires'][0] == 0) or ($entry['accountexpires'][0] == 9223372036854775807)) {
348338
return null;
349339
}
350340

351-
$enddate = \Ltb\Date::adDate2phpDate($entry[0]['accountexpires'][0]);
341+
$enddate = \Ltb\Date::adDate2phpDate($entry['accountexpires'][0]);
352342
return $enddate ? $enddate : null;
353343
}
354344
}

src/Ltb/Directory/OpenLDAP.php

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -343,11 +343,11 @@ public function getDnAttribute() : string {
343343
return "entryDn";
344344
}
345345

346-
public function isAccountValid($ldap, $dn) : bool {
346+
public function isAccountValid($entry, $pwdPolicyConfiguration) : bool {
347347

348348
$time = time();
349-
$startdate = $this->getStartDate($ldap, $dn);
350-
$enddate = $this->getEndDate($ldap, $dn);
349+
$startdate = $this->getStartDate($entry, $pwdPolicyConfiguration);
350+
$enddate = $this->getEndDate($entry, $pwdPolicyConfiguration);
351351

352352
if ( isset($startdate) ) {
353353
if ( $time <= $startdate->getTimestamp() ) {
@@ -364,41 +364,23 @@ public function isAccountValid($ldap, $dn) : bool {
364364
return true;
365365
}
366366

367-
public function getStartDate($ldap, $dn) : ?DateTime {
367+
public function getStartDate($entry, $pwdPolicyConfiguration) : ?DateTime {
368368

369369
$startdate = null;
370-
$search = \Ltb\PhpLDAP::ldap_read($ldap, $dn, "(objectClass=*)", array('pwdStartTime'));
371-
$errno = \Ltb\PhpLDAP::ldap_errno($ldap);
372-
373-
if ( $errno ) {
374-
error_log("LDAP - Search error $errno (".ldap_error($ldap).")");
375-
return null;
376-
} else {
377-
$entry = \Ltb\PhpLDAP::ldap_get_entries($ldap, $search);
378-
}
379370

380-
if ( isset($entry[0]['pwdstarttime']) ) {
381-
$startdate = \Ltb\Date::ldapDate2phpDate($entry[0]['pwdstarttime'][0]);
371+
if ( isset($entry['pwdstarttime']) ) {
372+
$startdate = \Ltb\Date::ldapDate2phpDate($entry['pwdstarttime'][0]);
382373
}
383374

384375
return $startdate ? $startdate : null;
385376
}
386377

387-
public function getEndDate($ldap, $dn) : ?DateTime {
378+
public function getEndDate($entry, $pwdPolicyConfiguration) : ?DateTime {
388379

389380
$enddate = null;
390-
$search = \Ltb\PhpLDAP::ldap_read($ldap, $dn, "(objectClass=*)", array('pwdEndTime'));
391-
$errno = \Ltb\PhpLDAP::ldap_errno($ldap);
392-
393-
if ( $errno ) {
394-
error_log("LDAP - Search error $errno (".ldap_error($ldap).")");
395-
return null;
396-
} else {
397-
$entry = \Ltb\PhpLDAP::ldap_get_entries($ldap, $search);
398-
}
399381

400-
if ( isset($entry[0]['pwdendtime']) ) {
401-
$enddate = \Ltb\Date::ldapDate2phpDate($entry[0]['pwdendtime'][0]);
382+
if ( isset($entry['pwdendtime']) ) {
383+
$enddate = \Ltb\Date::ldapDate2phpDate($entry['pwdendtime'][0]);
402384
}
403385

404386
return $enddate ? $enddate : null;

0 commit comments

Comments
 (0)