Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
132 changes: 132 additions & 0 deletions Server-Side Components/Script Includes/Query ldap server/LDAPquery.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
/**
* LDAPquery class provides a fluent interface for querying LDAP entries.
* It wraps GlideLDAP and allows configuration of RDN, filters, pagination, and server config.
* @class
*
* @example
* Example 1: Retrieve a single LDAP record
* var result = new LDAPquery()
* .setRDN('')
* .setFilter('uid=einstein')
* .setConfig('2fd003c083e07e10557ff0d6feaad3d7')
* .getOne();
* gs.info(result.telephoneNumber);
*
* @example
* Example 2: Retrieve multiple LDAP records with pagination
* var result = new LDAPquery()
* .setRDN('ou=scientists')
* .setPagination(1000)
* .setConfig('2fd003c083e07e10557ff0d6feaad3d7')
* .getIterable();
*
* var item;
* while (item = result.next()) {
* gs.info(JSON.stringify(j2js(item), null, 2));
* }
*/
var LDAPquery = Class.create();

/**
* @typedef {Object} LDAPquery
* @property {string} ldapConfig - The sys_id of the LDAP server config.
* @property {string} rdn - The relative distinguished name to start the query from.
* @property {string} filter - LDAP filter string.
* @property {boolean} boolean - Boolean flag used in query logic.
* @property {number} rowsPerPage - Number of rows to return per page.
* @property {GlideLDAP} glideLdap - GlideLDAP instance used to perform queries.
*/
LDAPquery.prototype = {
/**
* Initializes the LDAPquery instance with default values.
*/
initialize: function () {
this.ldapConfig = '';
this.rdn = '';
this.filter = '';
this.boolean = true;
this.rowsPerPage = 1;
this.glideLdap = new GlideLDAP();
},

/**
* Sets the relative distinguished name (RDN) for the query.
* @param {string} rdn - The RDN string.
* @returns {LDAPquery} Returns the current instance for chaining.
*/
setRDN: function (rdn) {
this.rdn = rdn || '';
return this;
},

/**
* Sets the LDAP filter string.
* @param {string} filter - The LDAP filter.
* @returns {LDAPquery} Returns the current instance for chaining.
*/
setFilter: function (filter) {
this.filter = filter || '';
return this;
},

/**
* Sets the number of rows to return per page.
* @param {number} rows - Number of rows.
* @returns {LDAPquery} Returns the current instance for chaining.
*/
setPagination: function (rows) {
this.rowsPerPage = rows || 1;
return this;
},

/**
* Sets the LDAP server configuration using its sys_id.
* Also sets the config on the GlideLDAP instance.
* @param {string} config - The sys_id of the LDAP server config.
* @returns {LDAPquery} Returns the current instance for chaining.
*/
setConfig: function (config) {
this.ldapConfig = config || '';
this.glideLdap.setConfigID(config);
return this;
},

/**
* Executes the query and returns the first matching LDAP entry.
* @returns {Object|null} Returns the first matching entry as a JavaScript object, or null if none found.
* @throws Will raise an error if ldapConfig is not set.
*/
getOne: function () {
if (!this.ldapConfig) {
NiceError.raise('no ldap config defined');
}
var entry;
if (entry = this._query().next()) {
return j2js(entry);
}
return null;
},

/**
* Executes the query and returns an iterable result set.
* @returns Returns a iterator over matching LDAP entries.
* @throws Will raise an error if ldapConfig is not set.
*/
getIterable: function () {
if (!this.ldapConfig) {
NiceError.raise('no ldap config defined');
}
return this._query();
},

/**
* Internal method to perform the LDAP query using GlideLDAP.
* @private
* @returns Returns the result iterator.
*/
_query: function () {
return this.glideLdap.getMatching(this.rdn, this.filter, this.boolean, this.rowsPerPage);
},

type: 'LDAPquery'
};
56 changes: 56 additions & 0 deletions Server-Side Components/Script Includes/Query ldap server/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# Query ldap server by config sys_id
Interact with GlideLDAP using a fluent wrapper. Handy for querying ldap server for non-persistent data for example via remote tables.

# How to use it?
Configure LDAP server in *ldap_server_config* table
Query server from serverside script using script include LDAPQuery

# Example 1: get one entry

```javascript
var result = new LDAPquery()
.setRDN('') //set relative start
.setFilter('uid=einstein') //set filter
.setConfig('2fd003c083e07e10557ff0d6feaad3d7') //set the sys_id for ldap_server_config record
.getOne(); //returns first entry as js object
gs.info(result.telephoneNumber);
```
```
output:
*** Script: 314-159-2653
```

# Example 2: get the iterable for query

```javascript
var result = new LDAPquery()
.setRDN('ou=scientists')
.setPagination(1000) //set how many records per page
.setConfig('2fd003c083e07e10557ff0d6feaad3d7')
.getIterable(); //returns iterable result object

var item;
while (item = result.next()) {
gs.info(JSON.stringify(j2js(item), null, 2)); //note that next returns a java object, hence the j2js to convert to js
}
```
```
output:
*** Script: {
"dn": "ou=scientists,dc=example,dc=com",
"objectClass": "groupOfUniqueNames^top",
"ou": "scientists",
"source": "ldap:ou=scientists,dc=example,dc=com",
"uniqueMember": "uid=einstein,dc=example,dc=com^uid=tesla,dc=example,dc=com^uid=newton,dc=example,dc=com^uid=galileo,dc=example,dc=com",
"cn": "Scientists"
}
*** Script: {
"dn": "ou=italians,ou=scientists,dc=example,dc=com",
"objectClass": "groupOfUniqueNames^top",
"ou": "italians",
"source": "ldap:ou=italians,ou=scientists,dc=example,dc=com",
"uniqueMember": "uid=tesla,dc=example,dc=com",
"cn": "Italians"
}
LDAP SEARCH: >>Next Page
```
Loading