diff --git a/lib/Service/LDAPConnectionService.php b/lib/Service/LDAPConnectionService.php index f7609941af21cc77e50b6ec2de74a61732f0fa0f..75bead435a4c2c8e0d5ac97965c248fd66c28184 100644 --- a/lib/Service/LDAPConnectionService.php +++ b/lib/Service/LDAPConnectionService.php @@ -122,4 +122,41 @@ class LDAPConnectionService { $this->closeLDAPConnection($conn); } + + /** + * Method getCreateTimestamp + * + * @param string $username [explicite description] + * + * @return string + */ + public function getCreateTimestamp(string $username): ?string { + try { + $conn = $this->getLDAPConnection(); + $baseDn = implode(',', $this->getLDAPBaseUsers()); + $filter = sprintf('(username=%s)', ldap_escape($username, '', LDAP_ESCAPE_FILTER)); + $attributes = ['createTimestamp']; + + $search = ldap_search($conn, $baseDn, $filter, $attributes); + if (!$search) { + throw new Exception("LDAP search failed for user: $username"); + } + + $entries = ldap_get_entries($conn, $search); + $this->closeLDAPConnection($conn); + + if ($entries['count'] === 0 || empty($entries[0]['createtimestamp'][0])) { + return null; + } + + // Format: 20220817084557Z -> 2022-08-17 08:45:57 + $raw = $entries[0]['createtimestamp'][0]; + $date = \DateTime::createFromFormat('YmdHis\Z', $raw); + return $date ? $date->format('Y-m-d H:i:s') : null; + } catch (Exception $e) { + // Log or handle error as needed + return null; + } + } + }