|
| 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 | +}; |
0 commit comments