2011-08-04 20:32:01 +00:00
|
|
|
// Copyright 2011 Mark Cavage, Inc. All rights reserved.
|
|
|
|
|
2015-10-31 17:28:25 +00:00
|
|
|
var assert = require('assert-plus');
|
2011-08-04 20:32:01 +00:00
|
|
|
var util = require('util');
|
|
|
|
|
|
|
|
var asn1 = require('asn1');
|
|
|
|
|
|
|
|
var LDAPMessage = require('./message');
|
|
|
|
var Attribute = require('../attribute');
|
|
|
|
var Protocol = require('../protocol');
|
2015-10-31 17:28:25 +00:00
|
|
|
var lassert = require('../assert');
|
2011-08-04 20:32:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
///--- Globals
|
|
|
|
|
|
|
|
var BerWriter = asn1.BerWriter;
|
|
|
|
|
|
|
|
|
|
|
|
///--- API
|
|
|
|
|
|
|
|
function SearchEntry(options) {
|
2015-10-31 17:28:25 +00:00
|
|
|
options = options || {};
|
|
|
|
assert.object(options);
|
|
|
|
lassert.optionalStringDN(options.objectName);
|
2011-08-04 20:32:01 +00:00
|
|
|
|
|
|
|
options.protocolOp = Protocol.LDAP_REP_SEARCH_ENTRY;
|
|
|
|
LDAPMessage.call(this, options);
|
|
|
|
|
|
|
|
this.objectName = options.objectName || null;
|
2011-08-04 21:25:12 +00:00
|
|
|
this.setAttributes(options.attributes || []);
|
2015-10-31 17:28:25 +00:00
|
|
|
}
|
|
|
|
util.inherits(SearchEntry, LDAPMessage);
|
|
|
|
Object.defineProperties(SearchEntry.prototype, {
|
|
|
|
type: {
|
|
|
|
get: function getType() { return 'SearchEntry'; },
|
|
|
|
configurable: false
|
|
|
|
},
|
|
|
|
_dn: {
|
|
|
|
get: function getDN() { return this.objectName; },
|
|
|
|
configurable: false
|
|
|
|
},
|
|
|
|
object: {
|
|
|
|
get: function getObject() {
|
|
|
|
var obj = {
|
|
|
|
dn: this.dn.toString(),
|
|
|
|
controls: []
|
|
|
|
};
|
|
|
|
this.attributes.forEach(function (a) {
|
|
|
|
if (a.vals && a.vals.length) {
|
|
|
|
if (a.vals.length > 1) {
|
|
|
|
obj[a.type] = a.vals.slice();
|
|
|
|
} else {
|
|
|
|
obj[a.type] = a.vals[0];
|
|
|
|
}
|
2011-09-30 17:48:56 +00:00
|
|
|
} else {
|
2015-10-31 17:28:25 +00:00
|
|
|
obj[a.type] = [];
|
2011-09-30 17:48:56 +00:00
|
|
|
}
|
2015-10-31 17:28:25 +00:00
|
|
|
});
|
|
|
|
this.controls.forEach(function (element, index, array) {
|
|
|
|
obj.controls.push(element.json);
|
|
|
|
});
|
|
|
|
return obj;
|
|
|
|
},
|
|
|
|
configurable: false
|
|
|
|
},
|
|
|
|
raw: {
|
|
|
|
get: function getRaw() {
|
|
|
|
var obj = {
|
|
|
|
dn: this.dn.toString(),
|
|
|
|
controls: []
|
|
|
|
};
|
|
|
|
|
|
|
|
this.attributes.forEach(function (a) {
|
|
|
|
if (a.buffers && a.buffers.length) {
|
|
|
|
if (a.buffers.length > 1) {
|
|
|
|
obj[a.type] = a.buffers.slice();
|
|
|
|
} else {
|
|
|
|
obj[a.type] = a.buffers[0];
|
|
|
|
}
|
2012-12-07 22:07:53 +00:00
|
|
|
} else {
|
2015-10-31 17:28:25 +00:00
|
|
|
obj[a.type] = [];
|
2012-12-07 22:07:53 +00:00
|
|
|
}
|
2015-10-31 17:28:25 +00:00
|
|
|
});
|
|
|
|
this.controls.forEach(function (element, index, array) {
|
|
|
|
obj.controls.push(element.json);
|
|
|
|
});
|
|
|
|
return obj;
|
|
|
|
},
|
|
|
|
configurable: false
|
|
|
|
}
|
|
|
|
});
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
SearchEntry.prototype.addAttribute = function (attr) {
|
|
|
|
if (!attr || typeof (attr) !== 'object')
|
2011-08-04 20:32:01 +00:00
|
|
|
throw new TypeError('attr (attribute) required');
|
|
|
|
|
|
|
|
this.attributes.push(attr);
|
|
|
|
};
|
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
SearchEntry.prototype.toObject = function () {
|
2011-08-23 00:16:36 +00:00
|
|
|
return this.object;
|
|
|
|
};
|
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
SearchEntry.prototype.fromObject = function (obj) {
|
|
|
|
if (typeof (obj) !== 'object')
|
2011-08-12 23:37:47 +00:00
|
|
|
throw new TypeError('object required');
|
|
|
|
|
|
|
|
var self = this;
|
2011-12-20 00:52:48 +00:00
|
|
|
if (obj.controls)
|
|
|
|
this.controls = obj.controls;
|
2011-08-12 23:37:47 +00:00
|
|
|
|
|
|
|
if (obj.attributes)
|
|
|
|
obj = obj.attributes;
|
|
|
|
this.attributes = [];
|
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
Object.keys(obj).forEach(function (k) {
|
2011-10-11 20:56:16 +00:00
|
|
|
self.attributes.push(new Attribute({type: k, vals: obj[k]}));
|
2011-08-12 23:37:47 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
SearchEntry.prototype.setAttributes = function (obj) {
|
|
|
|
if (typeof (obj) !== 'object')
|
2011-08-04 21:25:12 +00:00
|
|
|
throw new TypeError('object required');
|
|
|
|
|
|
|
|
if (Array.isArray(obj)) {
|
2012-02-18 08:15:52 +00:00
|
|
|
obj.forEach(function (a) {
|
2011-08-04 21:25:12 +00:00
|
|
|
if (!Attribute.isAttribute(a))
|
|
|
|
throw new TypeError('entry must be an Array of Attributes');
|
|
|
|
});
|
|
|
|
this.attributes = obj;
|
|
|
|
} else {
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
self.attributes = [];
|
2012-02-18 08:15:52 +00:00
|
|
|
Object.keys(obj).forEach(function (k) {
|
2011-08-04 21:25:12 +00:00
|
|
|
var attr = new Attribute({type: k});
|
|
|
|
if (Array.isArray(obj[k])) {
|
2012-02-18 08:15:52 +00:00
|
|
|
obj[k].forEach(function (v) {
|
2011-08-04 21:25:12 +00:00
|
|
|
attr.addValue(v.toString());
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
attr.addValue(obj[k].toString());
|
|
|
|
}
|
|
|
|
self.attributes.push(attr);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
SearchEntry.prototype._json = function (j) {
|
2011-08-04 20:32:01 +00:00
|
|
|
assert.ok(j);
|
|
|
|
|
|
|
|
j.objectName = this.objectName.toString();
|
|
|
|
j.attributes = [];
|
2012-02-18 08:15:52 +00:00
|
|
|
this.attributes.forEach(function (a) {
|
2011-08-10 21:46:04 +00:00
|
|
|
j.attributes.push(a.json || a);
|
2011-08-04 20:32:01 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
return j;
|
|
|
|
};
|
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
SearchEntry.prototype._parse = function (ber) {
|
2011-08-04 20:32:01 +00:00
|
|
|
assert.ok(ber);
|
|
|
|
|
|
|
|
this.objectName = ber.readString();
|
|
|
|
assert.ok(ber.readSequence());
|
|
|
|
|
2011-11-11 18:08:48 +00:00
|
|
|
var end = ber.offset + ber.length;
|
2011-08-04 20:32:01 +00:00
|
|
|
while (ber.offset < end) {
|
|
|
|
var a = new Attribute();
|
|
|
|
a.parse(ber);
|
|
|
|
this.attributes.push(a);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
SearchEntry.prototype._toBer = function (ber) {
|
2011-08-04 20:32:01 +00:00
|
|
|
assert.ok(ber);
|
|
|
|
|
|
|
|
ber.writeString(this.objectName.toString());
|
|
|
|
ber.startSequence();
|
2012-02-18 08:15:52 +00:00
|
|
|
this.attributes.forEach(function (a) {
|
2011-08-04 20:32:01 +00:00
|
|
|
// This may or may not be an attribute
|
|
|
|
ber = Attribute.toBer(a, ber);
|
|
|
|
});
|
|
|
|
ber.endSequence();
|
|
|
|
|
|
|
|
return ber;
|
|
|
|
};
|
2015-10-31 17:28:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
///--- Exports
|
|
|
|
|
|
|
|
module.exports = SearchEntry;
|