Skip to content

Commit ceaf255

Browse files
authored
Scriptinclude/ldapquery (#1962)
* Create LDAPquery.js * Create README.md * Update README.md * Update README.md * Update LDAPquery.js * Update README.md
1 parent 987da68 commit ceaf255

File tree

2 files changed

+188
-0
lines changed

2 files changed

+188
-0
lines changed
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
/**
2+
* LDAPquery class provides a fluent interface for querying LDAP entries.
3+
* It wraps GlideLDAP and allows configuration of RDN, filters, pagination, and server config.
4+
* @class
5+
*
6+
* @example
7+
* Example 1: Retrieve a single LDAP record
8+
* var result = new LDAPquery()
9+
* .setRDN('')
10+
* .setFilter('uid=einstein')
11+
* .setConfig('2fd003c083e07e10557ff0d6feaad3d7')
12+
* .getOne();
13+
* gs.info(result.telephoneNumber);
14+
*
15+
* @example
16+
* Example 2: Retrieve multiple LDAP records with pagination
17+
* var result = new LDAPquery()
18+
* .setRDN('ou=scientists')
19+
* .setPagination(1000)
20+
* .setConfig('2fd003c083e07e10557ff0d6feaad3d7')
21+
* .getIterable();
22+
*
23+
* var item;
24+
* while (item = result.next()) {
25+
* gs.info(JSON.stringify(j2js(item), null, 2));
26+
* }
27+
*/
28+
var LDAPquery = Class.create();
29+
30+
/**
31+
* @typedef {Object} LDAPquery
32+
* @property {string} ldapConfig - The sys_id of the LDAP server config.
33+
* @property {string} rdn - The relative distinguished name to start the query from.
34+
* @property {string} filter - LDAP filter string.
35+
* @property {boolean} boolean - Boolean flag used in query logic.
36+
* @property {number} rowsPerPage - Number of rows to return per page.
37+
* @property {GlideLDAP} glideLdap - GlideLDAP instance used to perform queries.
38+
*/
39+
LDAPquery.prototype = {
40+
/**
41+
* Initializes the LDAPquery instance with default values.
42+
*/
43+
initialize: function () {
44+
this.ldapConfig = '';
45+
this.rdn = '';
46+
this.filter = '';
47+
this.boolean = true;
48+
this.rowsPerPage = 1;
49+
this.glideLdap = new GlideLDAP();
50+
},
51+
52+
/**
53+
* Sets the relative distinguished name (RDN) for the query.
54+
* @param {string} rdn - The RDN string.
55+
* @returns {LDAPquery} Returns the current instance for chaining.
56+
*/
57+
setRDN: function (rdn) {
58+
this.rdn = rdn || '';
59+
return this;
60+
},
61+
62+
/**
63+
* Sets the LDAP filter string.
64+
* @param {string} filter - The LDAP filter.
65+
* @returns {LDAPquery} Returns the current instance for chaining.
66+
*/
67+
setFilter: function (filter) {
68+
this.filter = filter || '';
69+
return this;
70+
},
71+
72+
/**
73+
* Sets the number of rows to return per page.
74+
* @param {number} rows - Number of rows.
75+
* @returns {LDAPquery} Returns the current instance for chaining.
76+
*/
77+
setPagination: function (rows) {
78+
this.rowsPerPage = rows || 1;
79+
return this;
80+
},
81+
82+
/**
83+
* Sets the LDAP server configuration using its sys_id.
84+
* Also sets the config on the GlideLDAP instance.
85+
* @param {string} config - The sys_id of the LDAP server config.
86+
* @returns {LDAPquery} Returns the current instance for chaining.
87+
*/
88+
setConfig: function (config) {
89+
this.ldapConfig = config || '';
90+
this.glideLdap.setConfigID(config);
91+
return this;
92+
},
93+
94+
/**
95+
* Executes the query and returns the first matching LDAP entry.
96+
* @returns {Object|null} Returns the first matching entry as a JavaScript object, or null if none found.
97+
* @throws Will raise an error if ldapConfig is not set.
98+
*/
99+
getOne: function () {
100+
if (!this.ldapConfig) {
101+
NiceError.raise('no ldap config defined');
102+
}
103+
var entry;
104+
if (entry = this._query().next()) {
105+
return j2js(entry);
106+
}
107+
return null;
108+
},
109+
110+
/**
111+
* Executes the query and returns an iterable result set.
112+
* @returns Returns a iterator over matching LDAP entries.
113+
* @throws Will raise an error if ldapConfig is not set.
114+
*/
115+
getIterable: function () {
116+
if (!this.ldapConfig) {
117+
NiceError.raise('no ldap config defined');
118+
}
119+
return this._query();
120+
},
121+
122+
/**
123+
* Internal method to perform the LDAP query using GlideLDAP.
124+
* @private
125+
* @returns Returns the result iterator.
126+
*/
127+
_query: function () {
128+
return this.glideLdap.getMatching(this.rdn, this.filter, this.boolean, this.rowsPerPage);
129+
},
130+
131+
type: 'LDAPquery'
132+
};
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Query ldap server by config sys_id
2+
Interact with GlideLDAP using a fluent wrapper. Handy for querying ldap server for non-persistent data for example via remote tables.
3+
4+
# How to use it?
5+
Configure LDAP server in *ldap_server_config* table
6+
Query server from serverside script using script include LDAPQuery
7+
8+
# Example 1: get one entry
9+
10+
```javascript
11+
var result = new LDAPquery()
12+
.setRDN('') //set relative start
13+
.setFilter('uid=einstein') //set filter
14+
.setConfig('2fd003c083e07e10557ff0d6feaad3d7') //set the sys_id for ldap_server_config record
15+
.getOne(); //returns first entry as js object
16+
gs.info(result.telephoneNumber);
17+
```
18+
```
19+
output:
20+
*** Script: 314-159-2653
21+
```
22+
23+
# Example 2: get the iterable for query
24+
25+
```javascript
26+
var result = new LDAPquery()
27+
.setRDN('ou=scientists')
28+
.setPagination(1000) //set how many records per page
29+
.setConfig('2fd003c083e07e10557ff0d6feaad3d7')
30+
.getIterable(); //returns iterable result object
31+
32+
var item;
33+
while (item = result.next()) {
34+
gs.info(JSON.stringify(j2js(item), null, 2)); //note that next returns a java object, hence the j2js to convert to js
35+
}
36+
```
37+
```
38+
output:
39+
*** Script: {
40+
"dn": "ou=scientists,dc=example,dc=com",
41+
"objectClass": "groupOfUniqueNames^top",
42+
"ou": "scientists",
43+
"source": "ldap:ou=scientists,dc=example,dc=com",
44+
"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",
45+
"cn": "Scientists"
46+
}
47+
*** Script: {
48+
"dn": "ou=italians,ou=scientists,dc=example,dc=com",
49+
"objectClass": "groupOfUniqueNames^top",
50+
"ou": "italians",
51+
"source": "ldap:ou=italians,ou=scientists,dc=example,dc=com",
52+
"uniqueMember": "uid=tesla,dc=example,dc=com",
53+
"cn": "Italians"
54+
}
55+
LDAP SEARCH: >>Next Page
56+
```

0 commit comments

Comments
 (0)