Skip to content
Closed
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
var LDAPquery = Class.create();
LDAPquery.prototype = {
initialize: function () {
this.LDAP_CONFIG = '';
this.RDN = '';
this.FILTER = '';
this.BOOLEAN = true;
this.ROWS_PAGE = 1;
this.GLIDE_LDAP = new GlideLDAP();
},

setRDN: function (rdn) {
this.RDN = rdn || '';
return this;
},

setFilter: function (filter) {
this.FILTER = filter || '';
return this;
},

setPagination: function (rows) {
this.ROWS_PAGE = rows || 1;
return this;
},

setConfig: function (config) {
this.LDAP_CONFIG = config || '';
this.GLIDE_LDAP.setConfigID(config)
return this;
},

getOne: function () {
if (!this.LDAP_CONFIG) {
NiceError.raise('no ldap config defined');
}
var entry;

if (entry = this._query().next()) {
return j2js(entry);
}
return null;
},

getIterable: function () {
if (!this.LDAP_CONFIG) {
NiceError.raise('no ldap config defined');
}
return this._query();
},

_query: function () {
return this.GLIDE_LDAP.getMatching(this.RDN, this.FILTER, this.BOOLEAN, this.ROWS_PAGE);
},
type: 'LDAPquery'
};
55 changes: 55 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,55 @@
# Query ldap server by config sys_id

# 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