2011-08-04 20:32:01 +00:00
|
|
|
// Copyright 2011 Mark Cavage, Inc. All rights reserved.
|
|
|
|
|
|
|
|
var assert = require('assert');
|
|
|
|
var util = require('util');
|
|
|
|
|
|
|
|
var LDAPMessage = require('./message');
|
|
|
|
var dn = require('../dn');
|
|
|
|
var Attribute = require('../attribute');
|
|
|
|
var Protocol = require('../protocol');
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-09-30 23:39:19 +00:00
|
|
|
///--- Globals
|
|
|
|
|
|
|
|
var isDN = dn.DN.isDN;
|
2011-08-04 20:32:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
///--- API
|
|
|
|
|
|
|
|
function AddRequest(options) {
|
|
|
|
if (options) {
|
2012-02-18 08:15:52 +00:00
|
|
|
if (typeof (options) !== 'object')
|
2011-08-04 20:32:01 +00:00
|
|
|
throw new TypeError('options must be an object');
|
2014-09-30 23:39:19 +00:00
|
|
|
if (options.entry &&
|
|
|
|
!(isDN(options.entry) || typeof (options.entry) === 'string')) {
|
|
|
|
throw new TypeError('options.entry must be a DN or string');
|
|
|
|
}
|
2011-08-04 20:32:01 +00:00
|
|
|
if (options.attributes) {
|
|
|
|
if (!Array.isArray(options.attributes))
|
|
|
|
throw new TypeError('options.attributes must be [Attribute]');
|
2012-02-18 08:15:52 +00:00
|
|
|
options.attributes.forEach(function (a) {
|
2011-08-04 20:32:01 +00:00
|
|
|
if (!Attribute.isAttribute(a))
|
|
|
|
throw new TypeError('options.attributes must be [Attribute]');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
options = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
options.protocolOp = Protocol.LDAP_REQ_ADD;
|
|
|
|
LDAPMessage.call(this, options);
|
|
|
|
|
|
|
|
this.entry = options.entry || null;
|
|
|
|
this.attributes = options.attributes ? options.attributes.slice(0) : [];
|
|
|
|
|
|
|
|
var self = this;
|
2012-02-18 08:15:52 +00:00
|
|
|
this.__defineGetter__('type', function () { return 'AddRequest'; });
|
|
|
|
this.__defineGetter__('_dn', function () { return self.entry; });
|
2011-08-04 20:32:01 +00:00
|
|
|
}
|
|
|
|
util.inherits(AddRequest, LDAPMessage);
|
|
|
|
module.exports = AddRequest;
|
|
|
|
|
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
AddRequest.prototype._parse = function (ber) {
|
2011-08-04 20:32:01 +00:00
|
|
|
assert.ok(ber);
|
|
|
|
|
2014-09-30 23:39:19 +00:00
|
|
|
this.entry = ber.readString();
|
2011-08-04 20:32:01 +00:00
|
|
|
|
|
|
|
ber.readSequence();
|
|
|
|
|
|
|
|
var end = ber.offset + ber.length;
|
|
|
|
while (ber.offset < end) {
|
|
|
|
var a = new Attribute();
|
|
|
|
a.parse(ber);
|
2011-10-04 17:27:43 +00:00
|
|
|
a.type = a.type.toLowerCase();
|
2011-09-15 21:49:00 +00:00
|
|
|
if (a.type === 'objectclass') {
|
|
|
|
for (var i = 0; i < a.vals.length; i++)
|
|
|
|
a.vals[i] = a.vals[i].toLowerCase();
|
|
|
|
}
|
2011-08-04 20:32:01 +00:00
|
|
|
this.attributes.push(a);
|
|
|
|
}
|
|
|
|
|
2011-08-08 15:03:00 +00:00
|
|
|
this.attributes.sort(Attribute.compare);
|
2011-08-04 20:32:01 +00:00
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
AddRequest.prototype._toBer = function (ber) {
|
2011-08-04 20:32:01 +00:00
|
|
|
assert.ok(ber);
|
|
|
|
|
|
|
|
ber.writeString(this.entry.toString());
|
|
|
|
ber.startSequence();
|
2012-02-18 08:15:52 +00:00
|
|
|
this.attributes.forEach(function (a) {
|
2011-08-04 20:32:01 +00:00
|
|
|
a.toBer(ber);
|
|
|
|
});
|
|
|
|
ber.endSequence();
|
|
|
|
|
|
|
|
return ber;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
AddRequest.prototype._json = function (j) {
|
2011-08-04 20:32:01 +00:00
|
|
|
assert.ok(j);
|
|
|
|
|
|
|
|
j.entry = this.entry.toString();
|
|
|
|
j.attributes = [];
|
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
this.attributes.forEach(function (a) {
|
2011-08-04 20:32:01 +00:00
|
|
|
j.attributes.push(a.json);
|
|
|
|
});
|
|
|
|
|
|
|
|
return j;
|
|
|
|
};
|
2011-08-08 15:03:00 +00:00
|
|
|
|
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
AddRequest.prototype.indexOf = function (attr) {
|
|
|
|
if (!attr || typeof (attr) !== 'string')
|
2011-08-08 15:03:00 +00:00
|
|
|
throw new TypeError('attr (string) required');
|
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
for (var i = 0; i < this.attributes.length; i++) {
|
2011-08-12 23:37:47 +00:00
|
|
|
if (this.attributes[i].type === attr)
|
2011-08-08 15:03:00 +00:00
|
|
|
return i;
|
2012-02-18 08:15:52 +00:00
|
|
|
}
|
2011-08-08 15:03:00 +00:00
|
|
|
|
|
|
|
return -1;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
AddRequest.prototype.attributeNames = function () {
|
2011-08-08 15:03:00 +00:00
|
|
|
var attrs = [];
|
|
|
|
|
|
|
|
for (var i = 0; i < this.attributes.length; i++)
|
2012-02-18 08:15:52 +00:00
|
|
|
attrs.push(this.attributes[i].type.toLowerCase());
|
2011-08-08 15:03:00 +00:00
|
|
|
|
|
|
|
return attrs;
|
|
|
|
};
|
2011-08-10 17:57:58 +00:00
|
|
|
|
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
AddRequest.prototype.getAttribute = function (name) {
|
|
|
|
if (!name || typeof (name) !== 'string')
|
2011-10-04 17:27:43 +00:00
|
|
|
throw new TypeError('attribute name (string) required');
|
2011-08-10 17:57:58 +00:00
|
|
|
|
|
|
|
name = name.toLowerCase();
|
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
for (var i = 0; i < this.attributes.length; i++) {
|
2011-08-10 17:57:58 +00:00
|
|
|
if (this.attributes[i].type === name)
|
2013-10-08 17:57:53 +00:00
|
|
|
return this.attributes[i];
|
2012-02-18 08:15:52 +00:00
|
|
|
}
|
2011-08-10 17:57:58 +00:00
|
|
|
|
|
|
|
return null;
|
|
|
|
};
|
2011-08-12 23:37:47 +00:00
|
|
|
|
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
AddRequest.prototype.addAttribute = function (attr) {
|
2011-08-12 23:37:47 +00:00
|
|
|
if (!(attr instanceof Attribute))
|
2012-02-18 08:15:52 +00:00
|
|
|
throw new TypeError('attribute (Attribute) required');
|
2011-08-12 23:37:47 +00:00
|
|
|
|
|
|
|
return this.attributes.push(attr);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a "pure" JS representation of this object.
|
|
|
|
*
|
|
|
|
* An example object would look like:
|
|
|
|
*
|
|
|
|
* {
|
|
|
|
* "dn": "cn=unit, dc=test",
|
|
|
|
* "attributes": {
|
|
|
|
* "cn": ["unit", "foo"],
|
|
|
|
* "objectclass": ["top", "person"]
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* @return {Object} that looks like the above.
|
|
|
|
*/
|
2012-02-18 08:15:52 +00:00
|
|
|
AddRequest.prototype.toObject = function () {
|
2011-08-12 23:37:47 +00:00
|
|
|
var self = this;
|
|
|
|
|
|
|
|
var obj = {
|
|
|
|
dn: self.entry ? self.entry.toString() : '',
|
|
|
|
attributes: {}
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!this.attributes || !this.attributes.length)
|
|
|
|
return obj;
|
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
this.attributes.forEach(function (a) {
|
2011-08-12 23:37:47 +00:00
|
|
|
if (!obj.attributes[a.type])
|
|
|
|
obj.attributes[a.type] = [];
|
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
a.vals.forEach(function (v) {
|
2011-08-12 23:37:47 +00:00
|
|
|
if (obj.attributes[a.type].indexOf(v) === -1)
|
|
|
|
obj.attributes[a.type].push(v);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
return obj;
|
|
|
|
};
|