Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 619b8355 authored by theronakpatel's avatar theronakpatel
Browse files

getCreateTimestamp function added for LDAP

parent 6606271f
Loading
Loading
Loading
Loading
Loading
+38 −0
Original line number Diff line number Diff line
@@ -122,4 +122,42 @@ 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;
		}
	}

}