node-ldapjs/lib/messages/search_response.js

152 lines
4.2 KiB
JavaScript
Raw Normal View History

2011-08-04 20:32:01 +00:00
// Copyright 2011 Mark Cavage, Inc. All rights reserved.
var assert = require('assert-plus');
2011-08-04 20:32:01 +00:00
var util = require('util');
var LDAPResult = require('./result');
var SearchEntry = require('./search_entry');
var SearchReference = require('./search_reference');
2011-08-04 20:32:01 +00:00
2011-11-15 18:49:23 +00:00
var dtrace = require('../dtrace');
var parseDN = require('../dn').parse;
var parseURL = require('../url').parse;
2011-08-04 20:32:01 +00:00
var Protocol = require('../protocol');
///--- API
function SearchResponse(options) {
options = options || {};
assert.object(options);
2011-08-04 20:32:01 +00:00
options.protocolOp = Protocol.LDAP_REP_SEARCH;
LDAPResult.call(this, options);
this.attributes = options.attributes ? options.attributes.slice() : [];
this.notAttributes = [];
this.sentEntries = 0;
2011-08-04 20:32:01 +00:00
}
util.inherits(SearchResponse, LDAPResult);
/**
* Allows you to send a SearchEntry back to the client.
*
* @param {Object} entry an instance of SearchEntry.
* @param {Boolean} nofiltering skip filtering notAttributes and '_' attributes.
* Defaults to 'false'.
2011-08-04 20:32:01 +00:00
*/
SearchResponse.prototype.send = function (entry, nofiltering) {
if (!entry || typeof (entry) !== 'object')
2011-08-04 20:32:01 +00:00
throw new TypeError('entry (SearchEntry) required');
if (nofiltering === undefined)
nofiltering = false;
if (typeof (nofiltering) !== 'boolean')
throw new TypeError('noFiltering must be a boolean');
2011-08-04 20:32:01 +00:00
var self = this;
if (entry instanceof SearchEntry || entry instanceof SearchReference) {
if (!entry.messageID)
entry.messageID = this.messageID;
if (entry.messageID !== this.messageID)
throw new Error('SearchEntry messageID mismatch');
} else {
if (!entry.attributes)
throw new Error('entry.attributes required');
var savedAttrs = {};
var all = (self.attributes.indexOf('*') !== -1);
Object.keys(entry.attributes).forEach(function (a) {
var _a = a.toLowerCase();
if (!nofiltering && _a.length && _a[0] === '_') {
savedAttrs[a] = entry.attributes[a];
delete entry.attributes[a];
} else if (!nofiltering && self.notAttributes.indexOf(_a) !== -1) {
savedAttrs[a] = entry.attributes[a];
delete entry.attributes[a];
} else if (all) {
return;
} else if (self.attributes.length && self.attributes.indexOf(_a) === -1) {
savedAttrs[a] = entry.attributes[a];
delete entry.attributes[a];
}
});
var save = entry;
entry = new SearchEntry({
objectName: typeof (save.dn) === 'string' ? parseDN(save.dn) : save.dn,
messageID: self.messageID,
2012-02-18 08:54:22 +00:00
log: self.log
});
entry.fromObject(save);
}
2011-08-04 20:32:01 +00:00
try {
2012-02-18 08:54:22 +00:00
if (this.log.debug())
this.log.debug('%s: sending: %j', this.connection.ldap.id, entry.json);
this.connection.write(entry.toBer());
this.sentEntries++;
2011-11-15 18:49:23 +00:00
if (self._dtraceOp && self._dtraceId) {
dtrace.fire('server-search-entry', function () {
2011-11-15 18:49:23 +00:00
var c = self.connection || {ldap: {}};
return [
self._dtraceId || 0,
(c.remoteAddress || ''),
c.ldap.bindDN ? c.ldap.bindDN.toString() : '',
(self.requestDN ? self.requestDN.toString() : ''),
entry.objectName.toString(),
entry.attributes.length
2011-11-15 18:49:23 +00:00
];
});
}
// Restore attributes
Object.keys(savedAttrs || {}).forEach(function (k) {
save.attributes[k] = savedAttrs[k];
});
} catch (e) {
this.log.warn(e, '%s failure to write message %j',
this.connection.ldap.id, this.json);
}
2011-08-04 20:32:01 +00:00
};
SearchResponse.prototype.createSearchEntry = function (object) {
assert.object(object);
2011-08-04 20:32:01 +00:00
var entry = new SearchEntry({
messageID: this.messageID,
log: this.log,
objectName: object.objectName || object.dn
});
entry.fromObject((object.attributes || object));
return entry;
2011-08-04 20:32:01 +00:00
};
SearchResponse.prototype.createSearchReference = function (uris) {
if (!uris)
throw new TypeError('uris ([string]) required');
if (!Array.isArray(uris))
uris = [uris];
for (var i = 0; i < uris.length; i++) {
if (typeof (uris[i]) == 'string')
uris[i] = parseURL(uris[i]);
}
var self = this;
return new SearchReference({
messageID: self.messageID,
2012-02-18 08:54:22 +00:00
log: self.log,
uris: uris
});
};
///--- Exports
module.exports = SearchResponse;